WP_Query在单个帖子页面上显示所有帖子

时间:2015-05-16 作者:Dylan Hemmens

正在寻求以下方面的帮助WP_Query. 我正在学习创建WordPress主题,并通过创建房地产主题进行练习。我想做的是设置一个属性页(自定义帖子类型),显示自定义帖子类型的所有属性。

我的工作很好。我的问题是,当您单击某个特定属性以转到其单一列表页面(其中有更多详细信息)时,它将显示该自定义帖子类型中的所有属性,从最新添加的属性列表开始。

这是我的代码:

<div class="container">
    <div class="row" id="primary">      
        <main id="content" class="col-sm-8" role="main">

            <?php $property_listing = new WP_Query(array(
                \'post_type\' => \'property\'
                    )); ?>

            <?php while($property_listing->have_posts()) : $property_listing->the_post(); ?>

            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><?php the_title(); ?></h3>
              </div>


              <div class="panel-body">
               <?php if ( has_post_thumbnail() ) { ?>
                <div class="listing-feature">

                    <?php 
                        the_post_thumbnail( \'large\', array( \'class\' => \'listing-feature\' ) ); ?>
                      </div>
                      <?php } ?>

                <div class="panel-price row">
                <div class="col-sm-4"><h2><?php the_field(\'property_price\'); ?></h2></div>
                    <div class="col-sm-8"><p class="pull-right"><strong>Suburb:</strong> <?php the_field(\'property_location\'); ?> <strong>&#124;</strong> <strong>Type:</strong> <?php the_field(\'property_type\'); ?></p></div>
                </div> <!-- /row --> 
                <hr id="customhr" />
                <div class="listing-details row">
                <div class="col-sm-6">
                    <p><strong>Listing ID:</strong> <?php the_field(\'listing_id\'); ?></p>
                    <p><strong>Land Size:</strong> <?php the_field(\'land_size\'); ?></p>
                    <p><strong>Construction:</strong> <?php the_field(\'construction\'); ?></p>
                </div>
                 <div class="col-sm-6">
                    <p><strong>Bedrooms:</strong> <?php the_field(\'bedrooms\'); ?></p>
                    <p><strong>Bathrooms:</strong> <?php the_field(\'bathrooms\'); ?></p>
                    <p><strong>Car Spots:</strong> <?php the_field(\'car_spots\'); ?></p>
                </div>
                </div> <!-- /row --> 
                <hr id="customhr" />

                <div class="row">
                    <div class="gallery">
                       <h3>Gallery:</h3>

<?php 
$images = get_field(\'gallery_images\');
if( $images ): ?>


<?php foreach( $images as $image ): ?>

       <div class="item-img col-md-3">
        <a href="<?php echo $image[\'url\']; ?>" target="_blank">
             <img class="gallery-img img-thumbnail" src="<?php echo $image[\'sizes\'][\'thumbnail\']; ?>" alt="<?php echo $image[\'alt\']; ?>" />
           </a></div>

<?php endforeach; ?>

<?php endif; ?>

             </div> <!-- /gallery -->
                </div> <!-- /row -->
                <hr id="customhr" />

                <div class="row">
                   <div class="listing-description">
                       <h3>Description</h3>
                       <p><?php the_field(\'property_description\'); ?></p>

                   </div> <!-- /listing-description -->
                </div> <!-- /row -->
                <hr id="customhr" />

                <div class="row">
                    <div class="listing-features">
                      <h3>Additional Features:</h3>
                       <?php
                        // check if the repeater field has rows of data
                        if( have_rows(\'additional_features_main\') ):

                            // loop through the rows of data
                            while ( have_rows(\'additional_features_main\') ) : the_row(); ?>

                        <strong class="feature-tags"><?php the_sub_field(\'additional_features\'); ?></strong>

                        <?php
                            endwhile;
                                else :
                                    // no rows found
                                endif;
                                ?>

                    </div><!-- /listing-features -->
                </div> <!-- /row -->
                <hr id="customhr" />

                <div class="row">
                    <div class="listing-location">
                        <h3>Location:</h3>

                        <?php 

                            $location = get_field(\'map_location\');

                            if( !empty($location) ):
                            ?>
                            <div class="acf-map">
                                <div class="marker" data-lat="<?php echo $location[\'lat\']; ?>" data-lng="<?php echo $location[\'lng\']; ?>">
                                    </div>
                            </div>
                            <?php endif; ?>

                        <div class="listing-address">
                            <?php the_field(\'map_location\'); ?>
                        </div>

                    </div> <!-- /listing-location -->
                </div> <!-- /row -->
              </div>
            </div> 
            <?php wp_reset_query(); ?>
            <?php wp_reset_postdata(); ?>
            <?php endwhile; ?>

        </main>

1 个回复
SO网友:Anand

请修改yoy循环以显示您在single中编码的single post。php文件

<?php
        // Start the loop.
        while ( have_posts() ) : the_post();

        /*
         * Include the post format-specific template for the content. If you want to
         * use this in a child theme, then include a file called called content-___.php
         * (where ___ is the post format) and that will be used instead.
         */
        get_template_part( \'content\', get_post_format() );

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) :
            comments_template();
        endif;

        // Previous/next post navigation.
        the_post_navigation( array(
            \'next_text\' => \'<span class="meta-nav" aria-hidden="true">\' . __( \'Next\', \'twentyfifteen\' ) . \'</span> \' .
                \'<span class="screen-reader-text">\' . __( \'Next post:\', \'twentyfifteen\' ) . \'</span> \' .
                \'<span class="post-title">%title</span>\',
            \'prev_text\' => \'<span class="meta-nav" aria-hidden="true">\' . __( \'Previous\', \'twentyfifteen\' ) . \'</span> \' .
                \'<span class="screen-reader-text">\' . __( \'Previous post:\', \'twentyfifteen\' ) . \'</span> \' .
                \'<span class="post-title">%title</span>\',
        ) );

    // End the loop.
    endwhile;
    ?>

结束

相关推荐

自定义POST类型Single(固定链接)引发404错误

我正在一个安装了Woocommerce的网站上工作(如果我是对的话,也是一个CPT,但这个很好用)。此网站有一个自定义职位类型,此cpt有通过其永久链接链接到网站中的职位。post类型在管理面板中运行良好,但仅此而已。当我点击wordpress管理面板中的“查看工作机会”按钮或通过permalink在前端时,它会抛出一个意外的404错误。我在Google上做了一些研究,并尝试通过管理面板和flush_rewrite_rules() 作用我还仔细检查了注册帖子类型的方式,甚至尝试禁用任何重写。我尝试的另一件