Wordpress - 在短代码变量中包含PHP

时间:2016-03-08 22:02:34

标签: php wordpress

我正在尝试创建一个短代码来复制以下工作正常: -

$currency = get_field('currency', 'options');

$args = array(
    'posts_per_page'=> -1,
    'post_type'     => 'properties',

);
$featured_query = new WP_Query( $args ); ?>

        <!-- Featured Properties -->
        <h1 class="text-grey"><span class="xstrong">Featured</span> Properties</h1>

        <div id="featured-properties">         

            <div class="container">

                <?php if( $featured_query->have_posts() ): $property_increment = 0; ?> 

                    <div id="featured-carousel" class="carousel slide" data-ride="carousel">

                        <!-- Wrapper for slides -->
                        <div class="carousel-inner row" role="listbox">

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

                                <?php if($property_increment==0 || $property_increment%3==0 ){ echo '<div class="item '.(($property_increment==0)?'active':'').'">'; } ?>

                                <div class="col-md-4 col-sm-6 col-xs-12 row">
                                    <div class="property-wrapper">
                                        <a href="<?php echo get_permalink(); ?>">
                                        <div class="corner"></div><p class="corner-text"><?php the_field('house_status'); ?></p>
                                        <?php the_post_thumbnail($featured_query->ID, ''); ?>
                                            <div class="property-details">
                                                <h5 class="property-title"><?php the_title(); ?>,</h5>
                                                <h6 class="property-title"><?php the_field('house_town'); ?></h6>
                                                <h6 class="property-type"><?php the_field('house_type'); ?></h6>
                                                <?php $price = number_format(get_field('house_price')); ?>
                                                <h5 class="property-price"><?php echo $currency . $price; ?></h5>
                                                <h5 class="property-bedrooms"><?php the_field('house_bedrooms'); ?><span class="icon-bedrooms"></span></h5>
                                                <h5 class="property-bathrooms"><?php the_field('house_bathrooms'); ?><span class="icon-bathrooms"></span></h5>
                                                <span class="btn btn-white full">More Details</span>
                                            </div>
                                        </a>
                                    </div>
                               </div>

                                <?php echo ($property_increment%3==2)?'</div>':''; ?>
                            <?php $property_increment++; endwhile; ?>

                        </div>

                        <!-- Left and right controls -->
                        <a class="left carousel-control" href="#featured-carousel" role="button" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="right carousel-control" href="#featured-carousel" role="button" data-slide="next">
                            <span class=" glyphicon-chevron-right" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>

                    </div>
                <?php endif; wp_reset_query(); ?>

            </div>

        </div><!-- / Featured Properties -->

我的问题是,当我尝试将其创建为短代码时,你不能使用echo,如果,在变量中,我认为它现在几乎就在那里,我只是不知道我需要改变什么才能得到它例如,当我在变量中使用它时,我该怎么做取代'while'和'endif'?

以下是我到目前为止: -

/**
 * Shortcode: Latest Properties
 */

function latest_content_properties_func( $atts ) {

$currency = get_field('currency', 'options');

$args = array(
    'posts_per_page'=> -1,
    'post_type'     => 'properties',

);
$featured_query = new WP_Query( $args );

$featured_content_properties = '';

$featured_content_properties  = '<div id="featured-properties">';       
$featured_content_properties .=     '<div class="container">';
$featured_content_properties .=         ( $featured_query->have_posts() ) . $property_increment = 0;
$featured_content_properties .=         '<div id="featured-carousel" class="carousel slide" data-ride="carousel">';
$featured_content_properties .=             '<div class="carousel-inner row" role="listbox">';
$featured_content_properties .=                 while( $featured_query->have_posts() ) : $featured_query->the_post();
$featured_content_properties .=                     ($property_increment==0 || $property_increment%3==0 ) . '<div class="item '.(($property_increment==0)?'active':'').'">'; 
$featured_content_properties .=                     '<div class="col-md-4 col-sm-6 col-xs-12 row">';
$featured_content_properties .=                         '<div class="property-wrapper">';
$featured_content_properties .=                             '<a href="'. get_permalink() .'">';
$featured_content_properties .=                             '<div class="corner"></div><p class="corner-text">'. get_field('house_status') .'</p>';
$featured_content_properties .=                             get_the_post_thumbnail($featured_query->ID, "");
$featured_content_properties .=                                 '<div class="property-details">';
$featured_content_properties .=                                     '<h5 class="property-title">'. get_the_title() .',</h5>';
$featured_content_properties .=                                     '<h6 class="property-title">'. get_field('house_town') .'</h6>';
$featured_content_properties .=                                     '<h6 class="property-type">'. get_field('house_type') .'</h6>';
$featured_content_properties .=                                 $price = number_format(get_field("house_price"));
$featured_content_properties .=                                     '<h5 class="property-price">'. $currency . $price .'</h5>';
$featured_content_properties .=                                     '<h5 class="property-bedrooms">'. get_field('house_bedrooms') .'<span class="icon-bedrooms"></span></h5>';
$featured_content_properties .=                                     '<h5 class="property-bathrooms">'. get_field('house_bathrooms') .'<span class="icon-bathrooms"></span></h5>';
$featured_content_properties .=                                     '<span class="btn btn-white full">More Details</span>';
$featured_content_properties .=                                 '</div>';
$featured_content_properties .=                             '</a>';
$featured_content_properties .=                         '</div>';
$featured_content_properties .=                    '</div>';
$featured_content_properties .=                     ($property_increment%3==2)? '</div>':'';
$featured_content_properties .=                 $property_increment++; endwhile;
$featured_content_properties .=             '</div>';
$featured_content_properties .=             '<a class="left carousel-control" href="#featured-carousel" role="button" data-slide="prev">';
$featured_content_properties .=                 '<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>';
$featured_content_properties .=                 '<span class="sr-only">Previous</span>';
$featured_content_properties .=             '</a>';
$featured_content_properties .=             '<a class="right carousel-control" href="#featured-carousel" role="button" data-slide="next">';
$featured_content_properties .=                 '<span class=" glyphicon-chevron-right" aria-hidden="true"></span>';
$featured_content_properties .=                 '<span class="sr-only">Next</span>';
$featured_content_properties .=             '</a>';        
$featured_content_properties .=         '</div>';
$featured_content_properties .=     endif; wp_reset_query();
$featured_content_properties .= '</div>';
$featured_content_properties .= '</div>';

return $featured_content_properties;
}
add_shortcode( 'latest_content_properties', 'latest_content_properties_func' );

任何帮助都将非常感谢!!

1 个答案:

答案 0 :(得分:0)

为什么不创建一个文件,即featured-properties.php并粘贴你的工作代码: -

$currency = get_field('currency', 'options');

$args = array(
    'posts_per_page'=> -1,
    'post_type'     => 'properties',

);
$featured_query = new WP_Query( $args ); ?>

        <!-- Featured Properties -->
        <h1 class="text-grey"><span class="xstrong">Featured</span> Properties</h1>

        <div id="featured-properties">         

            <div class="container">

                <?php if( $featured_query->have_posts() ): $property_increment = 0; ?> 

                    <div id="featured-carousel" class="carousel slide" data-ride="carousel">

                        <!-- Wrapper for slides -->
                        <div class="carousel-inner row" role="listbox">

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

                                <?php if($property_increment==0 || $property_increment%3==0 ){ echo '<div class="item '.(($property_increment==0)?'active':'').'">'; } ?>

                                <div class="col-md-4 col-sm-6 col-xs-12 row">
                                    <div class="property-wrapper">
                                        <a href="<?php echo get_permalink(); ?>">
                                        <div class="corner"></div><p class="corner-text"><?php the_field('house_status'); ?></p>
                                        <?php the_post_thumbnail($featured_query->ID, ''); ?>
                                            <div class="property-details">
                                                <h5 class="property-title"><?php the_title(); ?>,</h5>
                                                <h6 class="property-title"><?php the_field('house_town'); ?></h6>
                                                <h6 class="property-type"><?php the_field('house_type'); ?></h6>
                                                <?php $price = number_format(get_field('house_price')); ?>
                                                <h5 class="property-price"><?php echo $currency . $price; ?></h5>
                                                <h5 class="property-bedrooms"><?php the_field('house_bedrooms'); ?><span class="icon-bedrooms"></span></h5>
                                                <h5 class="property-bathrooms"><?php the_field('house_bathrooms'); ?><span class="icon-bathrooms"></span></h5>
                                                <span class="btn btn-white full">More Details</span>
                                            </div>
                                        </a>
                                    </div>
                               </div>

                                <?php echo ($property_increment%3==2)?'</div>':''; ?>
                            <?php $property_increment++; endwhile; ?>

                        </div>

                        <!-- Left and right controls -->
                        <a class="left carousel-control" href="#featured-carousel" role="button" data-slide="prev">
                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                        </a>
                        <a class="right carousel-control" href="#featured-carousel" role="button" data-slide="next">
                            <span class=" glyphicon-chevron-right" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                        </a>

                    </div>
                <?php endif; wp_reset_query(); ?>

            </div>

        </div><!-- / Featured Properties -->

在此内部,然后在您的functions.php文件中,只需添加: -

/**
 * Shortcode: Latest Properties
 */

function latest_content_properties_func( $atts ) {

    ob_start();
    include 'shortcodes/featured-properties.php';
    return ob_get_clean();

}
add_shortcode( 'latest_content_properties', 'latest_content_properties_func' );