自定义分类术语和按日期排序的最新帖子分页问题

时间:2022-02-07 作者:Darko

我正在使用与此类似的代码question, 仅当我在页面上显示所有条款时,按发布日期订购条款才有效,例如terms_to_display = 99999, 但是,如果我将其设置为6,或者例如,完整的循环顺序被打乱,它不会在下一页继续,它会再次从最新的帖子开始(减去偏移量6),并按日期显示接下来的6篇帖子,结果是在第二、第三页有新的帖子。。。页面比第一页多。

    $taxonomy = \'ctc_sermon_series\'; //taxonomy name
    $terms_to_display = 6; //number of series to display per page
    $page         = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
    $totalterms   = wp_count_terms( $taxonomy, array( \'hide_empty\' => TRUE ) ); 
    $totalpages   = ceil( $totalterms / $number );
    $posts_per_term = 1; // number of posts per term to display

    $term_count = get_terms( array (
        \'taxonomy\' => $taxonomy,
        \'fields\'   => \'count\',
     ) );

    // find out the number of pages to use in pagination
    $max_num_pages = ceil( $term_count / $terms_to_display );

    // get the page number from URL query
    $current_page = get_query_var( \'paged\', 1 );

    // calculate the offset, if there is one.
    $offset = 0; // initial
    // or changed the if not the first (0)
    if( ! 0 == $current_page) {
        $offset = ( $terms_to_display * $current_page ) - $terms_to_display;
    }


    $terms = get_terms ( array (
        \'taxonomy\'      => $taxonomy, 
        \'hide_empty\'    => true,
        \'number\'        => $terms_to_display,
        \'offset\'        => $offset,
        ) 
    );
    $sermons_items = [];

    foreach ( $terms as $term ) {

        $args = array (
            \'post_type\'         => \'ctc_sermon\',
            \'posts_per_page\'    =>  $posts_per_term,
            \'orderby\'           => \'date\',
            \'order\'             => \'DESC\',
            \'tax_query\' => array (
             array (
                \'taxonomy\' => $taxonomy,
                \'field\'    => \'id\',
                \'terms\'    => array( $term->term_id )
                ),
             ),
          );

        $sermons_query = new WP_Query( $args );

        // ignore terms with not enough posts
        if ( $sermons_query->post_count < $posts_per_term ) {
            continue;
        }

        $sermons_posts = $sermons_query->get_posts();

        // get date of newest post in this term
        $newest_post_date = get_the_date( "U", $sermons_posts[0]->ID );

        $sermons_items[$newest_post_date] = array(
            \'term\' => $term,
            \'sermons\' => $sermons_posts,
        );
    }

    krsort( $sermons_items ); // sort descending by keys

    $sermons_items = array_slice( $sermons_items, 0, $terms_to_display );


    global $post;

    echo \'<section class="grid-posts">\';

    foreach ( $sermons_items as $item ) {

        echo \'<div class="col-md-6 no-padding grid-post">\'; 

        foreach ( $item[\'sermons\'] as $post ) {
            setup_postdata( $post ); ?>

            <div class="grid-post-content">
                <a href="<?php echo esc_url( home_url( \'/sermon-series/\' ) ).$item[\'term\']->slug; ?>">
                    <h2 class="entry-title"><?php echo $item[\'term\']->name; ?></h2>
                </a>
                <?php
                    echo get_the_title().\'</br>\';
                    echo get_the_date(); 
                ?>
            </div>

            <?php
        }

        echo \'</div>\';  

    }

    echo \'</section>\';


    echo paginate_links( array (
        \'total\'   => $max_num_pages,
        \'current\' => $current_page,
    ) );

1 个回复
SO网友:tiago calado

要更改每页的帖子数,可以在wp仪表板中执行,如下所示picture

此函数用于函数。php适合我

function tcPostsPerPage( $query ) {
  if (is_home()){$query->set(\'posts_per_page\', 6);}
  if (is_archive()){$query->set(\'posts_per_page\', 6);}
}
add_action( \'pre_get_posts\', \'tcPostsPerPage\' );
这两个6限制为每页6个,如果这对您不起作用,那么将is\\u archive()更改为类似于is\\u page(“yourpage”)的内容是一个问题

此外,如果可行,您可以删除数组的“posts\\u per\\u page”和附加的变量。

相关推荐

‘orderby’=>‘rand’替代方案以获得更好的性能?

我被告知,在阵列中使用这个:\'orderby\' => \'rand\' 将导致:随机排序的查询代价非常高昂,需要创建临时数据库表和扫描,因为它必须复制entireposts表,然后随机重新排序帖子,最后在销毁新表之前对新表进行实际查询。我被告知:在一个随机的日期之后,要求第一篇帖子要容易得多。有人能详细说明一下吗?是否有执行上述操作的数组代码行?如果您有任何与此相关的资源,我们将不胜感激。