Style PHP using echo Issue - php

Using this code on my WP site and need help styling the echo lines of code;
`<?php
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
echo '' . esc_html( $current_user->display_name );
echo get_avatar( $current_user->ID, 38 );
}
}
?>`
Would like to use CSS classes, but when I try the below code it doesn't work;
echo '<p class="CSS Style">' . ($current_user->display_name ) '</p>';
Thanks to anyone that can help. Probably pretty simple to most developers.

I try run your code, i think you are missing the ' . ' before </p>.
echo '<p class="CSS Style">' . ($current_user->display_name ) . '</p>';

Hey you can also do something like this if you want write a standalone P tag outside the PHP logic.
<?php
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) { ?>
<p class="username"><?php echo $current_user->display_name; ?></p>
<?php
echo get_avatar( $current_user->ID, 38 );
}
}
?>
Otherwise, you can only add a dot before P closing tag.

Use this line of code
echo '<p class="CSS Style">' . ($current_user->display_name ).'</p>';
Your forgot to give period(.);

Related

Wordpress ACF Get Block Field

I'm running into an issue with ACF, and I just can't figure out what's going on, and nothing on the internet is helping out.
I've added some fields to the Image Slider block:
But no matter what I try inside of our custom block code: image-slider.php I cannot get the values of any of the auto_play fields. get_field always returns null. I know the value is there, because if I dump out get_fields( $postID ), I can see the ['page_builder'][2] element has the value I want. I could get to it that way, but I can't seem to determine which index I'm on (the 2) programmatically.
So if you know either, how I can access the field directly, or figure out my current 'page_builder' index, that would be extremely helpful.
It's super confusing, because the have_rows( 'slide_setting' ) call obviously knows where to look, and works as expected.
The custom block php looks like:
<?php
if(have_rows( 'slide_setting' ) ) {
$digits = 3;
$randID = rand(pow(10, $digits-1), pow(10, $digits)-1);
echo '<div class="container"><div class="row"><div id="swiper_'.$randID.'" class="col-md-12 wiche-swiper-top-navigation-wrapper">';
echo '<div class="swiper-container wiche-swiper-top-navigation">';
// var_dump( get_fields( get_the_ID() )['page_builder'][2] );
// var_dump( get_post_field( 'auto_play' ) );
// var_dump(get_field('image_slider_settings_auto_play'));
// var_dump(get_row_index());
// var_dump(get_field_objects( $post->ID ));
// var_dump( get_row_index() );
// var_dump( acf_get_field_group( 'slide_setting' ) );
// die();
if ( get_field( 'auto_play' ) ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . get_field( 'auto_play_delay' ) . '" data-swiper-disable-on-interaction="' . get_field( 'auto_play_disable_on_interaction' ) . '">';
} else {
echo '<div class="swiper-wrapper">';
}
while( have_rows( 'slide_setting' ) ) {
the_row();
$title = get_sub_field( 'title' );
$image = get_sub_field( 'image' );
$content = get_sub_field( 'content' );
if ( $image || $content ) {
echo '<div class="swiper-slide swiper-banner-slide swiper-no-swiping">';
if ( $title ) {
echo '<div class="text-center slider-top-title">';
echo $title;
echo '</div>';
}
if ( $image ) {
echo '<div class="banner-image">';
echo wp_get_attachment_image( $image, 'full', '', array( 'loading' => false ) );
echo '</div>';
}
if ( $content ) {
echo '<div class="banner-content">';
echo $content;
echo '</div>';
}
echo '</div>';
}
}
echo '</div>';
echo '</div>';
echo '<div class="swiper-button-next swiper-button-next-outsite">Next</div><div class="swiper-button-prev swiper-button-prev-outsite">Prev</div>';
echo '</div></div></div>';
}
So I wasn't able to get a perfect answer to my question, looks like the API to get what I want doesn't exist (dumb).
What I ended up with - I set up a new function in my theme's functions.php file that looks like the following:
$post_slider_config_index = 0;
function get_the_slider_config( $post_id ) {
global $post_slider_config_index;
$page_builder = get_fields( $post_id )['page_builder'];
$slider_config = null;
foreach ($page_builder as $key => $value) {
if ( $value['acf_fc_layout'] === 'image_slider_settings' ) {
if ( $key > $post_slider_config_index ) {
$slider_config = $value;
$post_slider_config_index = $key;
break;
}
}
}
return $slider_config;
}
And then inside my image-slider.php file I call it like so:
$slider_config = get_the_slider_config( get_the_ID() );
if ( $slider_config[ 'auto_play' ] ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . $slider_config[ 'auto_play_delay' ] . '" data-swiper-disable-on-interaction="' . $slider_config[ 'auto_play_disable_on_interaction' ] . '">';
} else {
echo '<div class="swiper-wrapper">';
}
The $post_slider_config_index variable keeps track of the last index retrieved so that if there are multiple sliders on a page, it'll grab the right one as its rendered.
It's not perfect, it's not super efficient, but it does what I needed. Annoying WP doesn't just give you the information it obviously has already regarding where you are in the page.

PHP echo mehod ignores HTML part

I have written the following PHP code:
function x_woocommerce_after_shop_loop_item() {
$leistung = the_field('leistung_in_w');
if ( ! empty( $leistung ) ) {
echo '<div class="product-meta-leistung">Leistung:' . $leistung . 'W</div>';
}
echo '</div>';
}
the_field() is a function of "advanced custom fields" Plugin. https://www.advancedcustomfields.com/resources/the_field/
The problem is that only the variable is output, but not the HTML part. How can I solve the problem?
I don't know why, but the following code worked:
function x_woocommerce_after_shop_loop_item() {
global $product;
$id = $product->get_id();
$leistung = get_post_meta($id,'leistung_in_w',true);
if ( ! empty( $leistung ) ) {
echo '<div class="product-meta-leistung">Leistung: ' . $leistung . 'W</div>';
}
echo '</div>';
}

execute PHP snippet saved on a textbox inside wp_options table

I have a textbox where I save (inside wp_options table) this php snippet:
<?php if ( current_user_can( 'manage_options' ) ){ echo 'I\'m an admin!'; } ?>
and I like to execute that code in my html like this:
$geo_desktop = get_option( 'geo_desktop' );
if ( !empty( $geo_desktop ) ) { echo '<div class="desktop">' . $geo_desktop . '</div>'; }
But this isn't outputting anything and I should see:
I'm an admin!
However, if I type only text inside that text box, it output's text like a charm...
How can I do this output my PHP code?
You could do like this way :
$data = 'I\'m an admin!';
<?php if ( current_user_can( 'manage_options' ) ){ add_option('geo_desktop',$data); } ?>
then you can call wp_options
$geo_desktop = get_option( 'geo_desktop' );
if ( !empty( $geo_desktop ) ) { echo '<div class="desktop">' . $geo_desktop . '</div>'; }

How can i display only the first image from this JSON array?

OK, I'm trying to integrate an API that lists adoptable pets into a wordpress website. I've done lots of googling and read through tutorials, and so far have managed to put together a super basic plugin that seems to do what I'm trying to accomplish. Currently I'm trying to pull in an image, but just the first image. Each animal may have 5 images associated with it, but I only want to pull in the first (default). Currently my code brings them all. Now I realize the problem is that I'm using "foreach()". But, this is new to me and my googling is not going well, and any other way I've tried to do it is just not getting me ANY pictures. Any advice is appreciated....and if I'm doing anything else wrong, feel free to let me know :) I also need to figure out how to paginate it, but I'm thinking that's a separate question! Thanks!
<?php
add_shortcode('pets', 'petsshortcode');
function petsshortcode() {
$request = wp_remote_get( 'https://petstablished.com/api/v2/public/pets?public_key=UlEK4EWvDAoOjXeQXSCQZAyBywWfqfOg&search[status]=available,foster&pagination[limit]=20&pagination[page]=1' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data ) ) {
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
foreach( $collection->images as $images ) {
echo '<div class="pet-photo"><img src="' . $images->image->url; echo '" width="200"></div>';}
echo '</ul></div>';
}
}
}
You don't need nested foreach loops in this case, simply use just one foreach loop like this:
// your code
foreach( $data->collection as $collection ) {
echo '<div id="pet-block"><ul class="pet-profile"><li class="pet-name">'. $collection->name; echo '</li>';
echo '<li class="pet-meta">'.'<span class="sex">' . $collection->sex; echo '</span>' . '<span class="breed">' . $collection->breed; echo '</span><span class="age">' . $collection->age; echo '</span></li>';
echo '<li><p>' . $collection->description; echo '</li></p>';
echo '<div class="pet-photo"><img src="' . $collection->images[0]->image->url; echo '" width="200"></div>';
echo '</ul></div>';
}
// your code

How to echo a wordpress value inside an 'echo'

I'm trying to do this:
<?php
global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) {
echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>'; "\n";
echo 'Logout';
}
else {
echo 'Login';
}
?>
The problem is that href gives me back the empty value: wp_logout_url( home_url() );
When I use this WORDPRESS call outside the echo it works good, like this eg:
LOGOUT
How can i write this ??
echo 'Logout';
None of those was, but I appreciated.
This works well:
'text
echo 'Logout';
Needs to be changed to
echo 'Logout';
String started with single quote needs to be closed with single quote.
I think what you want is this:
<?php global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) { echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>'; "\n";
echo 'Logout'; } else { echo 'Login'; } ?>
The different is the string is closed before the result of wp_logout_url() is concatenated with it
It's because you've not ended you string
You'd want to use the following:
echo "<a href=\"" . wp_login_url( get_permalink() ); . "\"/>;
Here the correct option
<?php
global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) {
echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>\n';
echo 'Logout';
} else {
echo 'Login';
}
?>

Categories