我正在尝试添加一个 WP 查询代码,它将列出我的 wordpress 博客中的所有帖子。
此代码将位于我在 wordpress 博客中创建的页面下的自定义模板中。
这个 wp 查询代码的目的是在一个唯一的 div 类上显示所有帖子,并具有不同的 html/php 结构。例如,帖子#1 将显示标题和摘录,而帖子#2 将显示标题和内容等。
以下是上述代码:
<?php /*** Template Name: Custom Page - Blog */ get_header(); ?>
<!-- START of WP Query -->
<?php $the_query = new WP_Query( array("post_type"=>'post')); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="item1">
<span>Post 1 </span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 2) : ?>
<div class="item2">
<span>Post 2 </span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 3) : ?>
<div class="item3">
<span>Post 3 </span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 4) : ?>
<div class="item4">
<span>Post 4 </span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 5) : ?>
<div class="item5">
<span>Post 5</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count <= 7) : ?>
<div class="item6">
<span>Post 6 to 7 </span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 8 && $count <= 16) : ?>
<div class="item6">
<span>Post 8 to 15 </span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 17) : ?>
<div class="item6">
<span>Post 16 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
<?php else : ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<!-- END of WP Query -->
上面代码的问题是它没有正确显示我想要的内容。它显示帖子 #1 到 5 ,但之后它不遵循条件$count >= 5 || $count <= 7
、$count >= 8 || $count <= 15
和。$count >= 8 || $count <= 15
$count >= 16
编号分页的代码也不起作用。它不显示任何内容:
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
另外,这是我网页的链接,因此您可以查看代码实施时发生的情况。
有任何想法吗?非常感谢您提供的任何帮助。非常感谢!