0

I'm trying to figure out why my shortcode isn't working - can anyone see why (apologies for crappy formatting, can't seem to get this shortcode to display properly)?

The code is running above my shortcode for some reason. Not sure I completely understand how to use template tags /WP functions in shortcodes as I believe you need to use something like get_ at the beginning of the function in order to return it in a variable within shortcodes. Can anyone help?

Thanks

osu

/* News from Blog category only - category 3 */

add_shortcode( 'latestblogposts', 'osu_latestblogposts' );

function osu_latestblogposts() {
    $start = '<div class="widget widget_osu_blog">';
    $start .= '<a title="Subscribe to our RSS feed" href="/feed?cat=3" class="rss"><img alt="RSS" src="' . get_bloginfo('template_directory') . '/images/ico-rss-big.png"></a>';
    $start .= '<div>';

    $my_query = new WP_Query('category=3&showposts=3');
    while ($my_query->have_posts()) : $my_query->the_post();
        $inner = '<div class="item"><a href="' . get_permalink() . '" title="';
        $inner .= printf( esc_attr__( 'Permalink to %s', 'inspire' ), the_title_attribute( 'echo=0' ) );
        $inner .= '" rel="bookmark" class="title">' . the_title() . '</a>';
        $inner .= '<p class="post-meta">';
        $inner .= '<span class="small">by</span> <span class="post-author"><a title="Posts by ';
        $inner .= the_author();
        $inner .= '" href="' . the_author_posts_link() . '">' . the_author() . '</a></span>';
        $inner .= '<span class="small">on</span> <span class="post-date">';
        $inner .= get_the_date('d/m/Y') . '</span></p>';
        $inner .= the_excerpt() . '</div> <!-- End div.item -->';
    endwhile;

    $end = '</div>';
    $end .= '</div> <!-- End div.widget_osu_blog -->';

    $latestblogposts = $start . $inner . $end;
    return $latestblogposts;
}

1 Answer 1

0

If I understand you correctly, you need to call the functions with an optional argument in order to get the returned value instead of echo it directly. For instance, with the_title(), you have 3 optional arguments, the third sets the output (defaults to true). the_title().

For other values you will need to change the function you call. the_author() always displays (echo) the value, you need to call get_the_author() instead.

Sign up to request clarification or add additional context in comments.

4 Comments

You will also need to change "the_excerpt()" for get_the_excerpt().
Hi AJweb,Thanks for getting back to me - you hit the nail on the head. So is it a simple case of using get_ before most of these things? I assume I'll need to check the codex for each of these functions to see if it's available. I'm sure there's a neater way to put a loop in a shortcode as well!
That's right, I am afraid you will need to check the functions in the codex, it is not always the case of just adding get_ to the function name (the_title() is an example).
I suppose you could always capture the output with ob_start() and ob_get_contents(), although I only use output buffering if there is no other solution, if only because of the extra code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.