计算wordpress帖子的数量并回应它

时间:2018-05-30 08:49:10

标签: php wordpress

我创建了一些面包屑,告诉用户已经应用了哪些过滤器以及显示了多少帖子。我的问题是我试图在声明变量之前抓取变量,这是因为面包屑位于post循环之上。这是相关代码,向您展示我的意思;

这是我的$ post_counter变量的后循环

<section id="properties">
    <div class="container">
        <div class="row">
            <div class="col">
                <div class="row">
                    <?php 
                        if($_GET['min_price'] && !empty($_GET['min_price'])){
                            $min_price = $_GET['min_price'];
                        }else{
                            $min_price = 0;
                        }

                        if($_GET['max_price'] && !empty($_GET['max_price'])){
                            $max_price = $_GET['max_price'];
                        }else{
                            $max_price = 10000000;
                        }

                        if($_GET['min_beds'] && !empty($_GET['min_beds'])){
                            $min_beds = $_GET['min_beds'];
                        }else{
                            $min_beds = '1'; 
                        }

                        if($_GET['max_beds'] && !empty($_GET['max_beds'])){
                            $max_beds = $_GET['max_beds'];
                        }else{
                            $max_beds = '6+'; 
                        }

                        if($_GET['location'] && !empty($_GET['location'])){
                            $location = $_GET['location'];
                            $location_val = stripslashes($location);
                        }

                        if($_GET['type'] && !empty($_GET['type'])){
                            $type = $_GET['type'];
                        }else{
                            $type = array("Detached", "Semi-Detached", "Terraced", "End of Terrace", "Apartment", "Bungalow", "Commercial");
                        }

                        if($_GET['flash_type'] && !empty($_GET['flash_type'])){
                            $flash_type = $_GET['flash_type'];
                        }else{
                            $flash_type = array("Ideal First Time Buy", "Ideal Investment", "Under Offer", "Nothing Selected");
                        }

                    $posts = get_posts(array(
                        'posts_per_page'    =>  -1,
                        'post_type'         =>  'property',
                        'orderby'           =>  'date',
                        'meta_query'        =>  array(
                            'relation'  => 'AND',
                            array(
                                'key'       => 'property_status',
                                'value'     => 'For Sale'
                            ),

                            array(
                                'key'       => 'property_price',
                                'type'      => 'NUMERIC',
                                'value'     => array($min_price, $max_price),
                                'compare'   => 'BETWEEN'
                            ),

                            array(
                                'key'       => 'bedrooms',
                                'value'     => array($min_beds, $max_beds),
                                'compare'   => 'BETWEEN'
                            ),

                            array(
                                'relation'  => 'OR',
                                array(
                                    'key'       => 'street',
                                    'value'     => $location_val,
                                    'compare'   => 'LIKE'
                                ),

                                array(
                                    'key'       => 'town',
                                    'value'     => $location_val,
                                    'compare'   => 'LIKE'
                                ),

                                array(
                                    'key'       => 'county',
                                    'value'     => $location_val,
                                    'compare'   => 'LIKE'
                                ),

                                array(
                                    'key'       => 'postcode',
                                    'value'     => $location_val,
                                    'compare'   => 'LIKE'
                                )
                            ),

                            array(
                                'key'       => 'type_of_property',
                                'value'     => $type,
                                'compare'   => 'IN'
                            ),

                            array(
                                'key'       => 'optional_category',
                                'value'     => $flash_type,
                                'compare'   => 'IN'
                            ),
                        )
                    ));

                    $post_counter = 0;

                    if( $posts ):?> 

                        <?php foreach( $posts as $post ): 
                            setup_postdata( $post );
                            $post_counter = $post_counter + 1;
                        ?>
                            <div class="col-sm-6 col-md-4 col-lg-3">
                                <div class="shadowwrapper2">
                                <a href="<?php the_permalink(); ?>">
                                    <?php  
                                        $main_field = get_field('images');
                                        $first_row = $main_field[0];
                                        $img = $first_row['image'];
                                        $img_med = $img['sizes']['medium'];
                                    ?>

                                    <div class="propertywrapper">
                                        <img class="img-fluid gal_imgs" src="<?php echo $img_med ?>" alt="<?php $img['alt']; ?>"/>

                                        <?php $secondary_flash = get_field('optional_category'); ?>

                                        <?php if($secondary_flash == "Ideal First Time Buy"): ?>
                                            <span class="second_flash">Ideal First Time Buy</span>
                                        <?php elseif($secondary_flash == "Ideal Investment"): ?>
                                            <span class="second_flash">Ideal Investment</span>
                                        <?php elseif($secondary_flash == "Under offer"): ?>
                                            <span class="second_flash">Under offer</span>
                                        <?php endif; ?>
                                    </div>

                                    <div class="cornerflash">
                                        <img src="<?php bloginfo('template_directory'); ?>/imgs/forsale.svg" alt="corner flash">
                                    </div>

                                    <div class="propertyinfo">
                                        <div class="row m-0">
                                            <div class="col-6 p-0 mt-2"><?php the_field('type_of_property'); ?></div>
                                            <div class="col-6 p-0 mt-2"><?php the_field('bedrooms'); ?> bedrooms</div>
                                        </div>
                                    </div>

                                    <div class="streetpricewrapper">
                                        <p class="streetname">
                                            <?php the_field('street'); ?>, <?php the_field('town'); ?>
                                        </p>
                                        <p class="price">
                                            £<?php the_field('property_price'); ?>
                                        </p>    
                                    </div>                              
                                </a>
                            </div>
                        </div>

                        <?php endforeach; ?>

                        <?php wp_reset_postdata(); ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</section>

这是我的面包屑这个部分位于我页面上的post循环之上。这是我试图检索$ post_counter变量的地方;

<div class="container">
    <div class="row">
        <div class="col">
            <div id="crumb">
                <p>Now showing <?php echo $post_counter; ?> properties</p>

                <?php 
                    // check if property type has been selected if so loop through them and display them
                    if( $_GET['type'] ){
                        $crumb_type = $_GET['type'];

                        foreach($crumb_type as $val) {
                            echo '<span>'.$val.', </span>';
                        }
                    }

                    // check if min beds / max beds has been select if so display it in the crumb
                    if( $_GET['min_beds'] ){
                        $crumb_minbeds = $_GET['min_beds'];

                        echo '<p>from </p><span>'.$crumb_minbeds.' bedrooms, </span>';
                    }

                    if( $_GET['max_beds']){
                        $crumb_maxbeds = $_GET['max_beds'];

                        echo '<p>up to </p><span>'.$crumb_maxbeds.' bedrooms, </span>';
                    }

                    // check if min price / max price has been select if so display it in the crumb
                    if( $_GET['min_price'] ){
                        $crumb_minprice = $_GET['min_price'];

                        echo '<p>from </p><span>£'.$crumb_minprice.' </span>';
                    }

                    if( $_GET['max_price']){
                        $crumb_maxprice = $_GET['max_price'];

                        echo '<p>up to </p><span>£'.$crumb_maxprice.' </span>';
                    }

                    // check if location is set 
                    if( $_GET['location'] ){
                        $crumb_location = $_GET['location'];

                        echo '<p>properties in </p><span>'.stripslashes($crumb_location).'</span>';
                    }

                    // check if secondary filters are applied, if so display a clickable box
                    if( $_GET['flash_type'] ): ?>
                        <div id="flash_crumbs">
                            <?php 
                            if( in_array('Ideal First Time Buy', $_GET['flash_type']) ): ?>
                                <a href="javascript:;">Ideal First Time Buy <span data-id="first_time" >&times;</span></a>
                            <?php endif; 

                            if( in_array('Ideal Investment', $_GET['flash_type']) ): ?>
                                <a href="javascript:;">Ideal Investment <span data-id="investment" >&times;</span></a>
                            <?php endif; 

                            if( in_array('Under Offer', $_GET['flash_type']) ): ?>
                                <a href="javascript:;">Under Offer <span data-id="under_offer" >&times;</span></a>
                            <?php endif; ?>
                        </div>
                    <?php endif;
                ?>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

在显示之前处理所有内容。通过这种方式,您可以将代码中的问题分开来看起来更好。

将您的$posts = get_posts(...)移到php标记中的header.php内,然后在breadcrumb中写一下:

<p>Now showing <?php echo count($posts); ?> properties</p>

PHP Count

通常,在显示数据之前处理数据会更好。