dooo61733 2016-06-01 12:31
浏览 22
已采纳

如何在wordpress php中定义count ++?

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&ouml;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?

  • 写回答

1条回答 默认 最新

  • doutan3371 2016-06-01 12:33
    关注

    $count is never defined...It's up to you to define it. For example:

    $count = 0;
    foreach ( $recent_posts as $post ) {
    

    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 of get_posts(), you would have access to a $current_post property for this purpose, without having to manually set up a counter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程