如何在WordPress文章标题前显示“Post Updated”标签并通过快速编辑控制
WordPress剧集站点Post Updated标识实现方案
不用安装额外功能插件,直接用自定义代码即可实现完整需求,所有代码可插入当前主题functions.php文件,也可通过代码片段管理插件添加,避免主题更新后代码丢失。
步骤1:注册自定义存储字段
首先注册用于存储标识开关状态的自定义元字段:
// 注册Post Updated状态存储字段 function custom_register_updated_meta() { // 如果你用的是自定义文章类型,把下方的'post'替换为你的文章类型别名即可 register_post_meta('post', 'is_post_updated', array( 'show_in_rest' => true, 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', 'auth_callback' => function() { return current_user_can('edit_posts'); } )); } add_action('init', 'custom_register_updated_meta');
步骤2:快速编辑窗口添加控制复选框
实现快速编辑区域的复选框展示、值回填、保存逻辑:
// 快速编辑区加入Post Updated复选框 function custom_add_quick_edit_checkbox($column_name, $post_type) { // 文章类型匹配和修改规则同上 if ($column_name == 'is_post_updated' && $post_type == 'post') { ?> <fieldset class="inline-edit-col-right"> <div class="inline-edit-col"> <label class="alignleft"> <input type="checkbox" name="is_post_updated" class="is-post-updated-checkbox"> <span class="checkbox-title">Post Updated</span> </label> </div> </fieldset> <?php } } add_action('quick_edit_custom_box', 'custom_add_quick_edit_checkbox', 10, 2); // 保存快速编辑提交的开关值 function custom_save_updated_meta($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (!current_user_can('edit_post', $post_id)) return; $status = isset($_POST['is_post_updated']) ? true : false; update_post_meta($post_id, 'is_post_updated', $status); } add_action('save_post', 'custom_save_updated_meta'); // 后台列表添加隐藏列存储值,用于快速编辑回填 function custom_add_updated_column($columns) { $columns['is_post_updated'] = '更新标识'; return $columns; } // 非默认post类型修改为 manage_{自定义类型别名}_posts_columns add_filter('manage_post_posts_columns', 'custom_add_updated_column'); function custom_render_updated_column($column, $post_id) { if ($column == 'is_post_updated') { $val = get_post_meta($post_id, 'is_post_updated', true) ? '1' : '0'; echo '<div class="is-post-updated-val hidden">' . esc_html($val) . '</div>'; } } // 非默认post类型修改为 manage_{自定义类型别名}_posts_custom_column add_action('manage_post_posts_custom_column', 'custom_render_updated_column', 10, 2); // 快速编辑复选框状态回填JS function custom_quick_edit_fill_value() { global $current_screen; // 文章类型匹配规则同上 if ($current_screen->id != 'edit-post' || $current_screen->post_type != 'post') return; ?> <script> jQuery(function($) { const originalEditMethod = inlineEditPost.edit; inlineEditPost.edit = function(id) { originalEditMethod.apply(this, arguments); const postId = typeof id == 'object' ? parseInt(this.getId(id)) : 0; if (postId > 0) { const isChecked = $('#inline_' + postId).find('.is-post-updated-val').text() == '1'; $('#edit-' + postId).find('.is-post-updated-checkbox').prop('checked', isChecked); } }; }); </script> <?php } add_action('admin_footer', 'custom_quick_edit_fill_value');
步骤3:前端展示Post Updated标识
模板直接调用
在你需要显示标识的模板位置(比如标题旁、剧集详情页头部)插入如下代码:
<?php if (get_post_meta(get_the_ID(), 'is_post_updated', true)) : ?> <span class="post-updated-badge">Post Updated</span> <?php endif; ?>
添加以下CSS自定义标识样式,可根据站点风格调整:
.post-updated-badge { display: inline-block; margin-left: 8px; padding: 2px 8px; font-size: 12px; color: #fff; background-color: #dc2626; border-radius: 4px; }
短代码调用(区块编辑器适用)
如果需要在文章内容内插入标识,可添加以下代码注册短代码:
function custom_updated_badge_shortcode() { if (get_post_meta(get_the_ID(), 'is_post_updated', true)) { return '<span class="post-updated-badge">Post Updated</span>'; } return ''; } add_shortcode('post_updated_badge', 'custom_updated_badge_shortcode');
之后在文章编辑器内插入[post_updated_badge]即可显示标识。
内容的提问来源于stack exchange,提问作者Ahtsham Khan Qazi
相关产品推荐
相关产品推荐

