តើធ្វើដូចម្តេចដើម្បីធ្វើឱ្យ WordPress ប្រភេទ/ស្លាក/ទំព័រអ្នកនិពន្ធបង្ហាញអត្ថបទស្អិត?

WordPressមានមុខងារបិទភ្ជាប់អត្ថបទដែលភ្ជាប់មកជាមួយ ប៉ុន្តែតាមលំនាំដើមមានតែទំព័រកំពូលប៉ុណ្ណោះដែលត្រូវបានគាំទ្រដើម្បីបង្ហាញអត្ថបទស្អិត។

ទំព័របណ្ណសារផ្សេងទៀត (ដូចជាទំព័រប្រភេទ ទំព័រស្លាក ទំព័រអ្នកនិពន្ធ និងទំព័រកាលបរិច្ឆេទ) មិនអាចបង្ហាញអត្ថបទស្អិតនៅផ្នែកខាងលើបានទេ មានតែតាមលំដាប់លំនាំដើមប៉ុណ្ណោះ។

អ្វីដែលត្រូវធ្វើជាមួយ WordPressSEO មិត្តភក្តិ សង្ឃឹមថានឹងដោះស្រាយបញ្ហាទាំងនេះ។

តើធ្វើដូចម្តេចដើម្បីធ្វើឱ្យ 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 ហើយបន្ថែមថ្នាក់ដែលមានឈ្មោះស្អិតទៅប្រកាសស្អិត។

ស្បែក WordPress ទូទៅនឹងមានកូដ 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;
} 
  • បែបផែនរចនាប័ទ្មអត្ថបទកំពូលនៃទំព័រប្រភេទ,Chen Weiliangប្រភេទអត្ថបទប្លុក៖លេខទូរស័ព្ទចិន
  • (ការផ្លាស់ប្ដូរស្បែកបានផុតកំណត់)

មានវិធីមួយផ្សេងទៀតដើម្បីធ្វើឱ្យទំព័របណ្ណសារប្រភេទ WordPress បង្ហាញអត្ថបទ WordPress នៅផ្នែកខាងលើ▼

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) បានចែករំលែក "តើធ្វើដូចម្តេចដើម្បីធ្វើឱ្យ WordPress ចំណាត់ថ្នាក់/ស្លាក/ទំព័រអ្នកនិពន្ធ បង្ហាញអត្ថបទស្អិត? , ដើម្បី​ជួយ​អ្នក។

សូមស្វាគមន៍ចំពោះការចែករំលែកតំណភ្ជាប់នៃអត្ថបទនេះ៖https://www.chenweiliang.com/cwl-878.html

សូមស្វាគមន៍មកកាន់ឆានែល Telegram នៃប្លុករបស់ Chen Weiliang ដើម្បីទទួលបានព័ត៌មានថ្មីៗចុងក្រោយបង្អស់!

🔔 ក្លាយជាអ្នកដំបូងដែលទទួលបាន "ការណែនាំអំពីការប្រើប្រាស់ឧបករណ៍ AI ទីផ្សារមាតិកា ChatGPT" ដ៏មានតម្លៃនៅក្នុងបញ្ជីកំពូលរបស់ប៉ុស្តិ៍! 🌟
📚 មគ្គុទ្ទេសក៍នេះមានតម្លៃមហាសាល 🌟នេះជាឱកាសដ៏កម្រ សូមកុំអូសរំលង! ⏰⌛💨
Share និង Like ប្រសិនបើអ្នកចូលចិត្ត!
ការចែករំលែក និងការចូលចិត្តរបស់អ្នកគឺជាការលើកទឹកចិត្តបន្តរបស់យើង!

 

发表评论។

អាសយដ្ឋានអ៊ីមែលរបស់អ្នកនឹងមិនត្រូវបានផ្សព្វផ្សាយទេ។ 必填项已用។ * 标注។

រំកិលទៅកំពូល