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

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

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.

  • Can you just repeat the title After the content and style it however with css? – Howdy_McGee Sep 4 '13 at 14:44
  • I don't think I understand. If I use the_content() then the_title() I get a p tag containing the content and a separate p tag containing the title. I can't move the title inside the content p tag with css. – Simon Cooper Sep 4 '13 at 21:04

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 );
| improve this answer | |

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.

| improve this answer | |
  • You're not modifying the content really in the filter, why not use echo get_the_content().get_the_title() in The Loop instead? – Howdy_McGee Sep 4 '13 at 14:50
  • 1
    That is why I started the answer with "You can just repeat the_title() after the_content()" but I thought is interesting teach how to modify the content, just in case he needs to make some more changes. – cybmeta Sep 4 '13 at 14:52
  • 1
    Doesn't meet the requirements in the question: "So it would be the_title inside a 'em' which is inside the 'p' of the_content". A minor edit would make it match though. – s_ha_dum Sep 4 '13 at 15:12

Build a shortcode:

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

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

| improve this answer | |
  • The shortcode option is fine but if Simon has a custom post type and he wants the same structure in all post of that type, adding the shortcode to all posts is not the best solution. I think. – cybmeta Sep 4 '13 at 15:22
  • I am not sure the question is clear enough to allow for a distinct solution. That is why there are several answers. I am afraid that the OP is asking for something that requires regex on the post content, but hopefully someone's solution will work without that. – s_ha_dum Sep 4 '13 at 17:20

I feel CSS is best suited for this personally.

Inside Your Loop

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

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

    font-style: italic;
| improve this answer | |
  • the other answers don't preclude a css component, but they also don't require editing a template file accomplish – Milo Sep 4 '13 at 23:40

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.