这是一款子比主题的新版自定义文章前缀,这是代码版本,文章编辑页面可选择默认自带的六个前缀图标,也可以文字、自定义上传图标,喜欢的自行部署吧!
22
代码部署教程
定位:/wp-content/themes/zibll/func.php文件,没有这个文件自己创建一个,记得加上php头,要不然会报错,将下面的代码丢里面即可!
// 新版自定义文章前缀功能
function dearlicy_prefix_images()
{
return array(
'shice' => 'https://img.alicdn.com/imgextra/i4/2210123621994/O1CN01FK4hQd1QbItM1FCfT_!!2210123621994.webp',
'dujia' => 'https://img.alicdn.com/imgextra/i1/2210123621994/O1CN01skvHXt1QbItQzaqUp_!!2210123621994.webp',
'shoufa' => 'https://img.alicdn.com/imgextra/i1/2210123621994/O1CN01Qpy6tJ1QbItQmw1iW_!!2210123621994.webp',
'temai' => 'https://img.alicdn.com/imgextra/i1/2210123621994/O1CN01o6UWba1QbItQuKH58_!!2210123621994.webp',
'miaosha' => 'https://img.alicdn.com/imgextra/i1/2210123621994/O1CN01rec2MG1QbItQNN19F_!!2210123621994.webp',
'baiyibutie' => 'https://img.alicdn.com/imgextra/i3/2210123621994/O1CN01kvk4c41QbItQr7Vnm_!!2210123621994.webp',
);
}
if (!class_exists('DearLicy_Module')) {
class DearLicy_Module
{
public static function DearLicy_imgtitle($images = array())
{
return array_merge($images, dearlicy_prefix_images());
}
}
}
if (class_exists('CSF')) {
CSF::createMetabox('DearLicy_titles', array(
'title' => '标题前缀',
'post_type' => 'post',
'context' => 'advanced',
'data_type' => 'unserialize',
'priority' => 'high',
));
CSF::createSection('DearLicy_titles', array(
'fields' => array(
array(
'id' => 'titles_moshi',
'type' => 'radio',
'title' => '模式选择',
'desc' => '图片模式支持多选预设前缀,也可选择自定义图片前缀',
'inline' => true,
'options' => array(
'img' => '图片',
'text' => '文字',
),
'default' => 'img',
),
array(
'id' => 'text',
'type' => 'text',
'title' => '文字模式',
'desc' => '建议两个字',
'dependency' => array('titles_moshi', '==', 'text'),
),
array(
'id' => 'text_bg_color',
'type' => 'palette',
'title' => '背景颜色',
'desc' => '部分颜色带有文字颜色,其余默认白色',
'class' => 'compact skin-color',
'default' => 'jb-vip2',
'options' => class_exists('CFS_Module') ? CFS_Module::zib_palette(array(), array('jb')) : array(),
'dependency' => array('titles_moshi', '==', 'text'),
),
array(
'id' => 'img',
'type' => 'image_select',
'title' => '选择预设图片前缀',
'desc' => '可同时选择多个图片前缀,前台按预设排列顺序显示',
'multiple' => true,
'options' => dearlicy_prefix_images(),
'dependency' => array('titles_moshi', '==', 'img'),
),
array(
'id' => 'custom_img_prefixes',
'type' => 'gallery',
'title' => '自定义图片前缀',
'desc' => '从媒体库选择多张图片作为标题前缀',
'add_title' => '选择前缀图片',
'edit_title' => '编辑前缀图片',
'clear_title' => '清空前缀图片',
'dependency' => array('titles_moshi', '==', 'img'),
),
),
));
}
function dearlicy_get_selected_prefix_keys($value)
{
if (empty($value)) {
return array();
}
$keys = is_array($value) ? $value : array($value);
$keys = array_map('sanitize_key', $keys);
return array_values(array_intersect(array_keys(dearlicy_prefix_images()), $keys));
}
function dearlicy_get_custom_prefix_urls($value)
{
if (empty($value)) {
return array();
}
$urls = array();
foreach ((is_array($value) ? $value : explode(',', $value)) as $item) {
$attachment_id = is_array($item) ? absint($item['id'] ?? 0) : absint($item);
$url = $attachment_id ? wp_get_attachment_image_url($attachment_id, 'full') : '';
if (!$url && is_array($item) && !empty($item['url'])) {
$url = $item['url'];
}
if (!$url && is_string($item) && filter_var($item, FILTER_VALIDATE_URL)) {
$url = $item;
}
if ($url) {
$urls[] = esc_url_raw($url);
}
}
return array_values(array_filter(array_unique($urls)));
}
function dearlicy_prefix_img($url, $alt = 'prefix')
{
return $url ? '<img class="DearLicy_prefix_img" src="' . esc_url($url) . '" alt="' . esc_attr($alt) . '" style="height:20px;pointer-events:none;margin-right:3px;vertical-align:-3px;"/>' : '';
}
function dearlicy_title_prefix_html($post_id)
{
if (get_post_meta($post_id, 'titles_moshi', true) === 'text') {
$text = get_post_meta($post_id, 'text', true);
$color = get_post_meta($post_id, 'text_bg_color', true);
return $text === '' ? '' : '<span class="DearLicy_prefix ' . esc_attr($color) . '">' . esc_html($text) . '</span> ';
}
$html = '';
$images = dearlicy_prefix_images();
$selected = dearlicy_get_selected_prefix_keys(get_post_meta($post_id, 'img', true));
foreach ($selected as $key) {
$html .= dearlicy_prefix_img($images[$key], $key);
}
foreach (dearlicy_get_custom_prefix_urls(get_post_meta($post_id, 'custom_img_prefixes', true)) as $index => $url) {
$html .= dearlicy_prefix_img($url, 'custom-prefix-' . ($index + 1));
}
return $html;
}
function dearlicy_is_main_posts_list_title($post_id)
{
global $wp_query;
if (!$wp_query instanceof WP_Query || !$wp_query->in_the_loop || empty($wp_query->post->ID)) {
return false;
}
if ((int) $wp_query->post->ID !== (int) $post_id) {
return false;
}
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 8) as $trace) {
if (!empty($trace['function']) && $trace['function'] === 'zib_get_posts_list_title') {
return true;
}
}
return false;
}
function dearlicy_should_apply_title_prefix($post_id)
{
if (!$post_id || is_admin() || is_singular() || is_feed() || get_post_type($post_id) !== 'post') {
return false;
}
return dearlicy_is_main_posts_list_title($post_id);
}
function apply_dearlicy_prefixes_to_title($title, $id = null)
{
if (!dearlicy_should_apply_title_prefix($id)) {
return $title;
}
$prefix = dearlicy_title_prefix_html($id);
return $prefix ? $prefix . $title : $title;
}
add_filter('the_title', 'apply_dearlicy_prefixes_to_title', 10, 2);
function dearlicy_title_prefix_admin_style()
{
$screen = function_exists('get_current_screen') ? get_current_screen() : null;
if (!$screen || $screen->post_type !== 'post') {
return;
}
?>
<style>
#DearLicy_titles .csf-field-image_select .csf--image {
margin: 0 8px 8px 0;
vertical-align: top;
}
#DearLicy_titles .csf-field-image_select figure {
width: 112px;
height: 42px;
display: inline-flex;
align-items: center;
justify-content: center;
padding: 6px 8px;
box-sizing: border-box;
border-radius: 4px;
}
#DearLicy_titles .csf-field-image_select img {
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
display: block;
}
</style>
<?php
}
add_action('admin_head-post.php', 'dearlicy_title_prefix_admin_style');
add_action('admin_head-post-new.php', 'dearlicy_title_prefix_admin_style');
CSS代码
定位:子比主题–>>自定义CSS样式
.DearLicy_prefix{
position: relative;
overflow: hidden;
display: inline-flex;
align-items: center;
border-radius: 5px;
padding: 5px 4px;
margin-right: 3px;
height: 19px;
font-size: 12px;
top: -3px;
clip-path: polygon(7% 0, 99% 0, 93% 100%, 0 100%);
}
.DearLicy_prefix:after, .DearLicy_prefix:after {
position: absolute;
content: " ";
display: block;
left: -100%;
top: -5px;
width: 15px;
height: 145%;
background-image: linear-gradient(90deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0));
animation: sweepTitle 3s ease-in-out infinite;
transform: rotate(28deg);
}
@keyframes sweepTitle {
0% {
left: -100%
}
100% {
left: 100%
}
}
本站代码模板仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!

评论(0)