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

dougfosterNYCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Here is an example of how to do this. I have implemented the shortcode to take a parameter that specifies how many characters you want in the excerpt. This can be change to an option or similar.
In your page you would do this
[my_post_extract show_excerpt=100]

Open in new window

Then either in your functions.php of a child theme or preferably a plugin you would do this
PHP
function my_post_extract_shortcode($atts)
{
  $ret = '';
  
  $args = array(
    'posts_per_page' => 6,
    'order' => 'asc'
  );  
  $parameters = shortcode_atts (
    array(
      'show_excerpt' => false
    ), 
    $atts
  );

  $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>';
      if ($parameters['show_excerpt'] > 0) {
        $ret .= ' - ' . the_excerpt_max_charlength($parameters['show_excerpt']);
      }
      $ret .= '<hr>'; 
    }
    $ret .= '</div></div></section>';
  }
  else {
    echo "No posts!";
  }

  wp_reset_postdata();
  return $ret;
}
add_shortcode('my_post_extract', 'my_post_extract_shortcode');

function the_excerpt_max_charlength($charlength) {
  $excerpt = get_the_excerpt();
  $charlength++;
  $ret = '';
  if ( mb_strlen( $excerpt ) > $charlength ) {
    $subex = mb_substr( $excerpt, 0, $charlength - 5 );
    $exwords = explode( ' ', $subex );
    $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
    if ( $excut < 0 ) {
      $ret .= mb_substr( $subex, 0, $excut );
    } 
    else {
      $ret .= $subex;
    }
    $ret .= '[...]';
  } 
  else {
    $ret .= $excerpt;
  }

  return $ret;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dougfosterNYCAuthor Commented:
This worked great.  Exactly what I asked for.  Thanks Julian.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.