1

Suppose I have 3 posts on my wordpress site titled:

  • Test_post_1 with tag test_tag
  • Test_post_2 with no tag
  • Test_post_3 with tag test_tag

Suppose now that I have one page titled page_1. On that page I have some written text. At the end of that page I want to link to my 2 posts with tag test_tag. I want the 2 links to appear as post excerpts, with post title, featured image and about 50 words. (Here is an example of such post excerpts.)

I have did some reaseach on this, and it appears I need to use the php the_excerpt() function. This would result in making specific page templates in wordpress or adjusting page php code. I have some leads on how to do this already, but that is not what I want. I want a simple way to add post experts from the page editor in wordpress. Something as simple as a shortcode. Something like [excerpt tag="test_tag"] that I can put right after my text in the page editor. Has anyone any leads for similar simple ways?

4

You need a couple of things. A shortcode handler, and a custom query:

function wpse_186346_excerpt_length() {
    return 50;
}

function wpse_186346_excerpt( $atts ) { 
    $atts = wp_parse_args( $atts,
        array(
            'posts_per_page' => 2,
            // Any other default arguments
        )
    );

    // We don't need paging, always save a query
    $atts['no_found_rows'] = true;

    // Create query based off arguments from shortcode  
    $query = new WP_Query( $atts );

    // Start the buffer to "catch" our output below, so that we can return it as a string
    ob_start();

    // Set our custom excerpt length
    add_filter( 'excerpt_length', 'wpse_186346_excerpt_length' );

    // Loop over the posts and output whatever markup you need
    while ( $query->have_posts() ) {
        $query->the_post();
        the_title( '<h4 class="excerpt-title"><a href="' . get_permalink() . '">', '</a></h4>' );
        the_excerpt();  
    }

    // Remove our custom excerpt length
    remove_filter( 'excerpt_length', 'wpse_186346_excerpt_length' );

    // Restore the current global post
    wp_reset_postdata();

    return ob_get_clean();  
}

add_shortcode( 'excerpt', 'wpse_186346_excerpt' );

And use it like so:

[excerpt tag="test_tag"]

Your shortcode arguments just need to match those available, so you're not limited to only tags!

3
  • Thank you. Given that your clear answer is about adding a php function (possibly through a child theme) I assume that there are no ready-made ways to do this in WordPress without adding/modifying code. – Davor Josipovic May 7 '15 at 6:15
  • There is nothing in core for doing exactly this. But we're using all the cool stuff core has to offer to do it! – TheDeadMedic May 7 '15 at 12:54
  • Yes, I have noticed that. I actually learned something about WordPress in the process. Thank you. – Davor Josipovic May 7 '15 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.