WordPress 카테고리/태그/작성자 페이지에 고정 기사를 표시하는 방법은 무엇입니까?

워드프레스(WordPress)기사 붙이기 기능이 내장되어 있지만, 붙박이 기사 표시는 기본적으로 맨 위 페이지만 지원합니다.

다른 아카이브 페이지(예: 카테고리 페이지, 태그 페이지, 작성자 페이지 및 날짜 페이지)는 상단에 고정 기사를 표시할 수 없으며 기본 순서로만 표시됩니다.

워드프레스로 할 일이 많다SEO친구, 이러한 문제를 해결하기를 바랍니다.

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에 다음 코드를 추가하고 고정 포스트에sticky라는 클래스를 추가하세요.

일반 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;
} 

WordPress 카테고리 아카이브 페이지를 상단에 WordPress 기사를 표시하는 또 다른 방법이 있습니다▼

희망 첸 웨이량 블로그( https://www.chenweiliang.com/ ) 공유 "WordPress 카테고리/태그/작성자 페이지에 고정 기사를 표시하는 방법은 무엇입니까? , 당신을 돕기 위해.

이 기사의 링크를 공유하는 것을 환영합니다:https://www.chenweiliang.com/cwl-878.html

최신 업데이트를 받으려면 Chen Weiliang 블로그의 Telegram 채널에 오신 것을 환영합니다!

🔔 채널 상위 디렉토리에서 귀중한 "ChatGPT 콘텐츠 마케팅 AI 도구 사용 가이드"를 가장 먼저 받아보세요! 🌟
📚 이 가이드에는 엄청난 가치가 담겨 있습니다. 🌟이것은 흔치 않은 기회입니다. 놓치지 마세요! ⏰⌛💨
당신이 원한다면 공유하고 좋아하십시오!
당신의 공유와 좋아요는 우리의 지속적인 동기 부여입니다!

 

发表 评论

귀하의 이메일 주소는 공개되지 않습니다. 必填 项 已 用 * 标注

맨위로 스크롤