【发布时间】:2019-03-08 16:07:45
【问题描述】:
我正在尝试添加一个 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
) );
?>
另外,here's the link 我的网页,这样您就可以看到实现代码时发生的情况。
有什么想法吗?非常感谢您提供的任何帮助。非常感谢!
【问题讨论】:
-
你应该使用
&&作为倒数第二个条件,否则它将拉入大于 8 OR 小于 15 的所有内容(包括最后一个条件>= 16)。试试<?php elseif ($count >= 8 && $count <= 15) : ?>否则,请提供示例数据和你得到的输出。