在单个帖子类型中自定义帖子查询


Custom post query in single post type

我创建了一些自定义的帖子类型,比如作品、照片、教程。当我在主页和模板页面中查询时,它工作正常,但当我在singleworks.php中查询时所有帖子都重定向到最后添加的帖子。permalink显示正确的url,但当打开单个作品页面时,它会打开最后添加的帖子。这就是问题所在??我找不到任何问题

    <?php get_header(); ?>
<div class="page-section">
    <img src="<?php echo get_template_directory_uri();?>/images/banner.jpg" alt="" />
    <div class="page-section-text">
        <h2>Our Works</h2>
    </div>
</div>
<div class="blog-area padding fix">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
            <?php $wor= new WP_Query(array(
                'post_type' => 'works',
                'posts_per_page' => 1
            ));?>
            <?php if($wor->have_posts()):?>
            <?php while($wor->have_posts()): $wor->the_post(); ?>
                <div class="blog">
                    <div class="blog-image">
                        <?php the_post_thumbnail();?>
                    </div>
                    <div class="blog-text">
                        <h2 style="font-size:25px"><?php the_title(); ?></h2>
                        <?php the_content(); ?>
                    </div>
                </div>
            <?php endwhile;?>
            <?php else :?>
                <h3><?php _e('404 Error&#58; Not Found'); ?></h3>
            <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php get_footer(); ?>

它总是显示我添加的最后一个帖子。当我在template-works.php中点击permalink时,它会显示正确的链接which hover,但在点击任何帖子后,它总是转到最后添加的帖子。请帮帮我……

您不需要在singleworks.php 中编写任何自定义查询

<?php get_header(); ?>
<div id="primary" class="content-area texture">
  <?php while ( have_posts() ) : the_post(); ?>
        <?php the_title( '<h2 class="entry-title single-post-title">', '</h2>' ); ?>  
     <?php the_content(); ?>
  <?php endwhile; ?>
</div>

你也可以使用在循环时获得当前帖子的ID

 global $post;
 $post->ID;

或者使用get_the_ID();