飞流 发表于 2025-10-8 22:27:34

子比主题 – 文章版权单独设置开关

这是一篇文章版权的设置开关的功能,这个功能就是非常实用的了,就是如果你发布文章的时候是否开启这个文章的文章版权那么开了这个就有了这个文章版权,喜欢的自行部署!




代码部署

将下面的代码放到:/wp-content/themes/zibll/func.php文件,没有这个文件自己创一个,记得加上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');

页: [1]
查看完整版本: 子比主题 – 文章版权单独设置开关