Я хочу отобразить порядковый номер в цикле WordPress на боковой панели по категории «Программное обеспечение», но номер в последовательности не отображается, я не знаю, как его отобразить.
я хочу показать номер, как это
это мой код в sidebar.php
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'category_name' => 'Software'
);
$myposts = new WP_Query($args);
// the loop
if ($myposts->have_posts()):
while ($myposts->have_posts()):
$myposts->the_post();
// display article
get_template_part('template-parts/sidebar/latest-content-2', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
и это мой код в content.php
<!-- POST PREVIEW -->
<div class = "post-preview tiny padded gaming-news">
<!-- POST PREVIEW IMG WRAP -->
<a href = "<?php the_permalink(); ?>">
<div class = "post-preview-img-wrap">
<!-- POST PREVIEW IMG -->
<figure class = "post-preview-img liquid">
<?php the_post_thumbnail(); ?>
</figure>
<!-- /POST PREVIEW IMG -->
</div>
</a>
<!-- /POST PREVIEW IMG WRAP -->
<!-- BUBBLE ORNAMENT -->
<div class = "bubble-ornament small black no-link">
<p class = "bubble-ornament-info">01</p>
</div>
<!-- /BUBBLE ORNAMENT -->
<!-- POST PREVIEW TITLE -->
<a href = "<?php the_permalink(); ?>" class = "post-preview-title"><?php the_title(); ?></a>
<!-- POST AUTHOR INFO -->
<div class = "post-author-info-wrap">
<p class = "post-author-info small light">By <a
href = "<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"
class = "post-author"><?php the_author(); ?></a><span
class = "separator">|</span><?php the_time( 'F j, Y' ); ?></p>
</div>
<!-- /POST AUTHOR INFO -->
</div>
<!-- /POST PREVIEW -->
Добро пожаловать в Stack Overflow! Это элемент bubble-ornament
?
Вы можете передавать аргументы в get_template_part()
, поэтому все, что вам нужно сделать, это передать значение итератора $i
:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'category_name' => 'Software'
);
$myposts = new WP_Query($args);
// the loop
if ($myposts->have_posts()):
// Start the iterator here. Start at one so the first post has "1"
$i = 1;
while ($myposts->have_posts()):
$myposts->the_post();
// display article. Pass the iterator into the template part.
get_template_part('template-parts/sidebar/latest-content-2', get_post_format(), ['post_number' => $i ]);
// Increase the value of $i.
$i++;
endwhile;
endif;
wp_reset_postdata();
?>
Затем здесь вы получаете аргументы, которые были переданы в часть шаблона, а затем повторяете их в своем элементе bubble-ornament
.
<?php
// Get the value of the iterator
$post_number = $args['post_number'];
?>
<!-- POST PREVIEW -->
<div class = "post-preview tiny padded gaming-news">
<!-- POST PREVIEW IMG WRAP -->
<a href = "<?php the_permalink(); ?>">
<div class = "post-preview-img-wrap">
<!-- POST PREVIEW IMG -->
<figure class = "post-preview-img liquid">
<?php the_post_thumbnail(); ?>
</figure>
<!-- /POST PREVIEW IMG -->
</div>
</a>
<!-- /POST PREVIEW IMG WRAP -->
<!-- BUBBLE ORNAMENT -->
<div class = "bubble-ornament small black no-link">
<!-- Echo out the post number here from the iterator. Add the "0" before the number -->
<p class = "bubble-ornament-info"><?php echo '0' . $post_number; ?></p>
</div>
<!-- /BUBBLE ORNAMENT -->
<!-- POST PREVIEW TITLE -->
<a href = "<?php the_permalink(); ?>" class = "post-preview-title"><?php the_title(); ?></a>
<!-- POST AUTHOR INFO -->
<div class = "post-author-info-wrap">
<p class = "post-author-info small light">By <a
href = "<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"
class = "post-author"><?php the_author(); ?></a><span
class = "separator">|</span><?php the_time( 'F j, Y' ); ?></p>
</div>
<!-- /POST AUTHOR INFO -->
</div>
<!-- /POST PREVIEW -->
Вы захотите посмотреть на использование
$myposts->current_post
developer.wordpress.org/reference/classes/wp_query