WordPress Tips

Hook in Shortcode Before The_Content & After Title

This code filters the content on single pages of a specific post type and parses the shortcode after the entry title before returning the content.

The code includes the conditional tag for a custom post type which you can change to any other conditional.

The code also works in any theme as it uses the_content filter included in WordPress.

Here’s a single post of the type portfolio before the shortcode outputs after the title and before the content.

before shortcode

And here’s what the code generates after the entry title but before the content is returned by the filter function:

after shortcode output

Simply replace the gallery_shortcode in the PHP code with any shortcode for any type of function.

If you’re using a premium framework like Genesis, you don’t need to filter the content as Genesis includes hooks like the genesis_entry_header or genesis_entry_content, both of which you can use in a custom function instead.

Or You could also write the code like this:

add_action( 'genesis_entry_header', function() {
if ( is_singular( 'portfolio' ) ) {
echo do_shortcode( '[gallery_shortcode]' );}});

The genesis action hook produces exactly the same result and is more efficient than the_content filter but only works in Genesis child themes.

Leave a Reply

Your email address will not be published. Required fields are marked *