Archive for the ‘Wordpress’ Category

Wordpress Breadcrumb Navigation

Thursday, July 30th, 2009

Need a Wordpress breadcrumb navigation trail for your Wordpress theme, but do not want to rely on a plugin?

If you want your Wordpress theme to stand out from the crowd it is worth considering implementing a breadcrumb navigation trail. A breadcrumb trail can make it easier for your site visitors to identify what page they are currently viewing.

Plugins like Breadcrumb NavXT are available that will automatically produce a breadcrumb trail for you. However, if you are looking for an easier method that does not require a plugin the below solution should help.

Breadcrumb.php code

Copy the below code into a new file called breadcrumb.php – make sure this file is in your root theme folder.

<div id="breadcrumb">
  <?php /* If this is the homepage */ if (is_home()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a></p>
  <?php } ?>
  <?php /* If this is a tag archive */ if (is_tag() ) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="#" rel="bookmark" title="Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;">Posts Tagged &#8216;
    <?php single_tag_title(); ?>
    &#8217;</a></p>
  <?php } ?>
  <?php /* If this is a category archive */ if (is_category()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="#" title="<?php single_cat_title(); ?>">
    <?php single_cat_title(); ?>
    </a> </p>
  <?php } ?>
  <?php if /* If this is a page */ (is_page()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo;
    <?php
	global $wp_query;
	if (empty($wp_query->post->post_parent) ) {
		$parent = $wp_query->post->ID;
		echo '';
	} else {
		$parent = $wp_query->post->post_parent;
		echo '<a href="'.get_permalink($parent).'">'.get_the_title($parent).'</a> &raquo;';
	}
?>
    <a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
    <?php the_title(); ?>
    </a></p>
  <?php } ?>
  <?php if /* If this is a blog post */ (is_single()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo;
    <?php the_category(', ') ?>
    &raquo; <a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
    <?php the_title(); ?>
    </a></p>
  <?php } ?>
  <?php if /* If this is a search page */ (is_search()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a></p>
  <?php } ?>
  <?php /* If this is a 404 page */ if (is_404()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="#" title="404 Error page">404 Error</a></p>
  <?php } ?>
  <?php /* If this is a yearly archive */ if (is_year()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>archives</span></a> &raquo; <a href="#" title="Year">
    <?php the_time('Y'); ?>
    </a></p>
  <?php } ?>
  <?php /* If this is a monthly archive */ if (is_month()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>archives</span></a> &raquo; <a href="#" title="Month">
    <?php the_time('F, Y'); ?>
    </a></p>
  <?php } ?>
  <?php /* If this is a daily archive */ if (is_day()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>archives</span></a> &raquo; <a href="#" title="Month">
    <?php the_time('F jS, Y'); ?>
    </a></p>
  <?php } ?>
  <?php /* If this is a author archive */ if (is_author()) { ?>
  <p> <strong>You are here:</strong> <a href="<?php bloginfo('siteurl'); ?>" title="home"><span>home</span></a> &raquo; <a href="<?php bloginfo('siteurl'); ?>/archives" title="archives"><span>authors</span></a> &raquo;
    <?php the_author_posts_link(); ?>
    <?php
if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else :
$curauth = get_userdata(intval($author));
endif;
?>
    <a title="<?php echo $curauth->display_name; ?>'s posts" href="#"><?php echo $curauth->display_name; ?></a> </p>
  <?php } ?>
</div>
<!-- end breadcrumb -->

Call the above template anywhere in your theme files (usually in header.php) using the following code:

<?php include (TEMPLATEPATH . "/breadcrumb.php"); ?>

The end HTML result will look something like this:

<div id="breadcrumb">
  <p> <strong>You are here:</strong> <a href="http://yoursitename.com" title="home"><span>home</span></a></p>
</div><!-- end breadcrumb -->

Known issues!

This does not work for Grandchild categories. In my experience, grandchild categories are used very rarely. If you have found a workaround or any improvements to this code let me know.

Hide images from guests in Wordpress!

Friday, July 24th, 2009

If need to hide images from guests (users that are not registered) on your Wordpress blog this function should help.

Why would I need to hide images from guests?

You may have some images that you only want registered users to view on your blog. For example, you may be a photographer that would like to show your latest great shot to a select number of registered users. Making content available to registered only users can be a great incentive for your readers to register on your blog.

Simple add the below code to the top of your theme’s functions.php after the opening php tag.

PHP Function / Hide images

if ( is_user_logged_in() ) {

} else {
	add_filter('the_content','wpi_image_content_filter',11);

function wpi_image_content_filter($content){

    if (is_single() || is_front_page || is_home ()){
      $content = preg_replace("/]+\>/i", "" />", $content);

    }

    return $content;
}
}

Please note: the function will not block image links, so guests could still view the source image if they click on the image. To avoid this do not add links to inserted images.

Alternatively, use the below function to block images and links. Be careful, as this will remove (hide) all links in your blog posts.

PHP Function / Hide images and links

if ( is_user_logged_in() ) {

} else {
	add_filter('the_content','wpi_image_content_filter',11);

function wpi_image_content_filter($content){

    if (is_single() || is_front_page || is_home ()){
      $content = preg_replace("/]+href[^>]+>/", "", $content);
      $content = preg_replace("/]+\>/i", "" />", $content);

    }

    return $content;
}
}

How to use a current page marker in Wordpress!

Saturday, September 6th, 2008

“Current page markers” are a great way for web designers to illustrate to the reader what page they are currently viewing. “Current page markers” use the same principle as breadcrumb trails but are generally less obtrusive. “Current page markers” are suitable for small to medium sized websites. (more…)