Product ID should not be accessed directly - php

I recently updated wordpress and PHP on my testing server. I have fixed a lot of the errors but this one is stumping me. On my shop page, my thumbnails stopped appearing. My products and prices are still showing up correctly. Here is the error message
Notice: id was called incorrectly. Product properties should not be accessed directly.
Here is the PHP
<?php
$placeholder_width = $placeholder_height = 400;
$url = wp_get_attachment_image_src( get_post_thumbnail_id($product->ID), array(260,260) );
if ( has_post_thumbnail() ){?>
<?php if ( is_plugin_active( 'woocommerce-lazyload/woocommerce-lazyload.php' )) {?>
<img data-src="<?php echo get_the_post_thumbnail_url($posts->ID , ['220','220']); ?>" class="attachment-220x220 size-220x220 wp-post-image" width="300" height="300">
<?php }else{ ?>
<img src="<?php echo get_the_post_thumbnail_url($posts->ID , ['220','220']); ?>" class="attachment-220x220 size-220x220 wp-post-image">
<?php }?>
<?php
}
else {
echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="' . $placeholder_width . '" height="' . $placeholder_height . '" />';
}
?>
This chunk is where the problem is. I tried to remove the if statement and jsut have the image and URL grab. I tried to convert $product->ID to $product->get_id() and the latter actually takes the error message away but my thumbnails are not showing up. It must be getting postID correct because the names and prices are showing up and being categorized accordingly. Any suggestions?

Related

How do I get ACF image alt text?

I have a wordpress project that is using ACF fields to pass images/video into a carousel. How would I get the alt text for the associated image?
I have tried to get_field('image') and various get_sub_field() calls, but image does not seem to be a field even though get_sub_field('still_image_url') and get_sub_field('image_link') are already pulling in the respective data for those fields.
I'm not even sure how to get the id for the image. Another php file is using the_ID();, but that is not working here.
<?php while (have_rows('top_slider')) : the_row(); ?>
<?php
$video_url = get_sub_field('video_url');
$video_url_responsive = get_sub_field('video_url_responsive');
$video_link = get_sub_field('video_link');
$image_url = get_sub_field('still_image_url');
$image_link = get_sub_field('image_link');
$has_target = strpos($image_link, '/') !== 0;
?>
Make sure you are using the return format of the Image either as IMAGE ARRAY or IMAGE ID.
Use the below code to get the ALT tag of the image if the return format is IMAGE ARRAY.
<?php
$image =get_sub_field('image');
if( !empty($image )): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
Use the below code to get the ALT tag of the image if the return format is IMAGE ID.
$image_id = get_sub_field('image');
$img_url = wp_get_attachment_image_src($image_id, 'full');
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_title = get_the_title($image_id);
?>
<img src="<?php echo $img_url[0]; ?>" width="<?php echo $img_url[1]; ?>" height="<?php echo $img_url[2]; ?>" alt="<?php echo $image_alt; ?>" title="<?php echo $image_title; ?>">
Here the "image" denotes the field name which you set while creating the field.
get_sub_field('image');
Refer the image for understanding about Field Name, Return Format etc
You can see the image id and other details on the REST API if you will set the options to show on REST API.
After adding the contents to WP REST API, you can display the pages' content on the WP REST API as you can use this example link:
https://write-your-site-url-here/wp-json/wp/v2/pages

Displaying an image via acf in the theme header

I am using AFC Pro and created an options pafe so tjhat the user can upload a new header logo whenever he wants to.
The ACF field is called "headerlogo".
What I want now is that the Logo gets replaced by my theme automatically.
My Variables are:
$headerlogo = wp_get_attachment_image_src(get_field('headerlogo', 'option'), 'full');
$default_logo = '<img src="'echo .$headerlogo[0].'" alt="SITE Logo">';
they get called in:
echo '<a href="'. esc_url( home_url( '/' ) ) .'">
' . $default_logo . '
</a>';
But the Output is:
<a href="http://www.xxx.de/">
<img src="" alt="SITELogo">
</a>
What am I doing wrong here?
Thank in advance.
This should work:
<?php
$headerlogo = get_field('headerlogo');
if( !empty($headerlogo) ):
$default_logo = '<img src="'. $headerlogo['url'] . '" alt="' . $headerlogo['alt'] . '" />';
endif;
echo '' . $default_logo . '';
?>

Conditional featured image with youtube thumbnail in Wordpress

Basically what I am trying to do here is: Display the youtube thumbnail. If no youtube thumbnail -> Display the Featured image for the post. If no featured image -> Display the fallback image.
However it seems I am doing something wrong because the webpage displays blank.
<?php
// Check if the post has a Youtube thumbnail (using custom field video_url)
if get_post_meta($post->ID,'video_url',true)
echo '<img src="http://img.youtube.com/vi/' . get_post_meta($post->ID,'video_url',true) . '/0.jpg"/>';
// Check if the post has a Post Thumbnail assigned to it
else ( has_post_thumbnail() ) {
echo '<a href="' . get_permalink($post->ID) . '" >';
the_post_thumbnail('frontpage-thumb');
echo '</a>';
// If the post does not have a featured image then display the fallback image
} else {
echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
?>
The general code for displaying the youtube thumbnail with custom fields is <img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/> I just can't get it to work with the conditional statements...
In your first if condition, you have wrong syntax and the PHP must be closed
if (get_post_meta($post->ID,'video_url',true)) //
Correct code
<?php
if (get_post_meta($post->ID,'video_url',true)) ?>
echo '<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/>';
// Check if the post has a Post Thumbnail assigned to it
else ( has_post_thumbnail() ) {
echo '<a href="' . get_permalink($post->ID) . '" >';
the_post_thumbnail('frontpage-thumb');
echo '</a>';
// If the post does not have a featured image then display the fallback image
} else {
echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
?>

WordPress PHP - Hyperlink on image

first sorry if this is a simple question, its my first time with html / word press and this is just a learning exercise
basically i have downloaded the lawyeria lite theme on word-press, and in this theme they have three icons on the homepage with text underneath them , and what i want to do is make it so a user can click on the image and gets taken to another page, however in the customization options of the theme this is not possible.
i think i have found the php related for this, below, my question is how can i modify this to achieve my aim ?
<?php
if ( get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_icon',get_template_directory_uri().'/images/features-box-icon-one.png' ) ) {
echo '<div class="features-box-icon">';
echo '<img src="'.get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_icon', get_template_directory_uri().'/images/features-box-icon-one.png' ).'" alt="'.get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_title' ).'" title="'.get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_title' ).'" ;/>';
echo '</div>';
}
if ( get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_title','Lorem' ) ) {
echo '<h4>';
echo get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_title','Lorem' );
echo '</h4>';
}
if ( get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_content','Go to Appearance - Customize, to add content.' ) ) {
echo '<p>';
echo get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_content','Go to Appearance - Customize, to add content.' );
echo '</p>';
}
?>
</div><!--/div .features-box-->
and i have narrowed it down to this line of code :
echo '<img src="'.get_theme_mod( 'lawyeria_lite_frontpage_secondlybox_icon',get_template_directory_uri().'/images/features-box-icon-two.png' ).'" alt="'.get_theme_mod( 'lawyeria_lite_frontpage_secondlybox_title','Ipsum' ).'" title="'.get_theme_mod( 'lawyeria_lite_frontpage_secondlybox_title','Ipsum' ).'" />';
i have tried adding in a href
echo '< href="page.php" 'img src="'.get_theme_mod( 'lawyeria_lite_frontpage_secondlybox_icon',get_template_directory_uri().'/images/features-box-icon-two.png' ).'" alt="'.get_theme_mod( 'lawyeria_lite_frontpage_secondlybox_title','Ipsum' ).'" title="'.get_theme_mod( 'lawyeria_lite_frontpage_secondlybox_title','Ipsum' ).'" />';
but this give a HTTP ERROR 500
many thanks
Well it looks like i was on the right track but wrong format, this is how i have achieved what i wanted
echo '<img src="'.get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_icon', get_template_directory_uri().'/images/features-box-icon-one.png' ).'" alt="'.get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_title' ).'" title="'.get_theme_mod( 'lawyeria_lite_frontpage_firstlybox_title' ).'" ;/>';

conditional logic in the header.php

So I am using Wordpress and I have to have a specific logo on a specific page. From research I have to use conditional logic to swap the existing logo with another depending on the current page. Everything I have tried seems to just break the theme.. Any help on guiding me in the correct direction? So basically every page except page_id=79 would have the same logo in the header.
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo'])) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
<?php if ( is_page(79) ) { ?>
What to displayed on page 79.
<?php } else { ?>
What will be displayed everywhere else.
<?php } ?>
This should work.
Try using get_queried_object_id();
<a id="logo" href="<?php echo home_url(); ?>">
<?php
if(!empty($options['use-logo']) && get_queried_object_id() != 79) {
$default_logo_id = (!empty($options['retina-logo'])) ? 'id="default-logo"' : null;
echo '<img '.$default_logo_id.' alt="'. get_bloginfo('name') .'" src="' . $options['logo'] . '" />';
if(!empty($options['retina-logo'])) echo '<img id="retina-logo" alt="'. get_bloginfo('name') .'" src="' . $options['retina-logo'] . '" />';
} else { echo get_bloginfo('name'); }
?>
</a>
The url of your logo image is contained within $options['logo']. You should be able to modify this in the admin section of your WordPress installation (try looking in "Appearance -> Header").

Categories