我正在尝试添加一个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可以使您看到实施代码时的情况。
有什么想法吗?非常感谢您提供的任何帮助。非常感谢你!
答案 0 :(得分:1)
您应该查看PHP Logical Operators。 || elseif ($count >= 5 || $count <= 7)
条件下的运算符将提取> = 5 OR 的任何值<= 7,即任何数字和所有数字。以下情况适用于8和15的情况,因此以下解决方案应该可以解决您的问题:
发布1
<?php elseif ($count == 2) : ?>
<div class="item2">
<span>Post 2</span><?php the_title(); ?><?php the_content(); ?>
</div><!-- .item# -->
<?php elseif ($count == 3) : ?>
<div class="item3">
<span>Post 3</span><?php the_title(); ?><?php the_author(); ?>
</div><!-- .item# -->
<?php elseif ($count == 4) : ?>
<div class="item4">
<span>Post 4</span><?php the_title(); ?><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count == 5) : ?>
<div class="item5">
<span>Post 5</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 5 && $count <= 7) : ?>
<div class="item6">
<span>Post 6 to 7</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 8 && $count <= 15) : ?>
<div class="item6">
<span>Post 8 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
<?php elseif ($count >= 16) : ?>
<div class="item6">
<span>Post 8 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
注意: 更好的解决方案是忽略这些比较的第二部分,因为我们已经知道,第一个比较>> 5,第二个>> 8。最简单,最干净的解决方案如下所示:
...
<?php elseif ($count <= 7) : ?>
<div class="item6">
<span>Post 6 to 7</span><?php the_title(); ?>
</div><!-- .item# -->
<?php elseif ($count <= 15) : ?>
<div class="item6">
<span>Post 8 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->
...
编辑 问题已经改变,上面的代码可能具有一定的价值,所以我保留了它,但是最后两个条件应如下所示:
从先前的条件中,我们已经知道$ count为> = 8,因此删除倒数第二个条件的那一部分将可以正常工作:
<?php elseif ($count <= 16) : ?>
<div class="item6">
<span>Post 8 to 16 </span><?php the_title(); ?>
</div><!-- .item# -->
根据先前的条件,我们已经知道$ count的所有剩余值将为> = 17,因此简单的“ else”将可以正常工作:
<?php else : ?>
<div class="item6">
<span>Post 16 onwards - </span><?php the_title(); ?><?php the_excerpt(); ?>
</div><!-- .item# -->