根据帖子数在循环内调用变量

我在wordpress主题中创建了一个简码, 需要在其工作正常的区域上显示属性。我尝试保留10个自定义标题的区域。为此, 我在wordpress循环中将varable称为$ area_title1到10, 我还添加了一个计数器, 并使用获取$ area_title1的变量调用了该计数器, 我将变量称为$ area_title。$ counter, 它没有计数就显示了其确切值示例1, 2等我的代码如下。

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="http://www.srcmini.com/< ?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="http://www.srcmini.com/< ?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="http://www.srcmini.com/< ?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>

#1这是你在php中的问题答案。
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="http://www.srcmini.com/< ?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="http://www.srcmini.com/< ?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="http://www.srcmini.com/< ?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>

【根据帖子数在循环内调用变量】我相信这对你有用。

    推荐阅读