最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - List ALL posts within last week from current category on category page

programmeradmin0浏览0评论

My category template shows 5 (full content) posts per page and I want to add a list of ALL the posts during the past week for that category to the top of the page as a quick overview. So the first post in the list will be same as first full-content post below the list, etc. except the overview list can have more than 5 posts.

I found this similar answer but it would only show 5 items at a time too, since that is my default posts per page setting.

I also tried using this code, but it shows posts from all categories. I only want to include the current category.

<?php

$args = array(
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
    'date_query'     => array(
                            array(
                            'after' => '1 week ago'
                            )
                        )
);
$my_query = new WP_Query( $args );

if ( $my_query->have_posts() ) {

    echo '<ul>';

    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        echo '<li><a href="' . get_permalink( $post -> ID ) . '">' . get_the_title() . '</a></li>';

    }

    echo '</ul>';

}
wp_reset_postdata();

?>

My category template shows 5 (full content) posts per page and I want to add a list of ALL the posts during the past week for that category to the top of the page as a quick overview. So the first post in the list will be same as first full-content post below the list, etc. except the overview list can have more than 5 posts.

I found this similar answer but it would only show 5 items at a time too, since that is my default posts per page setting.

I also tried using this code, but it shows posts from all categories. I only want to include the current category.

<?php

$args = array(
    'post_type'      => 'post',
    'post_status'    => 'publish',
    'orderby'        => 'date',
    'order'          => 'DESC',
    'date_query'     => array(
                            array(
                            'after' => '1 week ago'
                            )
                        )
);
$my_query = new WP_Query( $args );

if ( $my_query->have_posts() ) {

    echo '<ul>';

    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        echo '<li><a href="' . get_permalink( $post -> ID ) . '">' . get_the_title() . '</a></li>';

    }

    echo '</ul>';

}
wp_reset_postdata();

?>
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Oct 7, 2014 at 17:37 draneydraney 152 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If I'm understanding you correctly you just need two more arguments in your $args array.

Firstly, category_name - see WP_Query - Category Parameter. We get the value from the $wp_query global via get_query_var(), so like this:

'category_name' => get_query_var( 'category_name' )

Secondly, for the number of posts to show there is posts_per_page as it is an archive one could actually use posts_per_archive_page - see WP_Query - Pagination Parameters.

So do either:

'posts_per_page' => -1

Or do:

'posts_per_archive_page' => -1
发布评论

评论列表(0)

  1. 暂无评论