plugin development - Adding filter to the title without affecting the menu title

admin2025-06-03  4

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?

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;
});

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?

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 edited May 1, 2013 at 4:48 Wern Ancheta asked May 1, 2013 at 3:55 Wern AnchetaWern Ancheta 4251 gold badge3 silver badges14 bronze badges 1
  • The answer to this question is here and uses in_the_loop(): stackoverflow/questions/13456521/… – rg89 Commented Dec 9, 2016 at 15:33
Add a comment  | 

2 Answers 2

Reset to default 1

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!

You can use this:

function change_title($title) {
    if( in_the_loop() && !is_archive() ) { //skipping Menu Items and Archive page
        return $title.'<span class="after_title_ss9"></span>';              
    }

    return $title;

}
add_filter('the_title', array($this, 'change_title'), 10, 2); 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748950110a315086.html

最新回复(0)