2014-01-30 22 views
0

如何檢查我的WordPress數據庫中是否存在彈頭?WordPress - 檢查彈性可用性

我想檢查任何蛞蝓(郵局,頁面,分類和自定義後類型/分類)

感謝

+2

使用'wp_unique_post_slug()'爲獨特的slu ... ... 如果您的slu is存在,此功能會給你帶有後綴(-2,-3,-4 ...)的新slu ... ... –

+1

Darn !我發佈後立即看到了這一點。 – larsemil

+0

@AkshayPaghdar它會檢查所有slu((頁面,cpt ...)或只發布slu?? thx – Fredmat

回答

1

我只是回答了這個在這裏:https://wordpress.stackexchange.com/questions/25940/how-to-check-if-a-slug-exists/144439#144439

不知道什麼對重複的答案的政策,但在這裏你去:

function the_slug_exists($post_name) { 
    global $wpdb; 
    if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) { 
     return true; 
    } else { 
     return false; 
    } 
} 

然後,您可以使用它像這樣:

if (the_slug_exists('contact')) { 
      // do something 
    } 

用你想測試的任何slu Replace替換「contact」。

+0

僅適用於wp_前綴未更改的默認安裝,因爲您應該有。這將工作:'if($ wpdb-> get_row(「SELECT post_name FROM」。$ wpdb-> prefix。「posts WHERE post_name ='」。$ post_name。「'」,'ARRAY_A')){...' –

0
<?php 
    $url = $_SERVER["REQUEST_URI"]; 
    $isItYourSlug = strpos($url, 'your_slug'); 

    if ($isItYourSlug!==false) { 
     Do Something 
    } 
?> 

好嗎?它適合我!