【问题标题】:Count posts generated by loop wordpress计算循环 wordpress 生成的帖子
【发布时间】:2016-06-24 10:32:12
【问题描述】:

在我的类别页面上,我将每个帖子显示为一个 li。我想为每个 li 分配一个从 1 开始并递增计数的 ID(即,如果一个类别有 10 个帖子,则输出是一个 ul,每个 li 都有一个从 1 到 10 的数字的 id。我将如何实现这一点?

category.php:

<?php
/**
 * The template for displaying category pages.
 **/

get_header(); 
$counter = 0;?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

        <?php
                if ( have_posts() ) : ?>
                    <header class="page-header">
                <?php
                    the_archive_title( '<h1 class="page-title">', '</h1>' );
                    the_archive_description( '<div class="taxonomy-description">', '</div>' );
                ?>
                    </header><!-- .page-header -->
                        <div class="slider-cont">
                        <div class="slider-nav">
                            <i class="fa fa-chevron-left"></i>
                        </div>
                        <div id="slider">
                        <ul class="slides">

            <?php
            /* Start the Loop */

            while ( have_posts() ) : the_post();
                        $counter++;

                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( 'template-parts/content-category', get_post_format() );

            endwhile;?>
                        </ul>
                        </div>
        <div class="slider-nav next">
            <i class="fa fa-chevron-right"></i>
        </div>
            <?php the_posts_navigation();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif; ?>
                    </div><!-- .slider-cont -->
        </main><!-- #main -->
    </div><!-- #primary -->

还有 content-category.php:

<?php
get_footer();

<li class="slide" id="post<?php $counter; ?>">
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">
                    <h2><?php echo the_title();
                    if ( 'post' === get_post_type() ) : ?></h2>
                    <?php
                    endif; ?>
            </header><!-- .entry-header -->

            <div class="entry-content">
                    <?php echo the_excerpt();?>
                     <a href="<?php echo the_permalink();?>">Read More.</a>
            </div><!-- .entry-content -->
    </article><!-- #post-## -->
</li>

提前致谢!

【问题讨论】:

    标签: php jquery wordpress loops foreach


    【解决方案1】:

    get_footer();方法放错了。

    将其从 content-category.php 移动到 category.php

    的末尾

    【讨论】:

      【解决方案2】:

      在循环之前添加:

      <? $counter = 0;?>
      

      然后在li之前的循环里面添加:

      <? $counter++;?>
      

      并将 li 更改为:

      <li class="slide" id="post<?= $counter;?>">
      

      【讨论】:

      • 这似乎为所有人生成了"post1" 的 ID。
      • 你能把你正在使用的全部内容发布出来吗?很可能第一块放置在错误的位置(即在循环内而不是在循环外)。
      • 我编辑了这个部分的全部代码
      • 在刚才删除 $counter = 0 :while ( have_posts() ) : the_post();此外,您需要使用 =$counter;?> 回显 $counter
      • 产生了&lt;li class="slide" id=""&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2012-10-31
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多