- This topic has 24 replies, 4 voices, and was last updated 2 years, 2 months ago by Elvin.
-
AuthorPosts
-
December 12, 2019 at 6:38 am #1102090Mike
Hi
Is there a way to stick a post excerpt to the top of a category archive page?Thanks
December 12, 2019 at 7:09 am #1102236DavidStaffCustomer SupportHi there,
how would you select or decide which excerpt is to be displayed?
e.g the most recent post in the category.December 12, 2019 at 7:37 am #1102264MikeI have two nav bar choices on a niche site which are ….Reviews and Information. These menu selections each open a category archive page.
I would like to stick a post to the top of the Reviews category page, so that every time that page opens you see the same review post excerpt at the top of the page with the recent (category review) post following it.
I’m sure it’s a hair brain idea, but it seems like I’ve been able to do it in the past but can’t make it happen now?
Anyway, that’s what I would like to do, and whether I can or not isn’t a life or death deal. And I really can’t tell you how great it is to be able to ask you?
Thank you
Mike MahaffeyDecember 12, 2019 at 8:01 am #1102286DavidStaffCustomer SupportHmmm… had a look at the WP Repo for Stick Posts on Category and all of them are out of date, unsupported or no longer available. Which makes it a more tricky affair.
Theres a few steps involved:
1. How to ‘select’ the Sticky Featured Post. I would suggest using a taxonomy Tag for that.
2. Then we can use that Tag to remove the post from the Category Loop.
3. And then we could use WP Show Posts and a Hook Element to use the same Tag to display just that post.That work for you? If so i can provide the steps to do that.
December 12, 2019 at 8:17 am #1102302MikeThanks David
I hate I didn’t mention that I had already been through the wp plugins.Yes, if you could (at your convenience) get me started with the steps you mentioned I would appreciate it.
Thanks
Mike MahaffeyDecember 12, 2019 at 8:32 am #1102323DavidStaffCustomer SupportOK
1. Add a new Tag to your featured post. Name it what you like eg. Featured Review.
2. Add this PHP Snippet to remove the Tag post from the category loop:add_filter('pre_get_posts', 'excludeTag'); function excludeTag($query) { if ( $query->is_category('category-slug') ) { $query->set('tag', '-1'); } return $query; }
Replace
category-slug
with the slug of the category eg.reviews
Replace the-1
in$query->set('tag', '-1');
with the ID of the Tag, you can find that by editing the tag and checking the URL, eg. if the ID=65 then the code will be:$query->set('tag', '-65')
If that works ie. that post is no longer displayed in the loop then:
3. Install the WP Show Posts Plugin: https://wordpress.org/plugins/wp-show-posts/
3.1 Dashboard > WP Show Posts > New. Configure your new list with Taxonomy filter to display just your new Tag and set it to display 1 post and do all the other settings you wish to display.
3.2 Whilst editing the list it provides you with a shortcode.4. Now you can add the Shortcode using a Hook Element:
https://docs.generatepress.com/article/hooks-element-overview/September 23, 2021 at 3:59 pm #1940946BenjaminThis is probably a dumb question on two counts, but must I be using GeneratePress as my theme? I do use WP Show Posts with Genesis. next, I tried modifying the above code to a custom taxonomy I’ve created called ‘college’ and have a tag called Seaver Spotlight (ID #2390). But no luck. I get a duplicate: https://newsmigstg.wpengine.com/college/cse/
add_filter(‘pre_get_posts’, ‘excludeTag’);
function excludeTag($query) {
if ( $query->is_college (‘cse’) ) {
$query->set(‘tag’, ‘2390’);
}
return $query;
}September 24, 2021 at 7:02 am #1941491DavidStaffCustomer SupportHi there,
unlike this forum the pre_get_posts filter is not Theme specific 🙂
For a custom taxonomy you would need to change this condition:
if ( $query->is_category('category-slug') ) {
to:
if ( $query->is_tax('taxonomy','term') ) {
See her fore more info on is_tax:
September 24, 2021 at 12:05 pm #1941937BenjaminThat got me pointed in the right direction, thanks a million! This is where I ended up, which is just about where I want to be: https://newsmigstg.wpengine.com/college/cse/
The function I used is:
function exclude_posts( $query ) { if ( $query->is_tax('college','cse') ) { $query->set( 'tag__not_in', array( 2390 ) ); } } add_action( 'pre_get_posts', 'exclude_posts' );
An unintended consequence is that this also prevents the post from appearing in the corresponding RSS feed. Any advice on preventing that?
September 25, 2021 at 10:14 am #1942742DavidStaffCustomer SupportTry this:
function exclude_posts( $query ) { if ( is_feed() ) { return $query; } if ( $query->is_tax('college','cse') ) { $query->set( 'tag__not_in', array( 2390 ) ); } return $query; } add_action( 'pre_get_posts', 'exclude_posts' );
This should return no changes if its not the main_query
September 30, 2021 at 9:20 am #1948430BenjaminThanks a million. The excluded item, unfortunately, is AWOl in the feed: http://newsmigstg.wpengine.com/college/cse/feed/
October 1, 2021 at 1:50 am #1949040DavidStaffCustomer SupportTry the updated code above:
https://generatepress.com/forums/topic/archive-sticky-post/#post-1942742October 5, 2021 at 10:45 am #1953759BenjaminNice!!!
October 5, 2021 at 3:47 pm #1954006DavidStaffCustomer SupportGlad to be of help
October 28, 2021 at 4:46 pm #1981450BenjaminApologies, a follow-up! If I wanted to achieve this same functionality on a standard tag archive page such as this: https://newsmigstg.wpengine.com/tag/lmutw/ would the code be:
function exclude_posts( $query ) { if ( is_feed() ) { return $query; } if ( $query->set( 'tag__not_in', array( 2414 ) ); { return $query; } add_action( 'pre_get_posts', 'exclude_posts' );
-
AuthorPosts
- You must be logged in to reply to this topic.