WordPress.org

Ready to get started?Download WordPress

Forums

Frontier
[resolved] Older posts right under slider? (8 posts)

  1. mikorka
    Member
    Posted 5 months ago #

    Hi Ron,

    Is it possible to have "older" posts listed right below the slider? So that readers do not need to scroll over the same posts they already see in the slider. Let's say we have the first 5 newest post displayed in the slider and then below the slider window it starts with the 6th.

    Thanks and regards!

  2. ronangelo
    Member
    Theme Author

    Posted 5 months ago #

    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.

  3. mikorka
    Member
    Posted 5 months ago #

    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

  4. mikorka
    Member
    Posted 5 months ago #

    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?

  5. ronangelo
    Member
    Theme Author

    Posted 5 months ago #

    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 );
  6. mikorka
    Member
    Posted 5 months ago #

    hmm...that removes the 5 newest posts from the slider too...something is still missing.

  7. ronangelo
    Member
    Theme Author

    Posted 5 months ago #

    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 );
  8. mikorka
    Member
    Posted 5 months ago #

    That did the trick! Thanks.

Reply

You must log in to post.

About this Theme

About this Topic

Tags

No tags yet.