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 already posted the same exact thing on stackoverflow without any comments or answer, i hope to get something going here:


I'm trying to change the post content of a page by hooking into "the_post" action, but the problem is that wathever i modify in there doesn't really get applied correctly.

The problem seems to come from the fact that "query->setup_postdata" creates a $pages variable and sets the content into it before calling the "the_post" hoook. So whatever changes i do to the $post object doesn't reflect in the page.

Further down the road, it seems that the "get_the_content" function which calls the "the_content" filter uses the content from $pages instead of the $post->post_content. Thus if i change the content of the post, it will not output it correctly because wordpress bases itself off something else to print out the content.

Now if i hook unto "the_content" filter and replace the text using a simple hook mechanism such as:

public function wordpress_hooks_the_title($title){
    if(get_the_ID() == 4){ return 'test title'; }
    return $title;
}

public function wordpress_hooks_the_content($content){
    if(get_the_ID() == 4){ return 'test content'; }
    return $content;
}

It works fine for the content, but the title overrides many other titles in the page such as in the menu, even if i call "get_the_ID()" which is fine because the current $post is ok...

So i'm stuck trying to find the right way and not a half and half way. I'd like to use "the_post" only or "the_content/the_title" only but only half of them work fine in each context.

What would be the right approach?

Thanks

share|improve this question

1 Answer 1

It seems you're actually looking for the the_content filter.

add_filter('the_content', 'modify_content');
function modify_content($content) {
    global $post;
    if ($post->ID != $desired_id)
        return $content;

    $modified_content = /* modify content here */;
    return $modified_content;
}
share|improve this answer
    
Thats what i'm already doing. –  Mathieu Dumoulin Jul 27 '12 at 13:55

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.