功能增强简介
发布文章时自定义文章前缀是一个非常实用的功能,它允许作者或编辑在文章标题前添加特定的文字或图像标签。这样的功能对于区分文章类型、提高文章的可识别度或者增加视觉吸引力都非常有帮助。接下来,我们将探讨如何使用这个功能以及它的主要特点。在优知新的网站看到的,但是修改主题文件就pass了,然后就有了下面的功能。本站在原作者的基础上在稍微美化了一点,增加了几张美化样式,如:亲测、实测、原创、如多种颜色。(可同时设置多个声明前缀)
效果展示
通过启用自定义文章前缀功能,用户可以
-
- 添加文本前缀:在文章标题前添加自定义文本,例如“[推荐]”、“[热门]”等,来标明文章的特殊属性或类别。
-
- 使用图像作为前缀:除了文本,还可以选择一个小图标作为文章的前缀,使文章在列表中更加醒目。
-
- 自定义CSS样式:为每个前缀设定独立的CSS样式,确保前缀的显示效果与网站的整体设计风格保持一致。
-
- 灵活配置:通过后台设置,可以轻松地为文章添加或修改前缀,而无需直接编辑代码。
添加教程
在func.php加入代码即可,(如果没有就加在主题的functions.php文件中)
/*文章前缀申明开始-思牧分享资源网网-wp.magiclee.com*/
// 注册Meta Box
function my_custom_prefixes_meta_box() {
add_meta_box(
'custom-title-prefixes', // Meta Box ID
'自定义标题前缀', // Meta Box 标题
'my_custom_prefixes_meta_box_callback', // 显示内容的回调函数
'post', // 对哪种类型的内容生效
'side' // 在哪个位置显示('normal', 'side', 'advanced')javascript:;
);
}
add_action('add_meta_boxes', 'my_custom_prefixes_meta_box');
// 获取可用的图片选项
function get_my_image_options() {
return array(
'实测1' => 'https://www.uzhix.com/wp-content/themes/zibllsucai/img/svg/shice.svg',
'实测2' => 'https://www.hgymw.com/wp-content/uploads/2024/02/shice.svg',
'亲测' => 'https://www.hgymw.com/wp-content/uploads/2024/02/qince.svg',
'原创' => 'https://www.hgymw.com/wp-content/uploads/2024/02/yuanc.svg',
);
}
// Meta Box内容的回调函数
function my_custom_prefixes_meta_box_callback($post) {
wp_nonce_field('custom_title_prefix_save', 'custom_title_prefix_nonce');
// 获取已保存的值
$saved_text_data = get_post_meta($post->ID, '_dynamic_title_prefixes', true);
$saved_image_names = get_post_meta($post->ID, '_selected_image_names', true);
if (!is_array($saved_image_names)) {
$saved_image_names = [];
}
// 文字前缀界面
echo '<div id="text_prefixes_container">
<p>使用以下格式添加文字前缀和CSS(一行一个):<br/>例:测试|ove_prefix</p>
<textarea name="dynamic_title_prefixes" style="width: 100%;" rows="5">'. esc_textarea($saved_text_data) .'</textarea>
</div>';
// 图像选择区
$options = get_my_image_options();
echo '<div id="image_selection_container" style="margin-top: 20px;">
<p>选择图像前缀:</p>';
foreach ($options as $name => $url) {
$checked = in_array($name, $saved_image_names) ? ' checked' : '';
echo "<label><input type='checkbox' name='image_name_selection[]' value='".esc_attr($name)."'$checked> ".esc_html($name)."</label><br>";
}
echo '</div>';
}
// 保存Meta Box中的数据
function save_custom_title_prefixes_data($post_id) {
if (!isset($_POST['custom_title_prefix_nonce']) ||
!wp_verify_nonce($_POST['custom_title_prefix_nonce'], 'custom_title_prefix_save') ||
(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ||
!current_user_can('edit_post', $post_id)) {
return;
}
// 保存文本前缀
if (isset($_POST['dynamic_title_prefixes'])) {
update_post_meta($post_id, '_dynamic_title_prefixes', sanitize_textarea_field($_POST['dynamic_title_prefixes']));
}
// 保存选择的图片名称
if (isset($_POST['image_name_selection'])) {
update_post_meta($post_id, '_selected_image_names', $_POST['image_name_selection']);
} else {
delete_post_meta($post_id, '_selected_image_names');
}
}
add_action('save_post', 'save_custom_title_prefixes_data');
// 显示标题时应用前缀和自定义CSS
// 显示标题时应用前缀和自定义CSS
function apply_custom_prefixes_to_title($title, $id = null) {
if (!is_admin() && !is_single() && $id) {
// 处理文本前缀
$prefixes_str = get_post_meta($id, '_dynamic_title_prefixes', true);
$prefixes = explode("\n", $prefixes_str);
foreach ($prefixes as $prefix) {
list($prefix_text, $css_class) = array_map('trim', explode('|', $prefix));
if (!empty($prefix_text)) {
$title = "<span class='". esc_attr($css_class) ."'>" . esc_html($prefix_text) . "</span> " . $title;
}
}
// 处理图片前缀
$selected_image_names = get_post_meta($id, '_selected_image_names', true);
if (!empty($selected_image_names) && is_array($selected_image_names)) {
$options = get_my_image_options();
foreach ($selected_image_names as $name) {
if (isset($options[$name])) {
$url = esc_url($options[$name]);
$title = "<img src='$url' alt='Prefix Image' style=' height: 20px; pointer-events: none;'/>" . $title;
}
}
}
}
return $title;
}
add_filter('the_title', '<a target="_blank" href="https://www.hgymw.com/tag/app" title="View all posts in app">app</a>ly_custom_prefixes_to_title', 10, 2);
/*文章前缀申明结束-思牧分享资源网网-wp.magiclee.com*/
自定义css代码添加
自定义css
/*文章前缀申明开始-思牧分享资源网网-wp.magiclee.com*/
.ove_prefix, .ove_prefix1{
font-size: 12px;
padding: 0px 10px;
height: 20px;
color:#fff;
text-align: center;
border-radius: 7px 5px 8px 3px;
line-height: 20px;
margin-right: 5px;
clip-path: polygon(7% 0, 99% 0, 93% 100%, 0 100%);
}
.item-heading{
a{
display: flex;
}
}
.ove_prefix{ background: linear-gradient(135deg, #60e464 10%, #5cb85b 100%); }
.ove_prefix1{ background: linear-gradient(135deg, #59c3fb 10%, #268df7 100%); }
/*文章前缀申明结束-思牧分享资源网网-wp.magiclee.com*/
示例:加在主题自定义代码CSS中即可