默认情况下,如果发布文章时没有选择分类,文章就会被自动归类到 后台 > 设置 > 撰写 设置的“默认文章分类目录”:

很多用户在后台发布文章,常常会忘记选择分类,所以很有必要添加一个提醒功能,如果没有选择分类,点击发布时,就显示一个提示信息。要实现这个功能,只要将下面的代码添加到主题的 functions.php 即可:
/**
* WordPress 发布文章前必须选择分类
* https://www.wpdaxue.com/choose-a-category-before-publish.html
*/
add_action('admin_footer-post.php', 'choose_a_category_before_publish');
add_action('admin_footer-post-new.php', 'choose_a_category_before_publish');
function choose_a_category_before_publish(){
global $post_type;
if($post_type=='post'){
echo "<script>
jQuery(function($){
$('#publish, #save-post').click(function(e){
if($('#taxonomy-category input:checked').length==0){
alert('抱歉,发布文章前,请选择一个分类');
e.stopImmediatePropagation();
return false;
}else{
return true;
}
});
var publish_click_events = $('#publish').data('events').click;
if(publish_click_events){
if(publish_click_events.length>1){
publish_click_events.unshift(publish_click_events.pop());
}
}
if($('#save-post').data('events') != null){
var save_click_events = $('#save-post').data('events').click;
if(save_click_events){
if(save_click_events.length>1){
save_click_events.unshift(save_click_events.pop());
}
}
}
});
</script>";
}
} |
/**
* WordPress 发布文章前必须选择分类
* https://www.wpdaxue.com/choose-a-category-before-publish.html
*/
add_action('admin_footer-post.php', 'choose_a_category_before_publish');
add_action('admin_footer-post-new.php', 'choose_a_category_before_publish');
function choose_a_category_before_publish(){
global $post_type;
if($post_type=='post'){
echo "<script>
jQuery(function($){
$('#publish, #save-post').click(function(e){
if($('#taxonomy-category input:checked').length==0){
alert('抱歉,发布文章前,请选择一个分类');
e.stopImmediatePropagation();
return false;
}else{
return true;
}
});
var publish_click_events = $('#publish').data('events').click;
if(publish_click_events){
if(publish_click_events.length>1){
publish_click_events.unshift(publish_click_events.pop());
}
}
if($('#save-post').data('events') != null){
var save_click_events = $('#save-post').data('events').click;
if(save_click_events){
if(save_click_events.length>1){
save_click_events.unshift(save_click_events.pop());
}
}
}
});
</script>";
}
}
以上代码默认只支持文章(post),见第 9 行的 if($post_type==’post’) 就是判读。
来源:
https://www.wpdaxue.com/choose-a-category-before-publish.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32425.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57