वर्डप्रेस कोटी/ट्याग/लेखक पृष्ठलाई स्टिकी लेखहरू कसरी बनाउने?

WordPressत्यहाँ एउटा अन्तर्निहित लेख स्टिकिङ प्रकार्य छ, तर पूर्वनिर्धारित रूपमा केवल शीर्ष पृष्ठ टाँसिने लेखहरू प्रदर्शन गर्न समर्थित छ।

अन्य अभिलेख पृष्ठहरू (जस्तै श्रेणी पृष्ठहरू, ट्याग पृष्ठहरू, लेखक पृष्ठहरू, र मिति पृष्ठहरू) ले टाँसिने लेखहरू शीर्षमा प्रदर्शन गर्न सक्दैन, केवल पूर्वनिर्धारित क्रममा।

WordPress संग धेरै गर्न को लागीएसईओसाथीहरू, यी समस्याहरू समाधान गर्ने आशा छ।

वर्डप्रेस कोटी/ट्याग/लेखक पृष्ठलाई स्टिकी लेखहरू कसरी बनाउने?

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

WordPress स्टिकी लेख कोड

कृपया तपाईंको हालको थिम ▼ अन्तर्गत 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 मा थप्नुहोस् र स्टिकी पोस्टमा स्टिकी नामको वर्ग थप्नुहोस्।

सामान्य वर्डप्रेस विषयवस्तुहरूमा, शीर्ष लेख शैलीको लागि CSS कोड हुनेछ, तपाईं अनुकूलन ▼ थप्न सक्नुहुन्छ

//置顶文章添加样式
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;
} 

वर्डप्रेस श्रेणी संग्रह पृष्ठहरू शीर्षमा WordPress लेखहरू देखाउने अर्को तरिका छ

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

यस लेखको लिङ्क साझा गर्न स्वागत छ:https://www.chenweiliang.com/cwl-878.html

नवीनतम अपडेटहरू प्राप्त गर्न चेन वेइलियाङको ब्लगको टेलिग्राम च्यानलमा स्वागत छ!

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

 

评论 评论

तपाईको इ-मेल ठेगाना प्रकाशित हुँदैन। आवाश्यक फिल्डहरू प्रयोग भएको छ * लेबल

शीर्षमा स्क्रोल गर्नुहोस्