此处广告位出售

WordPress判断文章标题重复

WordPress 云上的幻象 10个月前 (06-10) 414次浏览 15个评论 扫描二维码

对于文章数多的博客,在写文章时,如果发布了重复标题的文章,是一件很尴尬的事情,那么下面我们通过一些代码来防止这个问题出现。
将下面这些代码,放到主题目录functions.php下即可。
[cc lang=”html”]
// 发表文章页面,前端抓取标题并使用AJAX发送请求
add_action( ‘admin_print_footer_scripts’, ‘duplicate_titles_enqueue_scripts’, 100 );
function duplicate_titles_enqueue_scripts() {
?>

posts WHERE post_status = ‘publish’ AND post_type = ‘post’
AND post_title = ‘{$title}’ AND ID != {$post_id} “;
$results = $wpdb->get_results($titles);
if($results) {
echo ““. _( ‘此标题已存在,请换一个标题!’ , ” ) .” “;
} else {
echo ‘‘._(‘恭喜,此标题未与其他文章标题重复!’ , ”).’‘;
}
die();
}

// 检测后端标题并且避免同名文章更新草稿
add_action( ‘publish_post’,’duplicate_titles_wallfa_bc’ ) ;
function duplicate_titles_wallfa_bc( $post ){
global $wpdb ;
$title = $_POST[‘post_title’] ;
$post_id = $post ;
$wtitles = “SELECT post_title FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_type = ‘post’
AND post_title = ‘{$title}’ AND ID != {$post_id} ” ;
$wresults = $wpdb->get_results( $wtitles ) ;
if ( $wresults ){
$wpdb->update( $wpdb->posts, array( ‘post_status’ =>
‘draft’ ), array( ‘ID’ => $post ) ) ;
$arr_params = array( ‘message’ => ’10’, ‘wallfaerror’ => ‘1’ ) ;
$location = add_query_arg( $arr_params , get_edit_post_link( $post , ‘url’ ) ) ;
wp_redirect( $location ) ;
exit ;
}
}

/// 文章提交更新后的提示
add_action( ‘admin_notices’, ‘not_published_error_notice’ );
function not_published_error_notice() {
if(isset($_GET[‘wallfaerror’]) == 1 ){
?>


七彩云博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress判断文章标题重复
喜欢 (0)
[[email protected]]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(15)个小伙伴在吐槽
  1. Kaiser Alcohol Rehab http://aaa-rehab.com Alcohol Rehab Near Me Drug Treatment Centers Near Me https://scout.wisc.edu/comment/36812#comment-36812
    Best Alcohol Rehab Centers2020-04-01 17:03 回复 Windows 7 | Chrome 67.0.3396.87
  2. Inpatient Drug Rehab Centers Near Me http://aaa-rehab.com Alcohol Rehab Near Me Free Alcohol Rehab Near Me https://www.sitiosbolivia.com/author/flightcancer9/
    Samhsa Alcohol2020-04-01 10:46 回复 Windows 7 | Chrome 67.0.3396.79
  3. Heroin Rehab Centers Near Me http://aaa-rehab.com Drug Rehab Centers Near Me Cms Methadone Clinic http://dizzelpower.ru/user/rehabcenters/
    Free Methadone Clinic2020-04-01 01:07 回复 Windows 10 | Chrome 68.0.3440.75
  4. Heroin Rehab Centers Near Me http://aaa-rehab.com Drug Rehab Centers Near Me Cms Methadone Clinic http://dizzelpower.ru/user/rehabcenters/
    Free Methadone Clinic2020-04-01 01:07 回复 Windows 10 | Chrome 68.0.3440.75
  5. Substance Abuse Iop http://aaa-rehab.com Drug Rehab Centers Near Me Court Ordered Rehab http://resourcedirectory.naturalresources-sf.com/author/jamesreern/
    Free Rehab Near Me2020-03-31 18:52 回复 Windows 10 | Chrome 64.0.3282.140
  6. Polysubstance Abuse Dsm 5 http://aaa-rehab.com Drug Rehab Centers 30 Day Drug Rehab Near Me https://www.google.co.ls/url?q=http://aaa-rehab.com
    Methadone Therapy2020-03-31 08:41 回复 Windows 7 | Chrome 67.0.3396.87
  7. Inpatient Drug Treatment Centers http://aaa-rehab.com Drug Rehab Prenatal Substance Abuse http://sqworl.com/5drr4f
    Free Alcohol Rehab Centers Near Me2020-03-31 01:48 回复 Windows 8.1 | Chrome 67.0.3396.87
  8. Heroin Rehab Centers Near Me http://aaa-rehab.com Drug Rehab Centers Alcohol Inpatient https://oilvoice.com/User/mikegala
    Outpatient Drug Rehab2020-03-30 16:47 回复 Windows 7 | Chrome 66.0.3359.181
  9. Bay Cove Methadone Clinic http://aaa-rehab.com Alcohol Rehab Near Me Drug & Alcohol Treatment Centers https://vyvozmusorascherbinka.ru/user/rehabcenters/
    Polysubstance Abuse Disorder2020-03-30 10:48 回复 Windows 10 | Chrome 68.0.3440.106
  10. Alcohol Inpatient http://aaa-rehab.com Alcohol Rehab Centers http://aaa-rehab.com Luxury Drug Rehab http://aaa-rehab.com
    Inpatient Substance Abuse2020-03-27 09:31 回复 Windows 7 | Chrome 66.0.3359.170
  11. Alcohol Recovery Programs Near Me http://aaa-rehab.com Alcohol Rehab Centers http://aaa-rehab.com Alcohol Rehab Centers http://aaa-rehab.com
    Drug Treatment Centers2020-03-26 20:17 回复 Windows 7 | Chrome 67.0.3396.99
  12. Pretty! This was an extremely wonderful article. Thank you for providing this information.
    xe88 player download link2020-03-21 05:05 回复 Windows 7 | Chrome 61.0.3163.31
  13. Rollex – 5Bintang Kasino http://www.5bintangkasino.com/rollex/ - Rollex – 5Bintang Kasino>>>
    RubenViags2020-01-15 11:47 回复 Windows 10 | Chrome 65.0.3325.181
  14. Mega888 – 5Bintang Kasino http://www.5bintangkasino.com/mega888/ - Click here...
    RubenViags2020-01-12 08:37 回复 Windows 8.1 | Chrome 67.0.3396.87
  15. Login 918Kiss – 5Bintang Kasino http://www.5bintangkasino.com/login-918kiss/ - Login 918Kiss – 5Bintang Kasino!..
    RubenViags2020-01-02 19:47 回复 Windows 10 | Chrome 64.0.3282.140