Wordpress在不干扰帖子连续性的情况下,在帖子之间添加单独的部分


Wordpress add separate section in between of posts without disturbing the posts continuity

我正在构建一个基于Duplex主题的基本Wordpress博客。我想对网站做一些小的定制,在两篇帖子之后,网站上有一个Instagram小部件,然后继续这些帖子。

只是为了简化页面的顺序:

  1. 2个员额
  2. Instagram小工具
  3. 从第3个帖子继续

首页以我想要的方式显示设置为帖子。

我如何在不干扰流量的情况下,在帖子之间获得Instagram小工具?

您所做的是运行循环两次以显示第一个帖子,重置它,然后在小部件之后再次启动它-跳过第一个帖子。

你可以这样实现:

首先运行循环,显示前两个帖子。

// The Query
$the_query = new WP_Query( array( 'posts_per_page' => 2 ) );
// The Loop
if ( $the_query->have_posts() ) {
    //Display post
}

重置查询,并显示您的小工具

/* Restore original Post Data */
wp_reset_postdata();
//Your widget here.

显示下一篇文章,跳过前两篇。

// The Second Query
$the_query = new WP_Query( array( 'posts_per_page' => 10, 'offset' => 2  ) );
// The Loop
if ( $the_query->have_posts() ) {
    //Display post
}

非常感谢@danjah!下面是我的代码

<?php get_header(); ?>
    <div id="content">
        <div class="posts clearfix">
            <?php if ( is_search() ) { ?>
                <h2 class="archive-title"><i class="icon-search"></i> <?php echo $wp_query->found_posts; ?> <?php _e('Results for','hyphatheme'); ?> "<?php the_search_query() ?>" </h2>
            <?php } else if ( is_tag() ) { ?>
                <h2 class="archive-title"><i class="icon-tag"></i> <?php single_tag_title(); ?></h2>
            <?php } else if ( is_day() ) { ?>
                <h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date(); ?></h2>
            <?php } else if ( is_month() ) { ?>
                <h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('F Y'); ?></h2>
            <?php } else if ( is_year() ) { ?>
                <h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('Y'); ?></h2>   
            <?php } else if ( is_category() ) { ?>
                <h2 class="archive-title"><i class="icon-list-ul"></i> <?php single_cat_title(); ?></h2>
            <?php } else if ( is_author() ) { ?>
                <h2 class="archive-title"><i class="icon-pencil"></i> <?php echo get_userdata($author)->display_name; ?></h2>
            <?php } ?>
            <?php
            if ( have_posts()) :
                while ( have_posts() ) : the_post();
                    /*
                     * Include the post format-specific template for the content. If you want to
                     * use this in a child theme, then include a file called called content-___.php
                     * (where ___ is the post format) and that will be used instead.
                     */      
                    get_template_part( 'content', get_post_format() );
                endwhile;
             else:
                /*
                 * If no posts are found in the loop, include the "No Posts Found" template.
                 */
                get_template_part( 'content', 'none' );
            endif;
            ?>
        </div><!--END .posts-->
        <?php if ( hypha_page_has_nav() ) : ?>
            <!-- post navigation -->
            <?php $post_nav_class = ( get_option( 'hypha_customizer_infinite_scroll' ) == 'enable' ) ? ' infinite' : '' ; ?>
            <div class="post-nav<?php echo $post_nav_class; ?>">
                <div class="post-nav-inside clearfix">
                    <div class="post-nav-previous">
                        <?php previous_posts_link(__('<i class="icon-arrow-left"></i> Newer Posts', 'hyphatheme')) ?>
                    </div>
                    <div class="post-nav-next">
                        <?php next_posts_link(__('Older Posts <i class="icon-arrow-right"></i>', 'hyphatheme')) ?>
                    </div>  
                </div>
            </div>
        <?php endif; ?>
        <?php if ( is_single() ) { ?>
            <!-- comments template -->
            <?php if ( 'open' == $post->comment_status ) : ?>
                <?php comments_template(); ?>
            <?php endif; ?>
        <?php } ?>

    </div><!--END #content-->

提到的一种方法是@danjah wehre在页面上创建第二个循环来获取前两篇文章。既然你是wordPress的新手,那就从头开始吧。

所有的模板工作都围绕着循环进行。循环使用页面上已定义的查询。wordPress有各种默认查询,这取决于它加载的模板(一篇文章、一个档案、一篇帖子、一个页面、索引)

  • The Loop
  • Tempalte Heirarchy

因此,a)wordpress根据您所在的路线定义查询,b)您使用循环来获取查询。但是。您可以插入查询、覆盖查询或运行其他查询。

在WP_Query 上循环

让我们在标准循环中循环。在你的index.php中,你可能有这样的东西:

 <!-- Start the Loop. -->
 <?php if ( have_posts() ) : 
     while ( have_posts() ) : 
         the_post(); ?>
     <!-- use markup or template helpers in here -->
     <?php endwhile;
  endif; ?>

虽然您可以在主循环之前运行辅助查询,插入所需内容,然后更改主循环以跳过前两个,但第二种方法是,与其创建另一个循环并更改主循环,不如保留一个计数。您可以使用增量变量(毕竟它只是一个标准的phpwhile循环)来实现这一点,但我相信您也可以从全局$wp_query对象中获得当前的post索引。

 $index = 1;
 <!-- Start the Loop. -->
 <?php if ( have_posts() ) : 
     while ( have_posts() ) : 
         the_post(); ?>
     <!-- use markup or template helpers in here -->
     <?php if ($index == 2) : ?>
       <!-- do something after the second post -->
     <?php endif; ?>
     <!-- increment the index -->
     $index++;
     <?php endwhile;
  endif; ?>

我相信你也可以用$wp_query->current_post得到索引,但我已经有一段时间没有这样做了,也没有测试过。

希望它能有所帮助!