0

I'm looking for the best way to obtain this.

For example, I have the h1 tag whic is: Web Design

I want to add, in all the h1 tags of a certain post type, the same word at the beginning, which is my keyword. So, the result would be: MYKEYWORD Web Design

I have clear that I have to use the_content filter. I have this code:

add_filter( 'the_content', 'handle_h1' );

function handle_h1( $content )
{
    //check if it's the post type which I look for
    if (  'MYPOSTTYPE' != get_post_type() )
        return $content;


    return str_ireplace( '<h1', '<a href="#">Back to the top</a><h1', $content );
}

The example above adds the a href tag before the h1 tags...

Which I want is to change the output text of the h1, as I mentioned above.

EXAMPLE:

Original:

<h1>Books</h1>

What I want

<h1>Our Books</h1>

So the word added is "Our".

5
  • Do you want to add that to ever h1 HTML tag that wraps anything in your content (not the post type title or other parts)?
    – kaiser
    Nov 19, 2014 at 16:58
  • In these posts types, I only have one h1 tag in the page, and I want to append the word MYKEYWORD just before the h1 text <h1>MYKEYWORD text...</h1> Thanks! Nov 19, 2014 at 17:00
  • 1
    Again: In the content or the title or some meta field? Please update your question with an edit and give us (a) an example text to test on and (b) show us the template/loop contents where this should happen.
    – kaiser
    Nov 19, 2014 at 17:01
  • I just edited my question. I want to add to ever h1 of a custom post type. Nov 20, 2014 at 8:40
  • Daniel, it seems that you do not understand me fully. Could you ask a friend who speaks better English for help on answering my questions?
    – kaiser
    Nov 20, 2014 at 11:38

1 Answer 1

1

This is my solution:

add_filter( 'the_title', 'change_title', 10, 2 );   
function change_title( $title, $postid ) {

    global $post;

    if( get_post_type() == MYPOSTTYPE && $title == $post->post_title ){            

        $title = "MYKEYWORD ".$title;            
    }

    return $title;
}

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.