在WordPress中,可以有三种置顶文章的设置,但是都是将置顶的文章写入到options数据表中,meta_key为sticky_posts。
更新文章时:
![图片[1]-WordPress置顶文章 - KEKC博客-KEKC博客](http://www.kekc.cn/wp-content/uploads/2024/05/20240527105333137-image.png)
新增文章时:
![图片[2]-WordPress置顶文章 - KEKC博客-KEKC博客](http://www.kekc.cn/wp-content/uploads/2024/05/20240527105401912-image.png)
快速编辑
![图片[3]-WordPress置顶文章 - KEKC博客-KEKC博客](http://www.kekc.cn/wp-content/uploads/2024/05/20240527105508659-image.png)
今天介绍代码方式,不使用默认的置顶:
这个例子中,手机端置顶两篇,电脑端置顶一篇,同时博客页面ID为19114。
// 置顶三篇文章add_action( 'pre_get_posts', function($q){if(19114 == get_queried_object_id()){$stickies = array(165618,166278,161883);if ( wp_is_mobile() ) {$stickies = array(165618,166278);}$q->set( 'post__not_in', $stickies );if ( !$q->is_paged() ) {global $my_filter_executed;$my_filter_executed = false;add_filter( 'the_posts', function ( $posts ) use ( $stickies ){global $my_filter_executed;if($posts and !$my_filter_executed){$term_stickies = get_posts( array('post__in' => $stickies, 'nopaging' => true) );$posts = array_merge( $term_stickies, $posts );$my_filter_executed = true;}return $posts;}, 10, 1 );}}});// 置顶三篇文章 add_action( 'pre_get_posts', function($q){ if(19114 == get_queried_object_id()){ $stickies = array(165618,166278,161883); if ( wp_is_mobile() ) { $stickies = array(165618,166278); } $q->set( 'post__not_in', $stickies ); if ( !$q->is_paged() ) { global $my_filter_executed; $my_filter_executed = false; add_filter( 'the_posts', function ( $posts ) use ( $stickies ){ global $my_filter_executed; if($posts and !$my_filter_executed){ $term_stickies = get_posts( array('post__in' => $stickies, 'nopaging' => true) ); $posts = array_merge( $term_stickies, $posts ); $my_filter_executed = true; } return $posts; }, 10, 1 ); } } });// 置顶三篇文章 add_action( 'pre_get_posts', function($q){ if(19114 == get_queried_object_id()){ $stickies = array(165618,166278,161883); if ( wp_is_mobile() ) { $stickies = array(165618,166278); } $q->set( 'post__not_in', $stickies ); if ( !$q->is_paged() ) { global $my_filter_executed; $my_filter_executed = false; add_filter( 'the_posts', function ( $posts ) use ( $stickies ){ global $my_filter_executed; if($posts and !$my_filter_executed){ $term_stickies = get_posts( array('post__in' => $stickies, 'nopaging' => true) ); $posts = array_merge( $term_stickies, $posts ); $my_filter_executed = true; } return $posts; }, 10, 1 ); } } });
© 版权声明
THE END
暂无评论内容