单一帖子类型的自定义帖子查询

时间:2016-05-01 20:53:34

标签: php wordpress custom-post-type

我创建了一些自定义帖子类型,如作品,照片,教程。当我在我的主页和模板页面查询它工作正常,但当我在single-works.php查询所有发布重定向到最后添加的帖子。永久链接显示正确的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中查看永久链接时,它会显示悬停的正确链接,但在点击任何帖子后它总是转到最后添加的帖子。请帮帮我......

1 个答案:

答案 0 :(得分:0)

您不需要在single-works.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();