Reputation: 9
I currently have a page that displays only 1 post because I wanted to have 1 post per day and that show up on the homage.
I have decided to have multiple posts in a day but I only want the homepage to display posts from the current day only.
What in my loop could I change to accomplish this?
My site is http://thatshitsawesome.com for reference.
Upvotes: 0
Views: 849
Reputation: 4976
Firstly you must increase the number of visible posts shown at most. I assume you know how to do this already as you've managed to limit it to one per query. For completion you can change it using the posts_per_page
condition in the query arguments or in the "Blog pages show at most" value set under settings in the admin panel if you want to use the default value.
To limit posts to the current day use some specific Time parameters as defined in the WP_Query reference. You need the conditions year
, monthnum
and day
.
Example:
<?php
// Limit query posts to the current day
$args = array(
'year' => (int) date( 'Y' ),
'monthnum' => (int) date( 'n' ),
'day' => (int) date( 'j' ),
);
$query = new WP_Query( $args );
// The Loop
while ( $query->have_posts() ) :
$query->the_post();
// ...
endwhile;
?>
If you're not using an explicit query but rely on the internal WP Query a common way to change the internal query is using the pre_get_posts
action. Add the function below to your functions.php file to only show posts from the current day and only on the frontpage.
Example:
<?php
function limit_posts_to_current_day( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'year', (int) date( 'Y' ) );
$query->set( 'monthnum', (int) date( 'n' ) );
$query->set( 'day', (int) date( 'j' ) );
}
}
add_action( 'pre_get_posts', 'limit_posts_to_current_day' );
?>
Upvotes: 1