Site logo

[Resolved] Limit Parent Category Archive to 10 excerpts

Home Forums Support [Resolved] Limit Parent Category Archive to 10 excerpts

Home Forums Support Limit Parent Category Archive to 10 excerpts

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2044136
    Anne

    I have a category on my site called Tutorials. Underneath it, are various sub-categories, Excel, Google, etc.

    Tutorials
    – Excel
    – Google
    – Email
    – Etc

    Instead of showing all the tutorials on the Tutorials archive page, is there a way to limit it to just 10? The subcategory pages would show the full amount. I’ve got a custom field called “Priority” that I use for sorting. This way, I could end up with my parent Tutorials page being a “Top 10 Tutorials” page.

    Thanks in advance.

    #2044167
    Ying
    Staff
    Customer Support

    Hi Anne,

    Try this PHP snippet:

    add_filter('pre_get_posts', 'posts_in_category');
    
    function posts_in_category($query){
        if ($query->is_category()) {
            if (is_category('Tutorials')) {
                $query->set('posts_per_archive_page', 10);
            } 
        }
    }

    Let me know 🙂

    #2044196
    Anne

    Hi Ying,

    thanks for the prompt suggestion. Your code seems to make sense to me so I think we’re close although I’m not a coder.

    I pasted your code to the Code Snippets plugin. I use it instead of adding code directly to functions.php. However, after adding, I’m still getting more than 10 items under Tutorials.

    I did change to lower case “tutorials” as that is the slug name. Upper case didn’t work either. And I cleared the cache. I also verified the sub-categories didn’t stop at 10.

    And to be safe, I also deactivated the plugin that did sorting on the Priority code. It didn’t make a difference.

    Did I miss something?

    #2044238
    Elvin
    Staff
    Customer Support

    Hi Anne,

    Let’s try modifying the code a bit.

    try this one:

    add_action( 'pre_get_posts', 'posts_in_category' );
    function posts_in_category( $query ) {
    	if( $query->is_main_query() && is_category( 'tutorial' ) && ! is_admin() ) {
    		$query->set( 'posts_per_page', '10' );
    	}
    }

    You then check the page in incognito mode. 😀

    #2044993
    Anne

    Hi Elvin,

    thanks, but still no luck. And yes, I cleared the cache and reviewed it in an Incognito window. In both snippets, it seems to be returning what I want, but the page navigation still exists. If I click Page 2, the page refreshes but not the URL. I’m looking at the same top 10. However, if I click the icon for Page 9, it does go to /tutorials/page/9.

    I’m thinking I may just use CSS to hide the other pages with Simple CSS. It works on my staging site. I just need to think through to see if there are any unintended consequences.

    /* Remove bottom nav on Tutorials page */
    .category-tutorials #nav-below {
    display: none;
    }

    #2046893
    Elvin
    Staff
    Customer Support

    To clarify: Was the goal to display only 10 post and remove the page navigation regardless of how many post exists under a category?

    If that’s the case then your CSS would be fine.

    #2046896
    Anne

    Yes, the goal was to show only 10 entries so no page navigation would show.

    #2046898
    Elvin
    Staff
    Customer Support

    Try this:

    add_action( 'pre_get_posts', 'posts_in_category' );
    function posts_in_category( $query ) {
    	if( $query->is_main_query() && is_category( 'tutorial' ) && ! is_admin() ) {
    		$query->set( 'posts_per_page', '10' );
    		$query->set( 'no_found_rows', true );
    	}
    }
    #2046899
    Anne

    Thanks. That works. I no longer have to use CSS to hide pagination.

    #2046953
    Elvin
    Staff
    Customer Support

    No problem. 😀

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.