Site logo

[Resolved] Getting a Featured Post to only show on the first blog page archive

Home Forums Support [Resolved] Getting a Featured Post to only show on the first blog page archive

Home Forums Support Getting a Featured Post to only show on the first blog page archive

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2526296
    Nancy

    Hi,

    I’ve been scouring through different approaches, reading documentation and I’m just not seeing a way to have a Featured Post (it’s really the latest post – we aren’t tagging it) to only show on the first page of the Blog archive.

    I would hide it with CSS but I’m not sure how to do a range for the archive pages to set a “display:none”. But if I could target to have the featured post only show on the first page, that would be ideal if there is a means of targeting that in the display rules? Right now I have it set for “Blog” and attempted to exclude for “All Archives” which didn’t work :).

    I have the featured post as a “Hero” block.

    #2526501
    Nancy

    I got the answer to this quandry so will share it and a big thank you to Mike Oliver.

    I have the Featured Post (really the latest post) set as a hero block. On that block for “display rules” to “blog” for display. Then “Exclude” select “Paginated Results”. Be sure to clear the cache.

    For the Loop template for the archive, I have that separately as a block. On that, in the Query Loop, add the Parameter to Offset “1”.

    Once the cache clears it should be fine.

    #2526584
    Fernando
    Customer Support

    Hi Nancy,

    Glad you resolved the issue! Thank you for sharing the solution as well!

    #2530930
    Nancy

    Hi Fernando,

    I ended up (and thank you for answering the question in support) using “Inherit Query from Template” so I could have the nice SEO friendly pagination with page/2/ instead of page/2/?query-bbb96b2e-page=2 (yikes).

    But losing the offset by 1 means a repeating article. Anyway to do that another way so I can offset by 1 for the query loop after the hero area?

    Thanks,
    Nancy

    #2530958
    Fernando
    Customer Support

    You can set an offset through code.

    Try adding this PHP:

    function wpsites_exclude_latest_post( $query ) {
    	if ( ( $query->is_home() || is_archive() ) && $query->is_main_query() ) {
    		$query->set( 'offset', '1' );
    	}
    }
    
    add_action( 'pre_get_posts', 'wpsites_exclude_latest_post', 1 );

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    #2531805
    Nancy

    Fantastic – works great! Best of both worlds. Thank you Fernando!

    #2531835
    Nancy

    Well darn, I spoke to soon – it seems to repeat the query loop and doesn’t update going from page 2 on. I’ll try to convince the client to live with the extra stuff in the URL.

    #2532232
    Fernando
    Customer Support

    That’s odd. That code should just offset the query by 1.

    The ?query-string URL is the only method we can reliably allow pagination on query loops in static pages.

    The alternative would be to use a Block Element – Content Template instead. Using the code I provided to offset the query should work in this instance as well. Reference: https://docs.generatepress.com/article/block-element-content-template/

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