Join 3,430 readers in helping fund MetaFilter (Hide)


Another question in a long series of me not understanding PHP
July 24, 2012 11:40 AM   Subscribe

Continuing Wordpress help filter: trying to append a certain word to the end of each title of the posts on my blog. Issue complicated slightly by this being a child theme, details inside...

OK, so I'm using the Buddypress plugin, with the theme Frisco, which is a child of the default BP theme. I need to append the word "test" to the end of each post title, so if the title of a post was Foo, then I'd like for it to automatically be Foo Test when viewed on the front page.

Advice I've gotten from other people suggest that this can be modified by going in to single.php, finding the place where wordpress is calling the title of the post (the "php the_title();" function) and writing in "test" in plain HTML next to this, so in the current theme it's right here (with the full single.php file at that link). Every time I try to modify this, the changes don't show up on my site. I've tried editing the parent theme directly (a bad idea, I know) and putting a copy of the single.php file in to the child theme's directory. Neither one works.

Am I going about this the wrong way? Do I have the right idea and the wrong execution? Any help would be greatly appreciated.
posted by codacorolla to Computers & Internet (15 answers total) 1 user marked this as a favorite
 
Is this what you're doing?
<div class="post-content">
<h2 class="posttitle"><?php the_title(); ?> this</h2>
or are you doing something different? From my experiences, if you do the above it should work. (I've done it with tags and categories)
posted by royalsong at 11:51 AM on July 24, 2012


Yep, that's what I'm doing. It doesn't seem to be working though :/.
posted by codacorolla at 11:52 AM on July 24, 2012


OK, so with a child theme, if I have a single.php with the proper things changed in it, then do I have to do something to the file to make it clear that Wordpress should be using the single.php in the child theme directory instead of the parent theme directory? What I'm getting at is that the method should be working, but it's not, so it's making me think that I'm not doing something to get the theme to recognize the changes that I've made.
posted by codacorolla at 11:56 AM on July 24, 2012


Oh, you know, isn't single.php for pages and not blog posts?

Is it there if you look at the post all on it's own, like if you click on the title/time?
posted by royalsong at 12:02 PM on July 24, 2012


If you'd need to get that into more than one template file, try it with CSS:

h2.posttitle:after { content: ' TEST'; }
posted by mimi at 12:06 PM on July 24, 2012


Oh, you know, isn't single.php for pages and not blog posts?

Is it there if you look at the post all on it's own, like if you click on the title/time?


Yes! Thank God, I thought I was doing something monstrously stupid...

If you'd need to get that into more than one template file, try it with CSS:

h2.posttitle:after { content: ' TEST'; }


I had no idea CSS could do this... I couldn't get it to work by putting it in to my custom.css file, is it cross browser compatible?
posted by codacorolla at 12:10 PM on July 24, 2012


Actually, scratch that, it did work...
posted by codacorolla at 12:14 PM on July 24, 2012


OK, looks like the proper place to append the 'test' text is in single.php, archive.php, and index.php. This will put 'test' after any post title that a user is likely to look at. Thanks for the help everyone!
posted by codacorolla at 12:22 PM on July 24, 2012


Huh, I didn't know you could do it via CSS either. That's probably the easiest way to go about it.

But if you wanted to edit php files, you would just put "this" after every instance of the.. nevermind you figured it out!
posted by royalsong at 12:24 PM on July 24, 2012


Themes really only HAVE to have two files: style.css and index.php.

But when inspecting an existing or writing your own theme it's helpful to know what names the special parts of the theme have. Template Hierarchy diagram from Template Hierarchy in the Codex.
posted by artlung at 12:38 PM on July 24, 2012


The clean way to do this is to add a hook in your theme's functions.php file - paste the following at the end, after the closing ?> php tag. Avoid having spaces or blank lines between the closing ?> and opening <?php from below - it should run on, like ?><?php.

This hooks into all WordPress page requests, meaning you do not need to change template files. Obviously, change the value of $codacorolla_word to match your needs.

(note: untested, but should work)

<?php

$codacorolla_word = "my word";

add_filter( ‘wp_title’, ‘codacorolla_word_to_title’ );

codacorolla_word_to_title( $title, $sep ) {
 global $codacorolla_word;
 $title .= $sep . $codacorolla_word;
 return $title;
}

?>

posted by davemee at 12:49 PM on July 24, 2012


gah. edit window!


<?php

$codacorolla_word = "my word";

add_filter( ‘wp_title’, ‘codacorolla_word_to_title’ );

function codacorolla_word_to_title( $title, $sep ) {
 global $codacorolla_word;
 $title .= $sep . $codacorolla_word;
 return $title;
}

?>


Said it was untested. Missing 'function' keyword restored!
posted by davemee at 12:51 PM on July 24, 2012


Building on davemee's code, this worked for me on one of my sites:
$codacorolla_word = "[my added word]";
function codacorolla_word_to_title( $title, $sep=' ' ) {
   global $codacorolla_word;
   $title .= $sep . $codacorolla_word; 
   return $title;
}
add_filter( 'wp_title', 'codacorolla_word_to_title' );
add_filter( 'get_the_title', 'codacorolla_word_to_title' );

posted by artlung at 1:20 PM on July 24, 2012 [1 favorite]


is it cross browser compatible
Yep, with all modern ones. You can also use :before. CSS is neat!
posted by mimi at 3:39 PM on July 24, 2012


(For those of you curious about support for the :after content: stuff, check: http://caniuse.com/#search=:after for a table that talks about compatibility). Clever thought mimi!

I'm not certain, though, about whether such text gets read by web spiders like googlebot.
posted by artlung at 6:17 PM on July 24, 2012


« Older Do you know of a (US) website ...   |  Getting married soon (woo!). ... Newer »
This thread is closed to new comments.