如何将WordPress文章类型设为特色并使其置顶显示?
嘿,我来帮你搞定这个特色文章置顶的需求!你之前尝试用meta_key的思路完全正确,咱们把这个流程补全,分步骤实现,确保后台操作友好,同时前台和后台列表都能让特色文章优先显示:
一、添加「特色文章」的后台操作入口
首先得给编辑者提供设置特色文章的入口,咱们先做文章编辑页的侧边Meta Box,再补充列表页的快速操作和批量操作,方便高效管理:
1. 文章编辑页的Meta Box
把这段代码放到主题的functions.php或者自定义插件里:
// 添加特色文章Meta Box add_action('add_meta_boxes', 'featured_post_meta_box'); function featured_post_meta_box() { add_meta_box( 'featured_post', '特色文章设置', 'featured_post_meta_box_callback', 'post', // *替换成你的自定义文章类型slug,如果不是默认post的话* 'side', 'default' ); } // Meta Box内容回调 function featured_post_meta_box_callback($post) { // 安全验证字段 wp_nonce_field('featured_post_nonce', 'featured_post_nonce_field'); // 获取当前文章的特色状态 $is_featured = get_post_meta($post->ID, '_is_featured_post', true); ?> <label for="is_featured_post"> <input type="checkbox" id="is_featured_post" name="is_featured_post" value="1" <?php checked($is_featured, '1'); ?>> 将这篇文章设为特色文章(会显示在列表顶部) </label> <?php } // 保存特色状态的Meta值 add_action('save_post', 'save_featured_post_meta'); function save_featured_post_meta($post_id) { // 验证非ce、权限和自动保存状态 if (!isset($_POST['featured_post_nonce_field']) || !wp_verify_nonce($_POST['featured_post_nonce_field'], 'featured_post_nonce')) { return; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (!current_user_can('edit_post', $post_id)) { return; } // 处理复选框值:勾选则存1,否则存0 $is_featured = isset($_POST['is_featured_post']) ? '1' : '0'; update_post_meta($post_id, '_is_featured_post', $is_featured); }
2. 文章列表页的快速编辑&批量操作
为了更高效管理,咱们给列表页加上快速编辑和批量操作选项:
// 在快速编辑面板添加特色文章选项 add_action('quick_edit_custom_box', 'featured_post_quick_edit', 10, 2); function featured_post_quick_edit($column_name, $post_type) { if ($post_type !== 'post') return; // *替换成你的文章类型slug* ?> <fieldset class="inline-edit-col-left"> <div class="inline-edit-col"> <label> <span class="title">特色文章</span> <span class="input-text-wrap"> <input type="checkbox" name="is_featured_post" id="quick-edit-featured" value="1"> </span> </label> </div> </fieldset> <?php } // 保存快速编辑的特色状态 add_action('save_post', 'save_featured_post_quick_edit', 11); function save_featured_post_quick_edit($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (!current_user_can('edit_post', $post_id)) return; $is_featured = isset($_POST['is_featured_post']) ? '1' : '0'; update_post_meta($post_id, '_is_featured_post', $is_featured); } // 添加批量操作选项 add_action('bulk_actions-edit-post', 'featured_post_bulk_actions'); function featured_post_bulk_actions($actions) { $actions['mark_featured'] = '标记为特色文章'; $actions['unmark_featured'] = '取消特色文章'; return $actions; } // 处理批量操作逻辑 add_action('admin_action_mark_featured', 'process_featured_post_bulk_action'); add_action('admin_action_unmark_featured', 'process_featured_post_bulk_action'); function process_featured_post_bulk_action() { if (!isset($_POST['post']) || !is_array($_POST['post'])) return; foreach ($_POST['post'] as $post_id) { if (!current_user_can('edit_post', $post_id)) continue; $value = current_action() === 'mark_featured' ? '1' : '0'; update_post_meta($post_id, '_is_featured_post', $value); } // 跳回文章列表页 wp_redirect(admin_url('edit.php?post_type=post')); // *替换成你的文章类型slug* exit; }
二、修改文章查询,让特色文章置顶
接下来要让后台列表和前台的文章列表(比如博客首页、归档页)都把特色文章放在最前面,用pre_get_posts钩子修改查询顺序:
// 修改文章查询顺序,特色文章优先置顶 add_action('pre_get_posts', 'featured_posts_on_top'); function featured_posts_on_top($query) { // 后台列表页逻辑 if (is_admin() && !defined('DOING_AJAX')) { $screen = get_current_screen(); // 只影响目标文章类型的列表页查询 if ($screen->base === 'edit' && $screen->post_type === 'post') { // *替换成你的文章类型slug* $query->set('meta_key', '_is_featured_post'); $query->set('orderby', 'meta_value_num date'); $query->set('order', 'DESC DESC'); // 排序逻辑:先按特色状态倒序(1在0前面),再按发布日期倒序 } } // 前台查询逻辑(博客首页、归档页等) else { if ($query->is_main_query() && !is_singular()) { $query->set('meta_key', '_is_featured_post'); $query->set('orderby', 'meta_value_num date'); $query->set('order', 'DESC DESC'); } } }
这里的排序逻辑很重要:meta_value_num把咱们存的_is_featured_post值当数字排序,所以标记为1的特色文章会全部排在普通文章前面,同一类文章内部还是按发布日期倒序,保持最新的在前。
三、可选:在后台列表页显示「特色」标签
为了让编辑者一眼识别特色文章,咱们在文章标题前加个醒目标签:
// 在文章列表页标题前添加特色标签 add_filter('the_title', 'featured_post_title_tag', 10, 2); function featured_post_title_tag($title, $post_id) { $screen = get_current_screen(); if (is_admin() && $screen->base === 'edit' && $screen->post_type === 'post') { // *替换成你的文章类型slug* $is_featured = get_post_meta($post_id, '_is_featured_post', true); if ($is_featured === '1') { $title = '<span style="color:#007cba; font-weight:bold;">[特色]</span> ' . $title; } } return $title; }
最后提醒
- 所有代码里标注
*替换成你的文章类型slug*的地方,记得换成你实际使用的自定义文章类型标识(比如portfolio)。 - 推荐把代码放到自定义插件里,避免主题更新导致代码丢失;如果用主题的
functions.php,建议用子主题。
内容的提问来源于stack exchange,提问作者Kawish Fazal
相关产品推荐
相关产品推荐

