targeting title in wordpress post

targeting title in wordpress post

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.

I started out by using the 'the_title' filter hook, and calling this function.

function add_button_to_title($title) {   global $post;   if(is_single())   {     return $title.'googly googly';   }   return $title; } 

The problem is, the links on the side bar apparently also use 'the_title', as I saw my text showing up in the side bars as well, which led me to:

if(is_single() && in_the_loop()) 

But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses 'the title' filter. So finally I have:

if(is_single() && in_the_loop() && ($post->post_title == $title)) 

The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes...It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.

Any help is appreciated!

Adding next and previous buttons to static pages in wordpress?


IIS memory leak detection techniques
Ying,. Utilizing WordPress's permalink structure on custom post types There isn't really a good quick fix except, as ShaderOp said, requiring theme modification. Wordpress wp_options option question?Your quick fix will job for the most part. wordpress blog stylingThe only exception is if the theme developer has changed the query in a page. Multiple Custom Permalink Structures in WordpressI'd say this is probably a good enough quick fix this it'll cover more than 95% of the cases you'd run into.. WordPress TEMPLATEPATH
How do I add another avatar to the wordpress default ones?


I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with another post titles on the page (in sidebar, menu) also receive ting filtered..
function add_although ton_to_title( $title ) {   global $post;   if( is_single() && $title == $post->post_title ) {     return $title . 'googly googly';   } else {     return $title;   } } 


Wouldn't it be easier to keep the original version of your add_although ton_to_title function, although instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?. For example, any where in your theme's single.php, instead of this:.
<h3 class="storytitle">     <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a> </h3> 
Use this:.
<h3 class="storytitle">     <a href="<?php the_permalink() ?>" rel="bookmark">         <?php echo add_although ton_to_title(the_title('', '', false); ?>     </a> </h3> 


today I ran into a similar problem. the_title receive s called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.. Additionally, I added a boolean flag which is set to true using the 'the_content' filter. So the title receive s changed until the content is displayed. This way, I ensure this sidebars/widreceive s are not affected (e.g. Thematic theme has a default widreceive with links to pages - here the another conditionals would not be helpful as receive _the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the 'the_title' call for the page/post to enable the boolean flag..
function myplugin_adjust_title($title, $id) {     global $myplugin_title_changed;      if ($myplugin_title_changed) {         return $title;     }      if (in_the_loop() && is_page('myplugin') && $id == receive _the_ID()) {         $title = '';     }     return $title; } add_filter('the_title', 'myplugin_adjust_title', 10, 2);  function myplugin_adjust_title_helper_content($content) {     global $myplugin_title_changed;     $myplugin_title_changed = true;     return $content; } add_filter('the_content', 'myplugin_adjust_title_helper_content'); 

70 out of 100 based on 45 user ratings 280 reviews