我在wordpress中构建了一个短代码,以显示侧栏中的相关帖子。请在此网站右侧查看:http://www.immvestwolf.de/news/
但我得到一个php错误(注意:未定义的变量:在第74行的/web/1/000/045/787/175759/htdocs/immvestwolf/wp-content/themes/le-quartier/functions.php中计算)
我不知道这个代码有什么问题。在其他网站上代码工作没有PHP错误。
这是我的php代码:
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');
错误在$ count ++;
行有任何想法要纠正这个问题吗?
答案 0 :(得分:3)
$count
...由您来定义它。例如:
$count = 0;
foreach ( $recent_posts as $post ) {
那就是说,你甚至都没有使用$count
的值...所以看起来好像你可以完全删除它。
需要注意的另一点是,如果您使用WP_Query
代替get_posts()
,则可以为此目的访问$current_post
property,而无需手动设置计数器。