Site logo

Archived topic

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

7 replies · Started by Nancy on February 8, 2023

Viewing posts 1–8 of 8

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.

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.

Hi Nancy,

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

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

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

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

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.

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/

This archived topic is closed to new replies.