【问题标题】:WP Query Code to display unique div classes and WP FunctionsWP查询代码显示独特的div类和WP函数
【发布时间】: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 &gt;= 5 || $count &lt;= 7$count &gt;= 8 || $count &lt;= 15$count &gt;= 8 || $count &lt;= 15$count &gt;= 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 我的网页,这样您就可以看到实现代码时发生的情况。

有什么想法吗?非常感谢您提供的任何帮助。非常感谢!

【问题讨论】:

  • 你应该使用&amp;&amp; 作为倒数第二个条件,否则它将拉入大于 8 OR 小于 15 的所有内容(包括最后一个条件>= 16)。试试&lt;?php elseif ($count &gt;= 8 &amp;&amp; $count &lt;= 15) : ?&gt; 否则,请提供示例数据和你得到的输出。

标签: php wordpress


【解决方案1】:

您应该查看PHP Logical Operators。 || elseif ($count &gt;= 5 || $count &lt;= 7) 条件中的运算符将提取任何 >= 5 OR 任何

帖子 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# -->

【讨论】:

  • 我想说的是,您的深思熟虑和写得很好的帖子非常有帮助,我很欣赏您的所作所为。接受我的投票!
  • 感谢@mtr.web 的帮助。我已经更正了代码,但仍然存在问题。最后三个条件 $count >= 8 && $count >= 16 和 $count > 17 不起作用。它不显示第 16 条以后的帖子。似乎是什么问题?我已经根据上面的问题更新了我的代码。
  • @DanicaEscola,我编辑了答案。如果它对您有用,请不要忘记接受/投票。否则,让我知道现在发生了什么/没有发生什么,我将编辑答案以适合。
  • 我不确定为什么 $count
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多