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;
});
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);
in_the_loop()
: stackoverflow/questions/13456521/… – rg89 Commented Dec 9, 2016 at 15:33