How to enhance your wordpress code?

I get a lot of questions about enhancing my themes, put new functionality in them or just to help them do it themselves. Since I have a regular daily job, a freelance studio and themes as my little pet project, I simply can’t answer and help everyone. But, I surely can try.

Throughout this article, I’ll try to focus on most common questions you asked me during this year.

How to only show posts with a specific custom field?

Let’s say we want to only show posts that have custom field “thumb” and value “jpg”.
We’ll use a query_posts function and place it above the standard loop code.

<?php query_posts('meta_key=insert_key&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;thumb=jpg'); ?>

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

How to retrieve custom fields outside the loop?

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'insert_your_custom_field_key', true);
?>

How to replace the “Read more” link?

The most common problem with the “read more” link, is that you can’t change its text. Well, this was true till version 2.8. come out. Here is the solution for previous versions of wordpress. Just add this to your functions.php file.


<?php
add_filter( 'the_content_more_link', 'my_custom_link', 10, 2 );
$default_more = "Continue reading";
function my_custom_link( $link_text, $link_class ) {
return str_replace( $link_text, $default_more, $link_class );
}
?>

From version 2.8. you can use parameters within the template tag the_content().
So, we just enhance are content tag:


<?php the_content("Continue reading this post"); ?>

How to get short urls for social bookmarking?

Sometimes you need short urls for social bookmarking and using permalinks is not a good solution. That’s why we’ll use post ID instead.


<?php echo get_bloginfo('url')."/?p=".$post->ID; ?>

But, if you want to send this url to twitter we have to use tinyurls.
First, add this function to your functions.php file.


function getTinyUrl($url) {
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
return $tinyurl;
}

And now you can use it in your posts by calling this function.


<?php echo getTinyUrl(get_permalink($post->ID)); ?>

How to disable (don’t show) images on homepage?

I prefer to show images on homepage but sometimes less is more, depends on theme design. If you want to prevent images to show on your homepage just add this hack to your functions.php file.


<?php
add_filter('the_content','wp_home_no_image_filter',11);

function wp_home_no_image_filter($content){
if (is_home() || is_front_page()){
$content = preg_replace("/<img[^>]+\>/i", "", $content);
}
return $content;
}
?>

How to show parent page/post title?

This is quite simple. Just add this code where you want to show your parent page/post title.


<?php echo get_the_title($post->post_parent); ?>

How to add Breadcrumbs to WordPress?

Source: Add Breadcrumbs to WordPress Without a Plugin

There are a lot of wordpress plugins that will do the job. I personally, use Yoast Breadcrumbs on my themes, but I run into a great article by Brandon Cox who wrote this excellent piece of code.

<?php
function breadcrumbs() {
$theFullUrl = $_SERVER["REQUEST_URI"];
$urlArray=explode("/",$theFullUrl);
echo ‘You Are Here: <a href="/">Home</a>’;
while (list($j,$text) = each($urlArray)) {
$dir=”;
if ($j > 1) {
$i=1;
while ($i < $j) {
$dir .= ‘/’ . $urlArray[$i];
$text = $urlArray[$i];
$i++;
}
if($j < count($urlArray)-1) echo ‘ &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;raquo; <a href="’.$dir.‘">’ . str_replace("-", " ", $text) . ‘</a>’;
}
}
echo wp_title();
}
breadcrumbs();
?>