On my category pages I am displaying each post as an li. I would like to assign each li an ID starting from 1 and counting up incrementally (i.e. if a category has 10 posts, the output is an ul with each li having an id of a number going 1 through 10. How would i achieve this?
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 -->
And 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>
Thanks in advance!
Before your loop add:
<? $counter = 0;?>
Then inside the loop before the li add:
<? $counter++;?>
and change the li to:
<li class="slide" id="post<?= $counter;?>">
The get_footer();
Method is placed wrong.
Move it from content-category.php to the end of the category.php