i built a short-code in wordpress to show related posts in the sidebar. Please look on the right at this website: http://www.immvestwolf.de/news/
But i get a php error (Notice: Undefined variable: count in /web/1/000/045/787/175759/htdocs/immvestwolf/wp-content/themes/le-quartier/functions.php on line 74)
i dont knwo whats wrong with this code. On other sites the code works without an php error.
Here is my php code :
function my_recent_posts_with_image() {
// Lese die letzten zehn publizierten Artikel aus
$args = array(
'posts_per_page' => 10
);
$recent_posts = get_posts( $args );
echo '<div class="widget recent_posts_with_image_by_jongo">';
echo '<h5 class="widget_title">Die 10 letzten News</h5>';
foreach ( $recent_posts as $post ) {
$count++;
?>
<div>
<a title="Veröffentlich am: <?php echo get_the_time('d.m.Y', $post->ID ) ?>" href="<?php echo get_permalink( $post->ID ); ?>"><?php echo get_the_post_thumbnail( $post->ID, array(70,50) ); ?><p><?php echo get_the_title( $post->ID ); ?></p>
</a>
</div>
<?php
}
echo '</div>';
}
add_shortcode('get_recent_posts_with_image','my_recent_posts_with_image');
The error is at the line $count++;
Any ideas to correct this?
$count
is never defined…It’s up to you to define it. For example:That said, you’re never even using the value of
$count
…so it appears as if you could remove it altogether.Another thing to note is that if you were to use a
WP_Query
instead ofget_posts()
, you would have access to a$current_post
property for this purpose, without having to manually set up a counter.