文章最后更新时间:2025-03-25 14:13:39
此为初一小盏用户定制小功能,单独控制文章版权是否显示
教程
将代码放置于主题目录下的func.php内
本文付费阅读内容 – 月度会员免费
// 文章版权
function add_DearLicy_copyright_meta_box() {
add_meta_box(
\’DearLicy_copyright\’,
\’版权开关\’,
\’DearLicy_copyright_html\’,
\’post\’,
\’normal\’,
\’high\’
);
}
add_action(\’admin_menu\’, \’add_DearLicy_copyright_meta_box\’);
// 输出复选框的 HTML
function DearLicy_copyright_html($post) {
$checked = get_post_meta($post->ID, \’_DearLicy_copyright_checked\’, true) ? \’checked=\”checked\”\’ : \’\’;
echo \'<label><input type=\”checkbox\” name=\”DearLicy_copyright_checked\” value=\”1\” \’ . $checked . \’ /> 关闭该文章底部版权</label>\’;
}
// 保存复选框的值
function save_DearLicy_copyright_meta($post_id) {
if (defined(\’DOING_AUTOSAVE\’) && DOING_AUTOSAVE) return; // 防止在自动保存时执行
if (!isset($_POST[\’DearLicy_copyright_checked\’])) return; // 如果复选框没有被提交,则返回
$checked = isset($_POST[\’DearLicy_copyright_checked\’]) && $_POST[\’DearLicy_copyright_checked\’] == 1 ? true : false;
update_post_meta($post_id, \’_DearLicy_copyright_checked\’, $checked);
}
add_action(\’save_post\’, \’save_DearLicy_copyright_meta\’);
// 在前端加载自定义 CSS
function load_custom_css() {
global $post;
if (!is_singular(\’post\’)) return; // 确保只在文章页面加载
$checked = get_post_meta($post->ID, \’_DearLicy_copyright_checked\’, true);
if (!$checked) return; // 如果复选框没有被勾选,则返回
echo \'<style type=\”text/css\”>\’;
echo \’.em09.muted-3-color{display:none;}\’;
echo \'</style>\’;
}
add_action(\’wp_footer\’, \’load_custom_css\’);
暂无评论内容