• Hi,

    Been trying to figure this out for a while, but never bothered to go to the forums till now. What’s the deal with using template tags in shortcodes? I noticed that ‘get_’ needs to be in front of some of these functions / template tags in order for them to work.

    i.e. get_the_author() works, but the_author() just prints out the author’s name before I can return the variable for use as a shortcode.

    Can someone explain how this works please? Can’t find anything in the Codex.

    Thanks,

    mn

    PS, thought it might be useful to show what I’m trying to do as well:

    /* News from Blog category only (category '3') */
    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;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • I;m not sure what this has to do with shortcodes but the_author and get_the_author are both functions. The latter (sometimes referred to as a template tag) outputs the returned value to the screen whilst the form just returns it. But they’re still both just functions.

    Thread Starter Osu

    (@mrnabo)

    Hi semi,

    Thanks for getting back to me.

    I’m trying to place those template tags/wp functions into the $inner variable within a shortcode which is why I was mentioning shortcodes. But the same is true of any template tag within a function.

    What is the correct way to put the template tags/wp functions into the shortcode in the example I gave so that $latestblogposts returns correctly?

    Thanks,

    osu

    Not sure I’m following you…

    You can’t place template tags inside a shortcode tag – full stop. And the code you posted above is simply a function – not a shortcode.

    I’m confused…

    Thread Starter Osu

    (@mrnabo)

    Fair enough, I think I’m over-complicating things. I’m going to go back to the drawing board with this one. What I was trying to do is let my client add a shortcode to the sidebar as a text widget in order to let them display the latest posts from a specific category.

    To do that, I was trying to create a loop in a shortcode, but I’ll look at other possibilities.

    Thanks for your help,

    osu

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Shortcodes and template tags – best practice?’ is closed to new replies.