简体   繁体   中英

Wordpress - Including PHP in shortcode variables

I am trying to create a shortcode to replicate the following which works fine:-

$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 -->

My problem is that when I try to create this as a shortcode you can't use echo, if, while in variables, I think it's nearly there now, I just don't know what I need to change to get it working, for example, what do I do replace 'while' and 'endif' with when I am using it in a variable?

Here is what I have so far:-

/**
 * 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' );

Any help would be much appreciated!!

Why not create a file, ie featured-properties.php and paste your working code:-

$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 -->

Inside of this, and then in your functions.php file, simply add this:-

/**
 * 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' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM