Archived topic
Post excerpt and taxonomy shortcode
4 replies · Started by David on January 28, 2018
Hi Tom / Leo,
i am working on some Post layouts and want to display excerpt and taxonomies using a shortcode. Currently being added to a left side bar. So far i have cobbled together the following PHP. But i am not sure how to return the content so it remains within the aside widget.
function display_the_excerpt() {
if ( have_posts() ) :
while ( have_posts() ) : the_post();
echo '<div class="aside-excerpt">';
the_excerpt();
echo '</div>';
echo '<div class="aside-categories">';
the_terms( $post->ID, 'category', '', '');
echo '</div>';
echo '<div class="aside-post-tags">';
the_terms( $post->ID, 'post_tag', '', '');
echo '</div>';
endwhile;
endif;
}
add_shortcode ( 'display_excerpt' , 'display_the_excerpt' );
Any thoughts appreciated.
David
When echoing inside a shortcode, you need to use output buffering: https://codex.wordpress.org/Shortcode_API#Output
Doh! Thanks and apologies Tom this is not the first time you have pointed me there.
Too many late nights! I will go back to using the hooks!
Much appreciated
No problem! Glad I could help :)
Honestly - sorry for wasting your time. Been there before with shortcodes. Hooked the output into the left-side bar, works like a treat!