WordPress查询单个帖子由鼻涕虫

2022-08-30 07:45:39

当我想在不使用循环的情况下显示单个帖子时,我使用这个:

<?php
$post_id = 54;
$queried_post = get_post($post_id);
echo $queried_post->post_title; ?>

问题是,当我移动网站时,ID通常会发生变化。有没有办法通过鼻涕虫查询这篇文章?


答案 1

来自WordPress Codex:

<?php
$the_slug = 'my_slug';
$args = array(
  'name'        => $the_slug,
  'post_type'   => 'post',
  'post_status' => 'publish',
  'numberposts' => 1
);
$my_posts = get_posts($args);
if( $my_posts ) :
  echo 'ID on the first post found ' . $my_posts[0]->ID;
endif;
?>

WordPress Codex 获取帖子


答案 2

怎么样?

<?php
   $queried_post = get_page_by_path('my_slug',OBJECT,'post');
?>

推荐