2017-05-31 105 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

这不是我所期望的。我需要通过使用职位名称的职位编号,并检查类别名称。 –

相关问题