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>';
}
Related
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.
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(.);
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>'; }
I am not really expert in php, what I want to do is wrap the $duration variable in the last line inside a span tag, so I can style it.
<?php
$duration = '';
if( function_exists( 'cbc_get_video_data' ) ){
global $post;
$video = cbc_get_video_data( $post );
if( $video ) {
$duration = $video->get_human_duration();
}
}
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . $duration . get_the_title() . '</h1>';
?>
when I do something like this the site breaks
<?php
$duration = '';
if( function_exists( 'cbc_get_video_data' ) ){
global $post;
$video = cbc_get_video_data( $post );
if( $video ) {
$duration = $video->get_human_duration();
}
}
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . '<span class="video-duration">'$duration'</span>' . get_the_title() . '</h1>';
?>
Any suggestions?
Thanks
In your 2nd example you are missing a . either side of $duration. You also aren't closing off a <div> and don't need the additional concatenation between the opening span.
To refine this code, try the following:
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title"><span class="video-duration">' . $duration . '</span>' . get_the_title() . '</h1></div>';
I am currently trying to create a plugin using Pinterest for Wordpress. I have ran into a slight problem, I've been trying to return an image with a link but only been able to only return a link.
<?php
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request['body'], true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}
?>
Anyone experienced with Wordpress Plugins and Pinterest can help me with this problem ?
Try to change your code as below. You will get text in $pin['note'] and image url in $img['url'].
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request, true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
$img = $pin['image']['original'];
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}