使用 wp_query 查询单个帖子

Mil*_*ler 0 html php wordpress codex

我用 WP_query 循环检索了首页上的最新帖子,因为我发现这是最好的解决方案。但我想显示帖子被点击在首页。

单个帖子查询的最佳方法是什么?

我在 single.php 中再次使用了 wp_query(有没有更好的主意?)

$args =  array('name' => $the_slug,
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 1);
$the_query = new WP_Query( $args );
<?php while ( $the_query->have_posts() ) :?>

    <?php $the_query->the_post(); ?>
<p><?php $content = get_the_content('Read more');
print $content; ?></p>
<?php endwhile; ?><!-- end of the loop -->
<!-- put pagination functions here -->
    <?php wp_reset_postdata(); ?>
<?php else:  ?>

<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)

问题是它总是检索最新的帖子,即使我点击任何帖子 ID。我该如何解决这个问题?

Mil*_*ler 5

我找到了解决方案。

创建这个变量 $id= get_the_ID();

并添加 'p'=>$id to $args.