I am new to programming and learning with WordPress.
the_title(); //outputs the title of the page
I want to capture the title of the page into a string variable so I can manipulate it with strtolower
and str_replace
functions.
The only way I have gotten it to work is with output buffering.
ob_start();
the_title();
$result = ob_get_clean();
echo str_replace(" ","-",strtolower($result));
/*there has got to be an easier way....
i dont fully understand why */
str_replace(" ","-",strtolower(the_title()));
What am I doing wrong?
If what you really are looking for is the
wp_title
function, the 2nd argument it takes is a boolean on whether or not it should display it or return it. Pass it false so it will return it to the var, then you can do this:Otherwise, your only option is to find the function you’re looking for and modify the source code.
There is no easier way. Your function does not return the string, it prints it, therefore you will have to use output buffering if you want to capture the output.
It’s the difference between f1() and f2() in the following example.
WordPress is a HORRIBLE app to learn how to program from. It uses these global functions that “just work” but they do very specific tasks “inside ‘The Loop'”. As I say, this is a horrible example of what good code should be.
Thankfully (for you) there are other functions that just return the part you’re looking for. Rather than me just writing what you need, you can read a full listing here. Take care that you note down which must be within the mythical Loop and which you can use anywhere.
As it happens there are even more ways to get the title, but I was really imagining for this example you would do something like:
But as another poster (correctly) says you can use a fairly simple wp_title() function to grab the current loop title.
This brings me back to perhaps wanting to explain why learning programming from WordPress is a bad idea. They have so many damned way of doing the same damned thing that it’s almost impossible to keep on top of things.
A blog is a really simple set of data (even moreso in WP’s case because it isn’t fully normalised) but rather than just having one way to output a title
<?php echo $post->title; ?>
you have umpteen ways, all doing subtly different things.If you really want to learn how to program (instead of hacking your way around the crap that is the WP internals), creating a simple blog engine is fairly quick and fun… It’s certainly how a lot of people get into a new language or framework.
And if you really want to have fun, have a look at Django.
Enough of the WordPress rant. If you’re fighting something like this in the future that doesn’t have 100 ways of doing it, I really wouldn’t recommend output-buffer-capturing. It uses up a whole buttload of resources for something relatively simple.
The easiest way can be as simple as just taking the source for the original function, sticking it in a new function and replacing the
echo
withreturn
.Just note there may be some database connectivity to handle that returning prematurely may break… So if the echo isn’t the last statement, instead of returning right there, store the string as a variable and return at the end of the function.
just figured Id share my final solution with you guys.
This was to give my body tags unique id’s in wordpress.*/
Almost every ‘
the_*
‘ function in WordPress has a ‘get_the_*
‘ counterpart. So, you just have to useAnd it’s going to work like a charm. there’s also
get_the_excerpt()
, get_the_content() and the_permalink() which somehow breaks the naming convention (God knows how many times I’ve written “get_the_permalink()
” and got frustrated on why it didn’t work)Cheers!