>  기사  >  CMS 튜토리얼  >  WordPress 카테고리, 태그 등 아카이브 페이지를 상단에 고정하는 방법

WordPress 카테고리, 태그 등 아카이브 페이지를 상단에 고정하는 방법

藏色散人
藏色散人원래의
2019-12-28 09:52:261906검색

WordPress 카테고리, 태그 등 아카이브 페이지를 상단에 고정하는 방법

워드프레스 카테고리, 태그 등 아카이브 페이지를 상단에 고정하는 방법은 무엇인가요?

이 문서의 예에서는 WordPress 카테고리 및 태그와 같은 아카이브 페이지의 상단에 도달하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 분석은 다음과 같습니다.

권장: "wordpress tutorial"

wordpress에서는 기본적으로 기사를 페이지 상단에 고정할 수 있습니다. 이는 홈페이지와 같은 아카이브 페이지에만 해당됩니다. 기사를 상단에 고정하기 위한 카테고리/태그도 2차 개발이 필요합니다.

이제 wp-includes/query.php 홈페이지 상단의 코드를 참고하여 약간만 수정하면 카테고리 페이지 등의 아카이브 페이지가 생성됩니다. , 태그 페이지, 작성자 페이지 및 날짜 페이지도 홈페이지처럼 상단에 해당 범위 내에서 상위 기사를 표시할 수 있습니다. 현재 테마 아래의 function.php에 다음 코드를 입력하면 됩니다.

add_filter('the_posts', 'putStickyOnTop' ); 
function putStickyOnTop( $posts ) { 
if(is_home() || !is_main_query() || !is_archive()) 
return $posts; 
global $wp_query; 
$sticky_posts = get_option('sticky_posts'); 
if ( $wp_query->query_vars[&#39;paged&#39;] <= 1 && is_array($sticky_posts) && !emptyempty($sticky_posts) && !get_query_var(&#39;ignore_sticky_posts&#39;) ) { $stickies1 = get_posts( array( &#39;post__in&#39; => $sticky_posts ) ); 
foreach ( $stickies1 as $sticky_post1 ) { 
// 判断当前是否分类页 
if($wp_query->is_category == 1 && !has_category($wp_query->query_vars[&#39;cat&#39;], $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[&#39;tag&#39;], $sticky_post1->ID)) { 
// 去除不属于本标签的文章 
$offset1 = array_search($sticky_post1->ID, $sticky_posts); 
unset( $sticky_posts[$offset1] ); 
} 
if($wp_query->is_year == 1 && date_i18n(&#39;Y&#39;, strtotime($sticky_post1->post_date))!=$wp_query->query[&#39;m&#39;]) { 
// 去除不属于本年份的文章 
$offset1 = array_search($sticky_post1->ID, $sticky_posts); 
unset( $sticky_posts[$offset1] ); 
} 
if($wp_query->is_month == 1 && date_i18n(&#39;Ym&#39;, strtotime($sticky_post1->post_date))!=$wp_query->query[&#39;m&#39;]) { 
// 去除不属于本月份的文章 
$offset1 = array_search($sticky_post1->ID, $sticky_posts); 
unset( $sticky_posts[$offset1] ); 
} 
if($wp_query->is_day == 1 && date_i18n(&#39;Ymd&#39;, strtotime($sticky_post1->post_date))!=$wp_query->query[&#39;m&#39;]) { 
// 去除不属于本日期的文章 
$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[&#39;author&#39;]) { 
// 去除不属于本作者的文章 
$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 ( !emptyempty($sticky_posts) && !emptyempty($wp_query->query_vars[&#39;post__not_in&#39;] ) ) 
$sticky_posts = array_diff($sticky_posts, $wp_query->query_vars[&#39;post__not_in&#39;]); 
// Fetch sticky posts that weren&#39;t in the query results 
if ( !emptyempty($sticky_posts) ) { 
$stickies = get_posts( array( 
&#39;post__in&#39; => $sticky_posts, 
&#39;post_type&#39; => $wp_query->query_vars[&#39;post_type&#39;], 
&#39;post_status&#39; => &#39;publish&#39;, 
&#39;nopaging&#39; => true 
) ); 
foreach ( $stickies as $sticky_post ) { 
array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) ); 
$sticky_offset++; 
} 
} 
} 
return $posts; 
}

코드 설명:

1. 보관 페이지에 고정된 모든 기사를 표시하려면 코드의 11-43행을 삭제하세요.

2. 특정 카테고리 페이지에서 3번째 줄의

코드를 다음과 같이 변경하세요.

if( 
//改成: 
// abc是分类名称 
if ( is_category( &#39;abc&#39; ) ||

3. 특정 탭 페이지에 고정된 글이 표시되지 않게 하려면 3번째 줄을 변경하세요. 코드

코드는 다음과 같습니다. 다음:

if( 
//改成: 
// abc是标签名称 
if ( is_tag( &#39;abc&#39; ) ||

4. 작성자 페이지에 고정된 기사가 표시되지 않도록 하려면 3행의

코드를 다음과 같이 변경하세요.

if( 
//改成: 
// abc是作者昵称 
if ( is_author( &#39;abc&#39; ) ||

5. 위 코드는 다음과 같은 경우에만 메인 루프에 유효합니다. 아카이브 페이지 WP_Query 또는 query_posts에서 이를 사용하여 기사 목록을 가져오고 이 목록의 상단에 고정된 기사를 표시하려면 코드의 세 번째 줄에서 다음 코드를 삭제할 수 있습니다(참고: 표시되는 기사 수는 다를 수 있음) 설정한 것에서):

Code 다음과 같습니다:

코드는 다음과 같습니다:

|| !is_main_query()

Sticky 스타일: 끈끈한 기사에 스타일을 추가하려면 function.php에 다음 코드를 추가하세요. 상단 기사에 끈끈한 이름의 클래스, 특정 CSS 코드를 추가하고 사용자 정의:

코드는 다음과 같습니다.

add_filter(&#39;post_class&#39;, &#39;addStickyClass&#39; ,10,3 ); 
function addStickyClass( $classes, $class, $post_id ){ 
if( is_sticky() && is_category() && !isset( $classes[&#39;sticky&#39;] ) ){ 
$classes[] = &#39;sticky&#39;; 
} 
return $classes; 
}

이 기사가 모든 사람의

WordPress 웹 사이트 구축에 도움이 되기를 바랍니다.

위 내용은 WordPress 카테고리, 태그 등 아카이브 페이지를 상단에 고정하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.