ఆర్టికల్ డైరెక్టరీ
WordPressఅంతర్నిర్మిత కథనం అంటుకునే ఫంక్షన్ ఉంది, కానీ డిఫాల్ట్గా స్టిక్కీ కథనాలను ప్రదర్శించడానికి అగ్ర పేజీ మాత్రమే మద్దతు ఇస్తుంది.
ఇతర ఆర్కైవ్ పేజీలు (వర్గం పేజీలు, ట్యాగ్ పేజీలు, రచయిత పేజీలు మరియు తేదీ పేజీలు వంటివి) ఎగువన స్టిక్కీ కథనాలను ప్రదర్శించలేవు, డిఫాల్ట్ క్రమంలో మాత్రమే.
WordPressతో చాలా ఉన్నాయిSEOమిత్రులారా, ఈ సమస్యలను పరిష్కరిస్తారని ఆశిస్తున్నాను.
వాస్తవానికి, మేము 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()
అగ్ర కథనానికి శైలిని జోడించండి
మీరు స్టిక్కీ పోస్ట్కు స్టైల్లను జోడించాలనుకుంటే, ఫంక్షన్లు.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; }
- వర్గం పేజీ యొక్క అగ్ర కథనం శైలి ప్రభావం,చెన్ వీలియాంగ్బ్లాగ్ వ్యాస వర్గాలు:చైనీస్ మొబైల్ నంబర్
- (థీమ్ మార్పు గడువు ముగిసింది)
WordPress వర్గం ఆర్కైవ్ పేజీలు ఎగువన WordPress కథనాలను చూపేలా చేయడానికి మరొక మార్గం ఉంది▼
హోప్ చెన్ వీలియాంగ్ బ్లాగ్ ( https://www.chenweiliang.com/ ) భాగస్వామ్యం చేసిన "WordPress వర్గం/ట్యాగ్/రచయిత పేజీని అంటుకునే కథనాలను ఎలా చూపించాలి? , నీకు సహాయం చెయ్యడానికి.
ఈ కథనం యొక్క లింక్ను భాగస్వామ్యం చేయడానికి స్వాగతం:https://www.chenweiliang.com/cwl-878.html
తాజా నవీకరణలను పొందడానికి చెన్ వీలియాంగ్ బ్లాగ్ యొక్క టెలిగ్రామ్ ఛానెల్కు స్వాగతం!
📚 ఈ గైడ్లో భారీ విలువ ఉంది, 🌟ఇది ఒక అరుదైన అవకాశం, దీన్ని మిస్ చేయకండి! ⏰⌛💨
నచ్చితే లైక్ చేసి షేర్ చేయండి!
మీ భాగస్వామ్యం మరియు ఇష్టాలు మా నిరంతర ప్రేరణ!