29

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?

39

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.

  • also it is possible to add content after title; the filter the_title is the right hook. – bueltge Jan 24 '12 at 7:51
  • @ChipBennett question - how to do this with logic only for a custom post type - I tried to wrap it in if ( is_single() && 'property' == get_post_type() ) {} but that didn't work for me – Jason Jan 25 '12 at 0:02
  • @ChipBennett - I got it working on my custom post type, but the content disappears from any other post type. See edit above. – Jason Jan 25 '12 at 0:10
  • 1
    That's because you're not returning $content for post types other than for your custom post type. See updated answer. – Chip Bennett Jan 25 '12 at 0:55
  • Just a note - you don't need the else { } block - just the fallback return. If the condition is met, the return in the if() takes you out of the function, if you make it past the if() then the fallback return will hit. – phatskat Dec 6 '12 at 15:02
0
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

  • Late to the game here, but you're returning an empty variable in the instance that is_singular( 'property' ) returns false. If you invert your logic there, and simply return $content in that case, you will end up with cleaner, more readable code. – Travis Weston Sep 13 '18 at 13:50
  • Could also add else or use a ternary operator. Its a quick example not fully tested which can be extended. – Brad Dalton Sep 13 '18 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.