වර්ඩ්ප්‍රෙස් ප්‍රවර්ගය/ටැග්/කර්තෘ පිටුව ඇලෙන සුළු ලිපි පෙන්වන්නේ කෙසේද?

වර්ඩ්ප්රෙස්බිල්ට් ලිපි ඇලවීමේ ශ්‍රිතයක් ඇත, නමුත් පෙරනිමියෙන් ඇලෙන සුළු ලිපි පෙන්වීමට සහය දක්වන්නේ ඉහළ පිටුවට පමණි.

අනෙකුත් සංරක්ෂිත පිටු (ප්‍රවර්ග පිටු, ටැග් පිටු, කර්තෘ පිටු සහ දින පිටු වැනි) පෙරනිමි අනුපිළිවෙලින් පමණක් ඇලෙන සුළු ලිපි ඉහලින් සංදර්ශන කළ නොහැක.

WordPress සමඟ බොහෝ දේ කළ හැකියSEOමිත්‍රවරුනි, මේ ප්‍රශ්න විසඳන්න බලාපොරොත්තු වෙනවා.

වර්ඩ්ප්‍රෙස් ප්‍රවර්ගය/ටැග්/කර්තෘ පිටුව ඇලෙන සුළු ලිපි පෙන්වන්නේ කෙසේද?

ඇත්ත වශයෙන්ම, අපට අවශ්‍ය වන්නේ 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 වෙත එක් කර ඇලෙන පෝස්ට් එකට sticky නම් පන්තියක් එක් කරන්න.

සාමාන්‍ය වර්ඩ්ප්‍රෙස් තේමා වලදී, ඉහළ ලිපි විලාසය සඳහා 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;
} 

වර්ඩ්ප්‍රෙස් ප්‍රවර්ග සංරක්ෂිත පිටු වර්ඩ්ප්‍රෙස් ලිපි ඉහළින්ම පෙන්වීමට තවත් ක්‍රමයක් තිබේ▼

Hope Chen Weiliang බ්ලොග් ( https://www.chenweiliang.com/ ) shared "වර්ඩ්ප්‍රෙස් ප්‍රවර්ගය/ටැග්/කර්තෘ පිටුව ඇලෙන සුළු ලිපි පෙන්වන්නේ කෙසේද? , ඔබට උදව් කිරීමට.

මෙම ලිපියේ සබැඳිය බෙදා ගැනීමට සාදරයෙන් පිළිගනිමු:https://www.chenweiliang.com/cwl-878.html

නවතම යාවත්කාලීන ලබා ගැනීමට Chen Weiliang ගේ බ්ලොගයේ Telegram නාලිකාව වෙත සාදරයෙන් පිළිගනිමු!

🔔 නාලිකා ඉහළ නාමාවලියෙහි වටිනා "ChatGPT අන්තර්ගත අලෙවිකරණ AI මෙවලම් භාවිත මාර්ගෝපදේශය" ලබා ගත් පළමු පුද්ගලයා වන්න! 🌟
📚 මෙම මාර්ගෝපදේශයෙහි විශාල වටිනාකමක් ඇත, 🌟මෙය දුර්ලභ අවස්ථාවක්, එය අතපසු නොකරන්න! ⏰⌛💨
ඔබ කැමති නම් Share කර Like කරන්න!
ඔබගේ බෙදාගැනීම් සහ කැමැත්ත අපගේ අඛණ්ඩ අභිප්‍රේරණයයි!

 

发表

ඔබගේ විද්‍යුත් තැපැල් ලිපිනය ප්‍රකාශයට පත් නොකෙරේ. අවශ්‍ය ක්ෂේත්‍ර භාවිතා වේ * ලේබලය

ඉහළට අනුචලනය කරන්න