$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Adding filter to the title without affecting the menu title|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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); 
Post a comment

comment list (0)

  1. No comments so far