0

Looking for assistance with WP_Query. I'm learning to create WordPress themes and I am practicing by creating a real estate theme. What I am trying to do is set up a properties page (custom post type) that displays all the properties from the custom post type.

I have that working fine. My issue is when you click a specific property to go to its single listing page (where it has further details), it's displaying all the properties from that custom post type starting with the most newly added property listing.

Here is my code:

<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>
3
  • 1
    Remove the custom query. Use the default loop. Look at one of the bundled themes how they constructed the single page loop – Pieter Goosen May 16 '15 at 7:43
  • Just figured it ou just then, came back to delete my question haha, Thanks for your reply! – Dylan Hemmens May 16 '15 at 7:58
  • No problem, enjoy :-) – Pieter Goosen May 16 '15 at 8:05
0

please modified yoy loop to display single post which you have coded in single.php file

<?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;
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.