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.

Hi I'm having problems with the the_title filter here's the code that I have:

function zen_title_filter($title){

    if(is_singular()){
        if(!empty($_GET['asin'])){
            if($title == 'Product Details Page' || $title == 'Compare Products Page'){
                $title = zen_get_titles();

            }

        }

    }

    return $title;
}

add_filter('the_title', 'zen_title_filter');

As you can see this filter updates both the menu title for the product details page and compare products page. But the behavior that I'm expecting is that it should only update the title of the post itself but not the menu title which in this case is also the same as the post title. Any ideas?

enter image description here

UPDATE

I'm actually generating the page on the fly. Using a page as the template. So the post id is the id of the page. What I'm doing is adding filter to the content and completely replace the content of the page using the new content:

add_filter('the_content', function($content){
  if(is_page('compare')){
   $asin = esc_attr($_GET['asin']);
   $data = get_data($asin);
   $smarty->assign('item_data', $data);
   $content = $smarty->fetch('file.tpl');
  }
  return $content;
});
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Since you're generating things on the fly, we can use a global flag to let us know when we want the title applied.

add_filter('the_content', function($content){
  if(is_page('compare')){
   global $asin_doing_template;

   // mark that we're doing a template
   $asin_doing_template = true;

   $asin = esc_attr($_GET['asin']);
   $data = get_data($asin);
   $smarty->assign('item_data', $data);
   $content = $smarty->fetch('file.tpl');

   // set it back to false
   $asin_doing_template = false;
  }
  return $content;
});

function zen_title_filter( $title ){
    global $asin_doing_template;

    if( is_singular() && $asin_doing_template ){
        if( $title == 'Product Details Page' || $title == 'Compare Products Page' ){
            $title = zen_get_titles();
        }
    }

    return $title;
}

add_filter( 'the_title', 'zen_title_filter' );

Let me know if that does it!

share|improve this answer
    
it doesn't seem to work. I'm actually generating the post on the fly so the post id is actually the id of the current page. Please see the update in my question. –  Ieyasu Sawada May 1 '13 at 4:48
    
I've updated my answer based on the auto-generation factor. Let me know how it looks. –  Andy Adams May 1 '13 at 17:11
    
that would update both the menu title and the post title. but anyway thanks for your help, I guess I'll just modify the menu title via javascript once the page loads –  Ieyasu Sawada May 6 '13 at 5:05
add comment

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.