Search code examples
wordpresswordpress-themingcustom-wordpress-pageswordpress-shortcode

Calling variable inside loop based on post count


I am creating a shortcode in a wordpress theme which needs to show properties on area based it was working fine. I have tried to keep 10 area title which was custom one. For that i called varable as $area_title1 to 10 in the wordpress loop also i have added a counter and called the counter with the variable for getting $area_title1 i called variable as $area_title.$counter which was without counting it shows exactly the value for example 1 , 2, etc my code is below.

function home_category_with_image_session($atts){
ob_start();
$atts = shortcode_atts(
array(
'propertytype' => '',
'area' => '',
'area1' => '',
'area2' => '',
'area3' => '',
'area4' => '',
'area5' => '',
'area6' => '',
'area7' => '',
'area8' => '',
'area9' => '',
'area_title' => '',
'area1_title' => '',
'area2_title' => '',
'area3_title' => '',
'area4_title' => '',
'area5_title' => '',
'area6_title' => '',
'area7_title' => '',
'area8_title' => '',
'area9_title' => '',
),
$atts,
    'home_category_type'
);
$area=$atts['area'];
$area1=$atts['area1'];
$area2=$atts['area2'];
$area3=$atts['area3'];
$area4=$atts['area4'];
$area5=$atts['area5'];
$area6=$atts['area6'];
$area7=$atts['area7'];
$area8=$atts['area8'];
$area9=$atts['area9'];
$area_title=$atts['area_title'];
$area_title1=$atts['area1_title'];
$area_title2=$atts['area2_title'];
$area_title3=$atts['area3_title'];
$area_title4=$atts['area4_title'];
$area_title5=$atts['area5_title'];
$area_title6=$atts['area6_title'];
$area_title7=$atts['area7_title'];
$area_title8=$atts['area8_title'];
$area_title9=$atts['area9_title'];
$property_type=$atts['propertytype'];
$custom_terms = get_terms(array ('taxonomy' => 'property_city_taxonomy',
            'orderby' => 'count',
            'order' => 'ASC',
            'name' => array ($area, $area1, $area2, $area3, $area4, $area5, $area6, $area7, $area8, $area9 ),
) );
$counter = -1;
foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => $property_type,
        'tax_query' => array(
            array(
                'taxonomy' => 'property_city_taxonomy',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );
$loop = new WP_Query($args);

if($loop->have_posts())
{ 
$counter++  ?>
<div class="col-md-3 col-sm-4 col-xs-6"><a href="<?php echo get_term_link($custom_term); ?>?property-type=<?php echo "$property_type"; ?>"><div class="top-locations"><div class="image-holder">
<?php $custom_term_id = $custom_term->term_id; 
$custom_term_meta = get_term_meta( $custom_term_id, 'uploadimage_61032', true );   
$num = $loop->post_count;
$area_title_display = $area_title.$counter; 
?>


<img src="<?php echo $custom_term_meta; ?>" alt="<?php echo $term->name; ?>" class="img-responsive" />
<?php echo $area_title_display; ?> 
<div class="home-banner-overlay"></div>
<div class="area-name"><?php echo $custom_term->name; ?></div>
</div><div class="city-text-container text-center"><?php echo $num; ?> Properties Listed</div></div></a></div>   
<?php       
} }
// Restore original post data.
wp_reset_postdata();
return ob_get_clean();  }
add_shortcode('home_category_type', 'home_category_with_image_session');

//Featured Post Home
function home_featured_posts($atts){
    ob_start();
    $atts = shortcode_atts(
        array(
            'no' => '',
            'posttype' => '',
        ),
        $atts,
        'home-featured'
    );
?>
<div class="session-featured-title">
<h5>Featured Property <?php echo $atts['posttype']; ?></h5>
<div class="related-control"><a class="btn prev"><i class="fa fa-arrow-left"></i></a><a class="btn next"><i class="fa fa-arrow-right"></i></a></div>
</div>
<div id="featured" class="owl-carousel owl-theme">
<?php
$args_1= array( 
'post_type' => $atts['posttype'],
'posts_per_page' => $atts['no'],
'meta_query' => array(
        array(
            'key' => 'we_recommend_make-featured-property',
            'value' => '1'
        )
    )
);
query_posts($args_1); if (have_posts()) : while (have_posts()) :  the_post(); ?>
<div class="item"><a href="<?php the_permalink(); ?> " title="<?php the_title_attribute(); ?>">
<div class="featured-image">    
<?php the_post_thumbnail('full', array('class' => 'img-responsive') ); ?></div>
<div class="featured-text-holder">
<div class="recent-post-title"><?php the_title(); ?></div>
<div class="featured-meta-detail">
<div class="py-list-price"><?php ro_price(); ?></div>
<div class="py-list-bed"><?php ro_bedroom(); ?></div>
<div class="py-list-bath"><?php ro_bathroom(); ?></div>
</div>
</div>
</a></div>
<?php endwhile;  endif; wp_reset_query(); ?>
</div>

Solution

  • Here is your questions answer in php Refer This And modified code of yours is below.

    function home_category_with_image_session($atts){
    ob_start();
    $atts = shortcode_atts(
    array(
    'propertytype' => '',
    'area' => '',
    'area1' => '',
    'area2' => '',
    'area3' => '',
    'area4' => '',
    'area5' => '',
    'area6' => '',
    'area7' => '',
    'area8' => '',
    'area9' => '',
    'area_title' => '',
    'area1_title' => '',
    'area2_title' => '',
    'area3_title' => '',
    'area4_title' => '',
    'area5_title' => '',
    'area6_title' => '',
    'area7_title' => '',
    'area8_title' => '',
    'area9_title' => '',
    ),
    $atts,
        'home_category_type'
    );
    $area=$atts['area'];
    $area1=$atts['area1'];
    $area2=$atts['area2'];
    $area3=$atts['area3'];
    $area4=$atts['area4'];
    $area5=$atts['area5'];
    $area6=$atts['area6'];
    $area7=$atts['area7'];
    $area8=$atts['area8'];
    $area9=$atts['area9'];
    $area_title[0]=$atts['area_title'];
    $area_title[1]=$atts['area1_title'];
    $area_title[2]=$atts['area2_title'];
    $area_title[3]=$atts['area3_title'];
    $area_title[4]=$atts['area4_title'];
    $area_title[5]=$atts['area5_title'];
    $area_title[6]=$atts['area6_title'];
    $area_title[7]=$atts['area7_title'];
    $area_title[8]=$atts['area8_title'];
    $area_title[9]=$atts['area9_title'];
    $property_type=$atts['propertytype'];
    $custom_terms = get_terms(array ('taxonomy' => 'property_city_taxonomy',
                'orderby' => 'count',
                'order' => 'ASC',
                'name' => array ($area, $area1, $area2, $area3, $area4, $area5, $area6, $area7, $area8, $area9 ),
    ) );
    $counter = -1;
    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => $property_type,
            'tax_query' => array(
                array(
                    'taxonomy' => 'property_city_taxonomy',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
         );
    $loop = new WP_Query($args);
    
    if($loop->have_posts())
    { 
    $counter++  ?>
    <div class="col-md-3 col-sm-4 col-xs-6"><a href="<?php echo get_term_link($custom_term); ?>?property-type=<?php echo "$property_type"; ?>"><div class="top-locations"><div class="image-holder">
    <?php $custom_term_id = $custom_term->term_id; 
    $custom_term_meta = get_term_meta( $custom_term_id, 'uploadimage_61032', true );   
    $num = $loop->post_count;
    $area_title_display = '$area_title'.$counter; 
    echo $area_title[$counter];
    ?>
    <img src="<?php echo $custom_term_meta; ?>" alt="<?php echo $term->name; ?>" class="img-responsive" />
    <?php echo $area_title_display; ?> 
    <div class="home-banner-overlay"></div>
    <div class="area-name"><?php echo $custom_term->name; ?></div>
    </div><div class="city-text-container text-center"><?php echo $num; ?> Properties Listed</div></div></a></div>   
    <?php       
    } 
    }
    // Restore original post data.
    wp_reset_postdata();
    return ob_get_clean();  }
    add_shortcode('home_category_type', 'home_category_with_image_session');
    
    //Featured Post Home
    function home_featured_posts($atts){
        ob_start();
        $atts = shortcode_atts(
            array(
                'no' => '',
                'posttype' => '',
            ),
            $atts,
            'home-featured'
        );
    ?>
    <div class="session-featured-title">
    <h5>Featured Property <?php echo $atts['posttype']; ?></h5>
    <div class="related-control"><a class="btn prev"><i class="fa fa-arrow-left"></i></a><a class="btn next"><i class="fa fa-arrow-right"></i></a></div>
    </div>
    <div id="featured" class="owl-carousel owl-theme">
    <?php
    $args_1= array( 
    'post_type' => $atts['posttype'],
    'posts_per_page' => $atts['no'],
    'meta_query' => array(
            array(
                'key' => 'we_recommend_make-featured-property',
                'value' => '1'
            )
        )
    );
    query_posts($args_1); if (have_posts()) : while (have_posts()) :  the_post(); ?>
    <div class="item"><a href="<?php the_permalink(); ?> " title="<?php the_title_attribute(); ?>">
    <div class="featured-image">    
    <?php the_post_thumbnail('full', array('class' => 'img-responsive') ); ?></div>
    <div class="featured-text-holder">
    <div class="recent-post-title"><?php the_title(); ?></div>
    <div class="featured-meta-detail">
    <div class="py-list-price"><?php ro_price(); ?></div>
    <div class="py-list-bed"><?php ro_bedroom(); ?></div>
    <div class="py-list-bath"><?php ro_bathroom(); ?></div>
    </div>
    </div>
    </a></div>
    <?php endwhile;  endif; wp_reset_query(); ?>
    </div>
    

    I am sure this will works for you.