The slider is only meant to show posts from the selected category and not necessarily the newest ones. Maybe using a different slider plugin would be easier to accomplish what you’re after.
With the theme’s slider, what you can do is prevent posts from the slider from appearing on the main posts page so that there is no duplication.
You can add something like this on your child-theme’s functions.php
function my_query_exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() )
$query->set( 'cat', '-20' );
}
add_action( 'pre_get_posts', 'my_query_exclude_category' );
The 20
on the example above is the id/number of the category to be excluded. To get yours, go to the Categories page then hover over the selected category name. There will be a url on the status bar ( usually on the bottom part of your browser ) that contains the number.
Hi Ron,
I tried above code but if I use that nothing is displayed on the front page below the slider. If I go to the archives, all the posts are visible there. I only use one category for my posts shown on the front page so it should be simple but something is missing from the code (?)
Thanks for your help
regards
Ah, I know what you meant…okay, so let’s say I would like to exclude the 5 newest posts from the front page and only displayed in the archive. What would be the code for that?
Try this:
function my_main_query_offset( $query ) {
$offset = 5;
$ppp = get_option('posts_per_page');
if ( $query->is_paged ) {
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('offset', $page_offset );
}
else {
$query->set('offset',$offset);
}
}
add_action( 'pre_get_posts', 'my_main_query_offset' );
function my_main_query_adjust_pagination( $found_posts, $query ) {
return $found_posts - 5;
}
add_filter( 'found_posts', 'my_main_query_adjust_pagination', 10, 2 );
hmm…that removes the 5 newest posts from the slider too…something is still missing.
function my_main_query_offset( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$offset = 5;
$ppp = get_option('posts_per_page');
if ( $query->is_paged ) {
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('offset',$page_offset);
}
else {
$query->set('offset',$offset);
}
}
}
add_action( 'pre_get_posts', 'my_main_query_offset' );
function my_main_query_adjust_pagination( $found_posts, $query ) {
if ( $query->is_home() && $query->is_main_query() )
return $found_posts - 5;
else
return $found_posts;
}
add_filter( 'found_posts', 'my_main_query_adjust_pagination', 10, 2 );
That did the trick! Thanks.