使用$wp_Query->CURRENT_POST在分页页面上从零重新开始注入内容。如何在X个帖子之后插入内容,而不考虑分页?

时间:2018-08-23 作者:Garconis

我正在创建一个输出自定义循环的短代码。在循环内while, 我在特定的帖子计数后注入了某些内容。通过检查current_post 数字这个特定的函数相当长(并且充满了测试中注释掉的代码)。

这里是我当前版本的链接(当前所有帖子都在一个页面上):https://gist.github.com/Garconis/c93ce89c3d378bfb7f5d8df97acbebc2

如果/当所有帖子(posts_per_page => -1) 正在显示,但只要我添加分页(例如,设置posts_per_page => 5, 注入的内容从每个新页面上的计数0开始。

How can I update this, to be able to inject my specific content after X posts, without the count starting over with each new page?

我所拥有的赤裸裸的骨骼:

<?php
// create shortcode to list all Featured Itineraries
add_shortcode( \'fs_burn_homepage_loop\',\'fs_all_featured_itineraries_shortcode\' );
function fs_all_featured_itineraries_shortcode( $atts ) {
ob_start();
// Query posts from the database.
$options = array(
    // Arguments for your query.
    \'post_type\'         => array(\'post\', \'ajde_events\'),
    \'posts_per_page\'    => 5,
    \'paged\'             => 1,
);

/* To get the Easy Load More plugin to work */
if ( get_query_var( \'paged\' ) ) { 
    $options[\'paged\'] = get_query_var( \'paged\' ); 
} 
elseif ( get_query_var( \'page\' ) ) { 
    $options[\'paged\'] = get_query_var( \'page\' ); 
} else { 
    $options[\'paged\'] = 1; 
}

$query = new WP_Query( $options );
// Check that we have query results
if ( $query->have_posts() ) {

    // we have posts, so lets wrap them
    echo\'<div id="fs-homepage-loop" class="fs-all-posts">\';

        // Start looping over the query results.
        while ( $query->have_posts() ) {

            // Set up post data.
            $query->the_post(); 

            echo \'<article id="post-\'. get_the_ID() .\'" class="fs-post-article \'. join( \' \', get_post_class() ) .\'">
                <div class="fs-post-content-wrapper">
                    <div class="fs-post-name">
                        <h2 class="entry-title"><a href="\'. get_permalink(); .\'">\'. get_the_title() .\'</a></h2>
                    </div>
                </div>
            </article>\';

            // inject stuff after X posts
            if( ($query->current_post == 3) ) {
                echo \'<article class="fs-post-article">
                    <div class="fs-post-content-wrapper">
                        content after post 4 is here
                    </div>
                </article>\';
            }
            if( ($query->current_post == 9) ) {
                echo \'<article class="fs-post-article">
                    <div class="fs-post-content-wrapper">
                        content after post 10 is here
                    </div>
                </article>\';
            }

        // close the loop WHILE
        }

        wp_reset_postdata();

    // close post wrapper
    echo \'</div>\';

    //function for the Easy Load More plugin
    load_more_button();

    $myvariable = ob_get_clean();

    return $myvariable;

// close the query IF
}
// ELSE it cant find ANY posts that match
else {
    echo \'<div class="fs-no-posts-found"><h4>We currently have no stories to show you.</h4></div>\';
// close the query ELSE */
}
}
Would it be as simple as somehow checking the current_post count AND the page number of the pagination? 因为我应该知道每页会显示多少帖子。。。

1 个回复
SO网友:Garconis

作为记录,我刚刚找到了一种新的/不同的方法来实现这个目标。这并不完全相同,但它确实允许我在原始循环中的每X个帖子之后插入一个新的帖子类型。

这是来源:https://www.gowp.com/blog/inserting-custom-posts-loop/

该方法是通过下面的代码完成的,它本质上是将一个新的自定义post类型注入到循环中,无论分页如何,它似乎都能工作。

add_action( \'wp\', \'fs_only_do_on_homepage\' );
function fs_only_do_on_homepage() {

if( is_front_page() ) {

    /* change the query to inject ads after every X frequency */
    // https://www.gowp.com/blog/inserting-custom-posts-loop/

    add_filter( \'the_posts\', \'fs_insert_ads\', 10, 2 );
    function fs_insert_ads( $posts, $query ) {

        // We don\'t want our filter to affect post lists in the admin dashboard
        if ( is_admin() ) return $posts;

        // We also only want our filter to affect the main query
        if ( ! is_main_query() ) return $posts;

        // Set a variable for the frequency of the insertion which we\'ll use in some math later on
        $freq = 2;

        /* Retrieve the ads, and calculate the posts_per_page on the fly, 
         * based on the value from the original/main query AND our frequency
         * this helps ensure that the inserted posts stay in sync regardless of pagination settings
         */
        $args = array(
            \'post_type\'         => \'fs_ads\',
            \'posts_per_page\'    => floor( $query->query_vars[\'posts_per_page\'] / $freq ),
            \'paged\'             => ( $query->query_vars[\'paged\'] ) ? $query->query_vars[\'paged\'] : 1,
            \'orderby\'           => \'menu_order\',
            \'order\'             => \'ASC\',
            \'meta_key\'          => \'status\',
            \'meta_value\'        => \'enable\',
        );

        // inserts the ads into the $posts array, using array_slice() ... and do some math to ensure they are inserted correctly
        if ( $fs_ads = get_posts( $args ) ) {
            $insert = -1;
            foreach ( $fs_ads as $fs_ad ) {
                $insert =  $insert + ( $freq + 1 );
                array_splice( $posts, $insert, 0, array( $fs_ad ) );
            }
        }

        return $posts;
    } // end the fs_insert_ads function

} // end checking is_home

} // end the fs_only_do_on_homepage function
我确实无法说出哪个项目在哪个帖子之间,但它确实实现了我最初的目标,即在每X个帖子之后,尝试将自定义帖子类型注入到我的循环中。

结束

相关推荐

Loop by category and meta_key

我正在尝试为category.php 其中它基于当前类别和元键。但循环坚持显示其他类别的帖子global $taxonomy, $term; $cats = get_the_category(); $newQuery = new WP_Query( array( \'cat\' => $cats[0]->term_id, \'meta_key\' => \'p