来自Post Slug或帖子标题的Echo Post ID

时间:2015-10-22 09:00:29

标签: php wordpress-plugin

我尝试根据帖子图块或slug获取帖子ID(对我来说不重要)。之后我想将ID添加到短代码中。

工作代码

<?php 
    $test123 = get_post(30);
    echo $test123->ID; /* this works and returns 30 */
?>

<?php 
echo do_shortcode("[shortcode id='{$test123->ID}']"); /* this also works */
?>

所以下一步是根据slug或title获取帖子ID。我该怎么做呢?我测试了不同的代码,但到目前为止一切都没有。

非常感谢您提前提供任何帮助!

2 个答案:

答案 0 :(得分:1)

通过slug使用url to postid()函数(documentation)来发布帖子:

$post_id = url_to_postid( $url );

要按标题发布,您可以使用get_page_by_title()函数(documentation):

$post = get_page_by_title( 'Your post title', OBJECT, 'post' );
$post_id = $post->ID;

答案 1 :(得分:0)

得到了解决方案

<?php 

 $homepage1 = url_to_postid('/here-is-my-custom-post-permalink/post-slug/');
 echo do_shortcode("[my_shortcode id='{$homepage1}']"); 

?>

如果是普通帖子,你可以跳过/ here-is-my-custom-post-permalink / part

相关问题