Php 对循环wordpress生成的帖子进行计数

Php 对循环wordpress生成的帖子进行计数,php,jquery,wordpress,loops,foreach,Php,Jquery,Wordpress,Loops,Foreach,在我的分类页面上,我将每个帖子都显示为li。我想给每个li分配一个ID,从1开始递增(即,如果一个类别有10个帖子,则输出是一个ul,每个li的ID从1到10。我如何实现这一点 category.php: <?php /** * The template for displaying category pages. **/ get_header(); $counter = 0;?> <div id="primary" class="content-area"&

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

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 -->
<?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>

在循环之前添加:

<? $counter = 0;?>

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

<? $counter++;?>

并将li更改为:

<li class="slide" id="post<?= $counter;?>">
get_footer();
方法放置错误


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

的末尾,这似乎会为所有这些文件生成一个ID为
“post1”
。您可以发布您所使用的全部内容吗?第一个文件的位置可能不正确(即,在循环内而不是在循环外)。我编辑此部分以向您展示我的全部代码Drop$counter=0在:while(have_posts()):the_post();此外,您还需要使用生成的$counter回声$counter