Connect with us

Tips & Tricks

Remove spaces when echoing the_title

Ever wanted to print or echo the WordPress title attribute without spaces?

Published

on

This little trick can be useful for calling custom functions and printing the title with out spaces for W3C compatibility. I used this trick in a new theme called Galleria, which will be out for public download in the coming days.

Using this comes in handy for a delicious text link:

<?php $title = get_the_title(); ?>
<a href="http://del.icio.us/post?url=<?php echo $title; ?>&amp;<?php echo str_replace(" ", "%20", $title); ?>">
Bookmark This (<?php echo $title; ?>)</a>

What I am doing is calling $title = get_the_title(); and using str_replace(" ", "%20", $title); to replace empty spaces with a %20, which is used in URL encoding empty spaces.

Alternatively you can use a dash or underscore.

Thanks to Jason Boyle for his adaption.

Continue Reading
4 Comments

4 Comments

  1. Peter

    May 16, 2009 at 12:10 am

    If you’re just trying to make the string into a valid URL candidate, urlencode() might be a better choice- that way you don’t have to worry about other characters in your title (colons, commas, punctuation, etc).

    Peter´s last blog post..How to Hide Certain Custom Fields From the Edit Post Page

  2. Peter Kahoun

    May 16, 2009 at 7:58 am

    There is as “urlencode()” php function for this purpose. http://php.net/manual/en/function.urlencode.php

  3. Austin

    May 16, 2009 at 8:31 am

    Thanks for that!

  4. Matt

    August 16, 2009 at 11:58 pm

    Brilliant stuff, thanks!

You must be logged in to post a comment Login

Leave a Reply

Tips & Tricks

Display the_excerpt only if there is text

Have you ever wanted to display the excerpt only if you write one? A simple couple lines of code can display the_excerpt any where you like.

Published

on

In my new theme, I am using this coded trick to display the excerpt on a single post only if I’ve got text inside. Usually if you use the_excerpt and you don’t have one, it will fake one for you.

This is not something that I wanted to do on the single post page. So I used the following code to check if the excerpt existed.

if ( !empty( $post->post_excerpt ) ) :

Once this action is taken into account, you can factor in what code you want to out put if the post_excerpt isn’t empty.

if ( !empty( $post->post_excerpt ) ) :
	the_excerpt();
else :
	false;
endif;

The above code checks if there is an excerpt and print’s it to the screen. If there isn’t an except, it doesn’t do anything.

Continue Reading

Tips & Tricks

Add a shortcode

Published

on

This is a simple one.

/**
 * Your Blog title
 *
 */
function my_blog_title() {
	$blogname = get_bloginfo('name');
    return '<span class="blog-title">' . $blogname . '</span>';
}		  	
add_shortcode('blog-title', 'my_blog_title');

Just add this to your functions.php file and then add [blog-title] in any post or page and it will return your Blog Title. :)

Continue Reading

Tips & Tricks

Adding a external file after the first post

Published

on

How might you display a Google ad after the first post or anything you like? It is very simple. You just need to add the variable $loopcounter in the Loop. If the $loopcounter is less than or equal to 1, then include your option. Check out the code:

<?php if (have_posts()) : while (have_posts()) : the_post(); $loopcounter++; ?>

  // your loop

  <?php if ($loopcounter <= 1) { include (STYLESHEETPATH . '/you-file.php'); } ?>

<?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

Pretty simple huh. Well in the $loopcounter line, you may change the include to point to any file or maybe a custom widget like so:

<?php if ($loopcounter <= 1) { dynamic_sidebar( 'Plus Post' ); } ?>

Or use your code directly in between the { & }.

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Pratt Daddy Crystals Net Worth – & more crazy stats

Pratt Daddy Crystals Net Worth – & more crazy stats

Pratt Daddy Crystals Net Worth. How much is the pratt daddy crystals company worth? Let us go through what we know and what we don’t know about this company, seems like a lot of people are interested in knowing exactly how many sales does the pratt daddy crystals company make or what pratt daddy is […]

Todd Tucker Net Worth: 5 Interesting Facts You Should Know

Todd Tucker Net Worth: 5 Interesting Facts You Should Know

Todd Tucker is a well-known producer who has been an executive producer for several successes. Tucker was born on August 4, 1973, in Georgia. Among his several successes, some of them are My Super Sweet 16 with Reginae Carter & Lil Wayne, Hollywood Divas, A Mother’s Love, and TV One: Ebony Fashion Fair. 1. Todd […]

Allison Dunbar Wiki & Net Worth: Facts to Know about Her

Allison Dunbar Wiki & Net Worth: Facts to Know about Her

Allison Dunbar Wiki Allison Dunbar is an American actress known for her appearance on movies and TV shows like Quick Draw, 2 Broke Girls, The Convent, Daybreak and Strip Mall. Not much information is available about Allison Dunbar regarding her personal life, date of birth and education. What we know is that she started her […]

Ashley Massaro Net Worth: 5 Interesting Facts You Should Know

Ashley Massaro Net Worth: 5 Interesting Facts You Should Know

Ashley Massaro was an American professional wrestler, model, and television personality. She was known for her time with WWE and her appearance on show “Survivor: China”. Here are some interesting facts about Ashley Massaro. 1. Ashley Massaro net worth is estimated to be $2 million. Ashley Massaro net worth before her death is estimated to […]

Trending