【问题标题】:Wordpress only first post different designWordpress 仅第一次发布不同的设计
【发布时间】:2017-04-04 19:50:03
【问题描述】:

我有一个博客(离线制作),我做了一个 wordpress 循环,它检查第一篇文章是否使用不同的风格,其他文章使用不同的风格。

问题是:它工作正常,但是当我转到下一页时(2)它再次显示,我只想申请第一个最新帖子。不适用于每页上的第一篇文章。

代码:

    <?php 
         while ( have_posts() ) : the_post();?>
             <?php $count++; ?>
                   <?php if ($count == 1 && is_home()) : 

                      //First post different style goes here

                   <?php else : ?>

                      //Rest Other post different style goes here 

            <?php endif; ?>
          <?php
       endwhile;
    ?>

<?php else : ?>

     <article id="post-0" class="post no-results not-found">
           <header class="entry-header">
               <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
           </header><!-- .entry-header -->

           <div class="page_content">
                <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
                <?php get_search_form(); ?>
           </div><!-- .entry-content -->
      </article><!-- #post-0 -->

<?php endif; ?>

【问题讨论】:

    标签: html wordpress loops post content-management-system


    【解决方案1】:

    您应该使用get_query_var('paged')。它将返回当前页码,从 0 开始。

    所以你应该添加

    <?php if ($count == 1 && is_home() && get_query_var('paged') == 0 ) : ?>
        //First post different style goes here
    <?php else : ?>
        //Rest Other post different style goes here 
    <?php endif; ?>
    

    将此添加到您的 function.php 以更改帖子数量

    add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
    function custom_pre_get_posts($query) {
        if ( is_home() && get_query_var( 'paged' ) > 0 ) {
            $query->set('posts_per_page', 12);
        }
    
        return $query;
    }
    

    【讨论】:

    • Thanx Mate 它工作了@Stanimir Stoyanov,另外我如何平衡每个页面的帖子数量,截至目前,它从 wordpress 阅读部分设置为 13。当我转到第二页时,帖子的数量不准确,最后缺少一个。
    • 据我了解,您希望每个博客页面的帖子数量相同,对吗?您应该创建自定义查询,跳过在阅读设置中设置的 posts_per_page。
    • 是的,没错,在主页上第一个帖子是滑块,帖子总数为 13,而在第二页,其简单的基本 3 个网格帖子。对于第二页,我们跳过滑块,因此第二页上有额外的一篇文章。例如:构建这种风格的帖子templated.co检查第一页和第二页
    • 你能帮我解决这个问题吗?
    • 你应该把第二页、第三页等页面的帖子数改为12,对吧?我在我的评论中添加了额外的代码,你可以使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2013-08-23
    • 2020-09-16
    • 2022-07-22
    相关资源
    最近更新 更多