वर्डप्रेस कैटेगरी/टैग/ऑथर पेज को स्टिकी आर्टिकल्स कैसे दिखाएं?

WordPressएक अंतर्निहित लेख चिपकाने वाला कार्य है, लेकिन डिफ़ॉल्ट रूप से चिपचिपा लेख प्रदर्शित करने के लिए केवल शीर्ष पृष्ठ का समर्थन किया जाता है।

अन्य संग्रह पृष्ठ (जैसे श्रेणी पृष्ठ, टैग पृष्ठ, लेखक पृष्ठ और दिनांक पृष्ठ) केवल डिफ़ॉल्ट क्रम में शीर्ष पर चिपचिपा लेख प्रदर्शित नहीं कर सकते हैं।

वर्डप्रेस के साथ बहुत कुछ करनाएसईओदोस्तों, इन समस्याओं को हल करने की उम्मीद है।

वर्डप्रेस कैटेगरी/टैग/ऑथर पेज को स्टिकी आर्टिकल्स कैसे दिखाएं?

वास्तव में, हमें केवल wp-includes/query.php के होम पेज के कोड को संदर्भित करने और इसे थोड़ा संशोधित करने की आवश्यकता है, ताकि संग्रह पृष्ठ के शीर्ष (जैसे श्रेणी पृष्ठ, टैब पृष्ठ, लेखक पृष्ठ और दिनांक पृष्ठ) ) शीर्ष लेख भी प्रदर्शित कर सकते हैं।

वर्डप्रेस चिपचिपा लेख कोड

कृपया निम्न कोड को अपने वर्तमान विषय के अंतर्गत functions.php फ़ाइल में डालें

//让WordPress分类、标签、存档和作者页显示置顶文章
add_filter('the_posts', 'putStickyOnTop' );
function putStickyOnTop( $posts ) {
if ( is_series() || 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;
}

//置顶文章添加样式
add_filter('post_class',  'addStickyClass' ,10,3 );
function addStickyClass( $classes, $class, $post_id ){
  if( is_sticky() && is_category() && !isset( $classes['sticky'] ) ){
    $classes[] = 'sticky';
  }
  
  return $classes;
} 

चिपचिपा लेख के कोड का उपयोग करने के निर्देश

1) यदि आप चाहते हैं कि संग्रह पृष्ठ सभी शीर्ष लेखों को प्रदर्शित करे, तो कृपया कोड की 11-43 पंक्तियों को हटा दें;

2) यदि आप श्रेणी पृष्ठ पर शीर्ष लेख प्रदर्शित नहीं करना चाहते हैं, तो कृपया को पंक्ति 3 . में जोड़ें

if(

में संशोधित करें

// abc是分类的名称
if ( is_category( 'abc' ) || 

3) यदि आप शीर्ष लेख को टैब पृष्ठ पर प्रदर्शित नहीं करना चाहते हैं, तो कृपया पंक्ति 3 में

if(

में बदलें:

// abc是标签的名称
if ( is_tag( 'abc' ) || 

4) यदि आप नहीं चाहते कि लेखक पृष्ठ शीर्ष लेख प्रदर्शित करे, तो कृपया पंक्ति 3 में

if(

में संशोधित करें

// abc是作者的昵称
if ( is_author( 'abc' ) || 

5) यदि आप नहीं चाहते कि एक कस्टम श्रेणी पृष्ठ शीर्ष लेख प्रदर्शित करे, तो डाल दें

if(

इसे इसमें बदलें:

// series是自定义分类、abc是自定义分类名称
if ( is_series( 'abc' ) ||

उपरोक्त कोड केवल मुख्य लूप के लिए मान्य है, यदि आप संग्रह पृष्ठ पर पोस्ट की सूची प्राप्त करने के लिए WP_Query या query_posts का उपयोग कर रहे हैं, और उन सूचियों के शीर्ष पर पिन किए गए पोस्ट प्रदर्शित करना चाहते हैं।

आप पंक्ति 3 में निम्न कोड को हटा सकते हैं (आपके द्वारा सेट किए गए लेखों की संख्या प्रदर्शित होने से भिन्न हो सकती है)

|| !is_main_query() 

शीर्ष लेख में शैली जोड़ें

यदि आप स्टिकी पोस्ट में स्टाइल जोड़ना चाहते हैं, तो निम्नलिखित कोड को functions.php में जोड़ें और स्टिकी पोस्ट में स्टिकी नामक एक वर्ग जोड़ें।

सामान्य वर्डप्रेस थीम में, शीर्ष लेख शैली के लिए सीएसएस कोड होगा, आप कस्टम . भी जोड़ सकते हैं

//置顶文章添加样式
add_filter('post_class',  'addStickyClass' ,10,3 );
function addStickyClass( $classes, $class, $post_id ){
  if( is_sticky() && is_category() && !isset( $classes['sticky'] ) ){
    $classes[] = 'sticky';
  }
  
  return $classes;
} 

वर्डप्रेस श्रेणी संग्रह पृष्ठों को शीर्ष पर वर्डप्रेस लेख दिखाने का एक और तरीका है▼

होप चेन वेइलियांग ब्लॉग ( https://www.chenweiliang.com/ ) साझा किया गया "वर्डप्रेस श्रेणी/टैग/लेखक पृष्ठ को चिपचिपा लेख कैसे दिखाएं? , आपकी मदद के लिए।

इस लेख का लिंक साझा करने के लिए आपका स्वागत है:https://www.chenweiliang.com/cwl-878.html

नवीनतम अपडेट प्राप्त करने के लिए चेन वेइलियांग के ब्लॉग के टेलीग्राम चैनल में आपका स्वागत है!

🔔 चैनल शीर्ष निर्देशिका में मूल्यवान "चैटजीपीटी कंटेंट मार्केटिंग एआई टूल उपयोग गाइड" प्राप्त करने वाले पहले व्यक्ति बनें! 🌟
📚 इस गाइड में बहुत महत्व है, 🌟यह एक दुर्लभ अवसर है, इसे न चूकें! ⏰⌛💨
अच्छा लगे तो शेयर और लाइक करें!
आपका साझा करना और पसंद करना हमारी निरंतर प्रेरणा है!

 

发表 评论

आपकी ईमेल आईडी प्रकाशित नहीं की जाएगी। आवश्यक फ़ील्ड का उपयोग किया जाता है * लेबल

शीर्ष तक स्क्रॉल करें