Joseph Morgan
30 Jan 2022
Wordpress
Hello everyone, I am facing weird error in my website I can see this error
Warning: Undefined variable $post in /customers/2/5/e/mastratec.com/httpd.www/wp-content/themes/salient-child/skills-section.php on line 21
Warning: Attempt to read property "ID" on null in /customers/2/5/e/mastratec.com/httpd.www/wp-content/themes/salient-child/skills-section.php on line 21
the file looks like
<?php
$args = [
'post_type' => 'skills',
'status' => 'published',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => -1,
];
$count = 0;
$posts = new WP_Query( $args );
if( $posts->have_posts() ) :
?>
<div class="skills_container">
<?php
$count = 1 ;
while($posts->have_posts()):
$posts->the_post();
?>
<div class="post">
<div class="inner_post">
<?php if(get_the_post_thumbnail_url($post->ID)){ ?>
<img src="<?php echo get_the_post_thumbnail_url($post->ID); ?>" alt="Image">
<?php } ?>
<div class="details">
<h4><?php the_title(); ?></h4>
</div>
</div>
</div>
<?php
if($count % 6 == 0){
echo "</div> <div class='skills_container'>";
}
$count++ ;
endwhile;
else:
echo "<h3 style='text-align: center;'> No posts to show </h3>";
endif;
?>
This is the first time to see this error however that code is working fine on another server,
Please help
Hi Joseph,
This might be a error because of a different PHP version or any Wordpress update, but anyway I got the issue & there are a simple way to figure this out,
You can figure this error by replacing $post->ID with a function to get the ID of the post inside the loop which is get_the_ID()
Your loop should looks like
<?php
while($posts->have_posts()):
$posts->the_post();
?>
<div class="post">
<div class="inner_post">
<?php if(get_the_post_thumbnail_url(get_the_ID())){ ?>
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Image">
<?php } ?>
<div class="details">
<h4><?php the_title(); ?></h4>
</div>
</div>
</div>
<?php
if($count % 6 == 0){
echo "</div> <div class='skills_container'>";
}
$count++ ;
endwhile;
?>
Good luck!!
Joseph Morgan
30 Jan 2022
Thank you so much! you are amazing
© 2024 Copyrights reserved for web-brackets.com