The effect I want is to sticky articles in categories but not on the homepage. I found methods to pin articles in categories online, but none of them solved the problem of not sticky articles on the homepage. They all talked about changing the homepage to sticky. So I thought of a way. The first step is to pin the categories to the top, and the second step is to prevent the sticky articles from being displayed on the homepage. That’s how it’s solved. It’s just a temporary need at the moment, and I don’t need a very powerful function, so I’m just using it.
Step 1
Add the following code to the functions.php file of the theme:
add_filter(‘the_posts’, ‘putStickyOnTop’ );
function putStickyOnTop( $posts ) {
if(is_home() || !is_main_query() || !is_archive())
return $posts;
global $wp_query;
$sticky_posts = get_option(‘sticky_posts’);
if ( $wp_query->query_vars[‘paged’] <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !get_query_var(‘ignore_sticky_posts’) ) { $stickies1 = get_posts( array( ‘post__in’ => $sticky_posts ) );
foreach ( $stickies1 as $sticky_post1 ) {
if($wp_query->is_category == 1 && !has_category($wp_query->query_vars[‘cat’], $sticky_post1->ID)) {
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_tag == 1 && has_tag($wp_query->query_vars[‘tag’], $sticky_post1->ID)) {
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_year == 1 && date_i18n(‘Y’, strtotime($sticky_post1->post_date))!=$wp_query->query[‘m’]) {
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_month == 1 && date_i18n(‘Ym’, strtotime($sticky_post1->post_date))!=$wp_query->query[‘m’]) {
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_day == 1 && date_i18n(‘Ymd’, strtotime($sticky_post1->post_date))!=$wp_query->query[‘m’]) {
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
if($wp_query->is_author == 1 && $sticky_post1->post_author != $wp_query->query_vars[‘author’]) {
$offset1 = array_search($sticky_post1->ID, $sticky_posts);
unset( $sticky_posts[$offset1] );
}
}
$num_posts = count($posts);
$sticky_offset = 0;
// Loop over posts and relocate stickies to the front.
for ( $i = 0; $i < $num_posts; $i++ ) {
if ( in_array($posts[$i]->ID, $sticky_posts) ) {
$sticky_post = $posts[$i];
// Remove sticky from current position
array_splice($posts, $i, 1);
// Move to front, after other stickies
array_splice($posts, $sticky_offset, 0, array($sticky_post));
// Increment the sticky offset. The next sticky will be placed at this offset.
$sticky_offset++;
// Remove post from sticky posts array
$offset = array_search($sticky_post->ID, $sticky_posts);
unset( $sticky_posts[$offset] );
}
}
// If any posts have been excluded specifically, Ignore those that are sticky.
if ( !empty($sticky_posts) && !empty($wp_query->query_vars[‘post__not_in’] ) )
$sticky_posts = array_diff($sticky_posts, $wp_query->query_vars[‘post__not_in’]);
// Fetch sticky posts that weren’t in the query results
if ( !empty($sticky_posts) ) {
$stickies = get_posts( array(
‘post__in’ => $sticky_posts,
‘post_type’ => $wp_query->query_vars[‘post_type’],
‘post_status’ => ‘publish’,
‘nopaging’ => true
) );
foreach ( $stickies as $sticky_post ) {
array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
$sticky_offset++;
}
}
}
return $posts;
}
Step 2
Modify the homepage, theme template index.php
ignore_sticky_posts’ => 1
‘ignore_sticky_posts’ => 1 is the key parameter, cancel the article pinning (i.e. not displaying it at the top), and output the article in the normal way
Search
Popular on Blogar
Content Marketing-What is Content Marketing
- October 24th, 2023
- 1400 Views
Social Media Marketing-Social Media Marketing (SMM)
- October 24th, 2023
- 1691 Views
Pay-Per-Click-What is PPC
- October 24th, 2023
- 1271 Views
How to Activate Windows 11 Pro (No tools needed)
- September 3rd, 2024
- 97 Views
How to disable automatic updates for themes and plugins in WordPress
- August 16th, 2024
- 96 Views
Get all WordPress category names and IDs
- August 21st, 2024
- 94 Views
WordPress sticky articles in categories but not on the homepage
- August 13th, 2024
- 93 Views
WordPress-How to show single post content in front-end?
- August 26th, 2024
- 90 Views
How To Find The IP Of Any Whatsapp Call
- September 3rd, 2024
- 84 Views
WordPress display articles by category
- August 28th, 2024
- 84 Views
Recent Post
Content Marketing-What is Content Marketing
- October 24th, 2023
- 1400 Views
Social Media Marketing-Social Media Marketing (SMM)
- October 24th, 2023
- 1691 Views
Pay-Per-Click-What is PPC
- October 24th, 2023
- 1271 Views
VMware cannot install VMware Tools, prompts VMCI, memory driver
- November 25th, 2024
- 57 Views
More Post
- Howo Tractor Truck
- Howo Tipper/Dump Truck
- Howo Cargo Truck
- Howo Concrete Mixer Truck
- Howo Special Truck
- Howo Light Truck
- Shacman Tractor Truck
- Shacman Tipper/Dump Truck
- Shacman Cargo Truck
- Shacman Concrete Mixer Truck
- LGMG Mining Trucks
- XCMG Wheel Loader
- XCMG Excavator
- XCMG Crane
- XCMG Asphalt Pavers
- XCMG Road Roller
- Shantui Bulldozer
Copyright © 2024 BBBBF All Rights Reserved.