Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'll start by saying yes, I searched, and yes, I found an answer to this question. It's basically the solution I came up with but I want to make sure this isn't a bad solution.

Here's the real question. I want make the post title a part of the first paragraph on certain types of posts. My solution is straightforward. I call this instead of the_content for a certain category.

function yb_link_post() {
  $link_post_title = '<b class="headline"><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></b>';
  $link_post_content = $link_post_title . ' — ' . get_the_content();

  remove_filter('the_content', 'wpautop');
  echo wpautop($link_post_content);
  add_filter('the_content', 'wpautop');
}

So is this a bad way to do this? I have a hunch that removing and then adding wpautop could have repercussions that I'm not considering. Can anyone tell me if this is a good solution, or if there's a smarter way to do this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Well, removing autop filter from the_content filter tag makes no sense here, because you never apply the_content filters in your code...

Let's take a look at the source code of the_content() function:

function the_content( $more_link_text = null, $strip_teaser = false) {
        $content = get_the_content( $more_link_text, $strip_teaser );
        $content = apply_filters( 'the_content', $content );
        $content = str_replace( ']]>', ']]&gt;', $content );
        echo $content;
}

As you can see, it applies the_content filter to the result of get_the_content() function.

So how your function should look like?

function yb_link_post() {
    $link_post_title = '<strong class="headline"><a href="' . get_permalink() . '" title="' . esc_attr(get_the_title()) . '">' . get_the_title() . '</a></strong>';
    $link_post_content = $link_post_title . ' — ' . get_the_content();

  // you don't have to remove autop filter if you want to run it anyway... and content is already modified, so it will be `autop`ed correctly.
    $content = apply_filters('the_content', $link_post_content);
    $content = str_replace( ']]>', ']]&gt;', $content );
    echo $content;
}

PS. You shouldn't use <b> - use <strong> instead. Also you should escape eveything correctly (i.e. if you print get_the_title() as html attribute, you should run esc_attr on it).

share|improve this answer
    
Thanks for the great answer! As for <b> vs. <strong>, in this particular context, <b> will actually be more correct since I'm not applying any semantic meaning or extra importance to it. In fact, the more I think about it, I should probably just remove that element and put the class on the link instead. It's really only there because it started as the <h1>, and I changed it to <b> when I made it inline. –  Thrillho Nov 18 '13 at 14:22
    
Well, in that case adding class to link would be OK. –  Krzysiek Dróżdż Nov 18 '13 at 15:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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