0

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?

1 Answer 1

1

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).

2
  • 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, 2013 at 14:22
  • Well, in that case adding class to link would be OK. Nov 18, 2013 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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