Link to home
Start Free TrialLog in
Avatar of Doug Foster
Doug FosterFlag for United States of America

asked on

adding content excerpt option to shortcode

I found some old code in functions.php which creates a shortcode for future events.  I'd like an option in the shortcode to include the excerpt (and if not there, a portion of the content). How do I add that?

add_shortcode('upcoming_events','upcoming_events');
function upcoming_events($atts){
    $ret = '';
    //wp_query get upcoming events list titles.
    $args = array(

        'post_type' => 'events',

        'posts_per_page' => 6,

        'post_status' => 'future',

        'order' => 'asc'

    );

    $the_query = new WP_Query( $args );

    if($the_query->have_posts()){

        $ret .= '<section class="widget widget_text"><div class="widget-wrap"><div class="textwidget">';

        while ($the_query->have_posts()){

            $the_query->the_post();

            $ret .= '<span>'.get_the_date('m/d/Y').' - <a href="'.get_the_permalink().'">'.get_the_title().'</a><hr/>';

        }

        $ret .= '</div></div></section>';

    }

    wp_reset_postdata();

    return $ret;

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Problem unclear

So you want to be able to pass the short code a parameter and based on that you want to pull the posts excerpt if (it exists) otherwise you want an excerpt of the data (how much in that case)?
Avatar of Doug Foster

ASKER

Hi Julian.

Yes, i want it to be a parameter in the short code, and have it pull the post's excerpt if it exists.  If not, i thought it would be in the settings how many characters to take from the content, but i can change it to what i need.  Let's start with 100 characters.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This worked great.  Exactly what I asked for.  Thanks Julian.
You are welcome.