2017-05-31 133 views
0

如何通過在WordPress中使用帖子名稱和類別名稱來獲取帖子ID?如何通過在WordPress中使用帖子名稱和類別名稱來獲取帖子ID?

我有使用此代碼,但它不幫助我。

$post_slug = get_post_field($post_name, get_post()); 
$args = array(
'name'  => $post_slug, 
'category_name' => $pcat_name, 
'post_type' => 'post', 
'post_status' => 'publish', 
'numberposts' => 1 
); 
$my_posts = get_posts($args); 
if($my_posts) : 
$rid=$my_posts[0]->ID; 
endif; 

slu is是不行的。

我想檢查郵政名稱和它是必須的類別名稱。

回答

0

您可以使用get_page_by_title()(https://codex.wordpress.org/Function_Reference/get_page_by_title),以獲得職位或任何其他類型後:

$posts = get_page_by_title($post_name, OBJECT, 'post'); 
foreach($posts as $post){ 
    if(in_category($pcat_name, $post->ID)){ 
     echo $post->ID; 
    }; 
}; 
+1

這不是我所期望的。我需要通過使用職位名稱的職位編號,並檢查類別名稱。 –

相關問題