The question:
trying to insert content before the post content in my functions.php – I know how to use the regular wp hooks, but unsure how to insert into other areas.
Tried this, but it kills content on any other post type:
function property_slideshow( $content ) {
if ( is_single() && 'property' == get_post_type() ) {
$custom_content = '[portfolio_slideshow]';
$custom_content .= $content;
return $custom_content;
}
}
add_filter( 'the_content', 'property_slideshow' );
How do I make this conditional?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
Just use the the_content
filter, e.g.:
<?php
function theme_slug_filter_the_content( $content ) {
$custom_content = 'YOUR CONTENT GOES HERE';
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'theme_slug_filter_the_content' );
?>
Basically, you append the post content after your custom content, then return the result.
Edit
As Franky @bueltge points out in his comment, the process is the same for the post title; simply add a filter to the the_title
hook:
<?php
function theme_slug_filter_the_title( $title ) {
$custom_title = 'YOUR CONTENT GOES HERE';
$title .= $custom_title;
return $title;
}
add_filter( 'the_title', 'theme_slug_filter_the_title' );
?>
Note that, in this case, you append your custom content after the Title. (It doesn’t matter which; I just went with what you specified in your question.)
Edit 2
The reason your example code isn’t working is because you only return $content
when your conditional is met. You need to return $content
, unmodified, as an else
to your conditional. e.g.:
function property_slideshow( $content ) {
if ( is_single() && 'property' == get_post_type() ) {
$custom_content = '[portfolio_slideshow]';
$custom_content .= $content;
return $custom_content;
} else {
return $content;
}
}
add_filter( 'the_content', 'property_slideshow' );
This way, for posts not of the ‘property’ post-type, $content
is returned, un-modified.
Method 2
There was a new hook introduced in 5.2 that triggers after the opening <body>
tag. No more need to mess with the post content.
/**
* Add code snippets directly after the opening <body> tag.
*/
function add_to_top_of_body() {
echo '<!-- Google Tag Manager (noscript) --><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-YOURMOM" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><!-- End Google Tag Manager (noscript) -->';
}
add_action( 'wp_body_open', 'add_to_top_of_body' );
Method 3
function property_slideshow( $content ) {
if ( is_singular( 'property' ) ) {
$custom_content = do_shortcode( '[portfolio_slideshow]' );
$custom_content .= $content;
}
return $custom_content;
}
add_filter( 'the_content', 'property_slideshow' );
The is_singular
conditional tag checks if a singular post is being displayed and enables you to specify the $post_types parameter which in this case is property.
Also, you might want to look at do_shortcode
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0