Output the_title() inside the_content()

I’m sure this isn’t possible but thought I’d ask.

I have a testimonials section, the html of each testimonial is like so

Read More
    <p>
      the testimonal here blah, blah, blah, blah, blah, blah, blah, blah
      <em>Name of person</em>
    </p>

Now in WP I have the testimonials as a Custom post type. The Name of person is the title and the testimonial is the content.

Can I output the testimonial post so it’s like the html.

So it would be the_title inside a ’em’ which is inside the ‘p’ of the_content.

I know I could add the name inside the content in wordpress and then make that italic but I know the client wouldn’t like doing this.

Related posts

4 comments

  1. You could achieve that with a filter on the_content:

    function my_the_content_filter( $content ) {
        global $post;
        if( 'testimonial' == $post->post_type )
            $content .= ' <em>' . $post->post_title . '</em>';
        return $content;
    }
    add_filter( 'the_content', 'my_the_content_filter', 0 );
    
  2. You can just repeat the_title() after the_content(), as @Howdy_McGee suggested, or use the_content filter. For example:

    //Make your conditional to check if the current post is of your custom post type
    //Maybe remove wpautop to not have nested <p> elements?
    remove_filter ('the_content',  'wpautop');
    add_filter( 'the_content','filter_function_name');
    function filter_function_name($content){
       $content = '<p>'.$content.'<em>'.get_the_title().'</em></p>';
       return $content;
    }
    

    Just a note: don’t use the_title() inside the filter because the_title() will echo the title and is not suitable here. Use get_the_title(); instead.

  3. Build a shortcode:

    function echo_title_in_post( $atts, $content = null ) {
      return '<em>'.get_the_title().'</em>';
    }
    add_shortcode('the_title','echo_title_in_post');
    

    Then add [the_title/] to your post body wherever you want the title to appear.

  4. I feel CSS is best suited for this personally.

    Inside Your Loop

    the_content();
    the_title('<span class="title">', '</span>');
    

    CSS – Here you can move it or style it however you want:

    .title{
        font-style: italic;
    }
    

Comments are closed.