Skip Navigation

[Resolved] Shortcode output showing up in the wrong place

This support ticket is created 8 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by kasiaG 8 years, 3 months ago.

Assisted by: shekhar.

Author
Posts
#355626
Screen-Shot-2015-12-24-at-1.19.20-PM.jpg
Screen-Shot-2015-12-24-at-1.13.26-PM.jpg

I am using a plugin called Co-Authors Plus to enable guest authors without creating additional WordPress users. Because the Views plugin does not support adding any of this data to Content Templates natively, I wrote a simple custom shortcode in my functions.php file to output a link to the guest author archive if the post has a guest author, otherwise output a link to the author archive. The code for that is as follows:

function custom_author_link_function() {
	if ( function_exists( 'coauthors_posts_links' ) ) {
		coauthors_posts_links();
	} else {
		the_author_posts_link();
	}
}

add_shortcode('custom_author_link', 'custom_author_link_function');

I have included the custom shortcode in my Content Template as follows (see near the end of line 7):

<hr class="no-margin-top">
<h1 class="center caps blue">[wpv-post-title]</h1>
<hr>
[wpv-conditional if="( '[wpv-post-taxonomy type='post_tag' format='text']' ne '' )"]
<div class="center tag oswald caps">[wpv-post-taxonomy type="post_tag"]</div>
[/wpv-conditional]
<div class="center meta oswald caps grey"><small>Posted on</small> [wpv-post-date][wpv-conditional if="( has_term('general', 'category', null) ne '1' )"] <small>in</small> [wpv-post-taxonomy type="category"][/wpv-conditional] <small>by</small> [custom_author_link]</div>
<hr class="no-margin-bottom">
<div class="post-body margin-top margin-bottom">[wpv-post-body]</div>

The custom shortcode is outputting the correct content, however it is not showing up in the right place. It is showing up outside of the main layout container. Please see attached screenshots of rendering and source code for more information.

I tested the functionality with a third party plugin shortcode and did not have the same problem. The output appeared in the correct place. What am I doing wrong?

#355807

shekhar
Supporter

Hi there,

Thank you for contacting Toolset support.

Please use the following code:

add_shortcode('custom_author_link', 'custom_author_link_function');

function custom_author_link_function() {
	 if ( function_exists( 'coauthors_posts_links' ) ) {
	 	ob_start();
	 	coauthors_posts_links();
		$content = ob_get_clean();
		return $content;
    } else {
    	ob_start();
	 	the_author_posts_link();
		$content = ob_get_clean();
		return $content;
    }
}
 

The reason why we use ob_start() and ob_get_clean() is to buffer the output.

Please let me know if the above solution works for you, I look forward to your reply!

Thanks

#356483

Worked brilliantly, thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.