WordPress Code Snippets

Dynamically Insert the URL for Your WordPress Website

This is particularly useful to place in the logo link or main navigation of your WordPress theme, as it adjusts to the URL of the particular installation of WordPress.

<?php bloginfo('url'); ?>

This example wraps a link to the site home around the name of the site:

<a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a>

Source: Designing Headers: WordPress Codex

Basic Nav of Pages and Home

<div id="nav">
 <ul>
 <li<?php
 if (is_home())
 {
 echo " class=\"current_page_item\"";
 }?>><a href="<?php bloginfo('url'); ?>">Home</a></li>
 <?php wp_list_pages('title_li=&depth=3'); ?>
 </ul>
 </div><!-- closes nav div -->

Dynamically Insert URL to the Site Home

<?php echo get_option('home'); ?>/

Insert URL to Stylesheet Directory

<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/assets/css/print.css" media="print" />

Specialized Loop: Specific Category and Specific Number of Posts

<?php
// retrieve posts from category ID 5 & Limit to 4 posts
query_posts('cat=5&showposts=4'); 
?>

<?php while ( have_posts() ) : the_post() ?>
<dl>
<dt><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php htmlentities(the_title()); ?>"><?php the_title(); ?></a></dt> 
<dd>
<?php the_excerpt('Read the rest of this story &raquo;'); ?>
</dd>
</dl>
<?php endwhile; ?>

Alternative Ways to Display Posts from Specific Categories

Here is a bit of Wordpress code for pulling a post of a specific category. Limited functionality: title, author, excerpt (not content).

	<?php		$myposts = get_posts('numberposts=10&category=15');		foreach($myposts as $post) :
	?>
		<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
        		<p><?php the_excerpt('Read the rest of this story &raquo;'); ?></p>
                	<?php endforeach; ?>

Breadcrumbs

Code for displaying name and link to parent categories, followed by current category:

<?php echo(get_category_parents($cat, TRUE, ' &raquo; ')); ?>

Code for displaying name and link to parent page, followed by current page:

Dynamically Generate Page Title and Site Name in the Title Tag

<title>
<?php if (is_home()) { echo bloginfo('name');
} elseif (is_404()) {
echo '404 Not Found &raquo; '; bloginfo('name');
} elseif (is_category()) {
echo wp_title(' &raquo; ',true,'right'); bloginfo('name');
} elseif (is_search()) {
echo 'Search Results &raquo; '; bloginfo('name');
} elseif ( is_day() || is_month() || is_year() ) {
echo wp_title(' &raquo; ',true,'right'); bloginfo('name');
} else {
echo wp_title(' &raquo; ',true,'right'); bloginfo('name');
}
?>
</title>

Pull Excerpt with no Markup (no p tags)

<?php the_excerpt_rss(); ?>

Get Custom Fields

<?=get_post_meta($post->ID, 'FlickrImageURL', true)?>

RSS Feeds

Include an RSS feed from any source. Line one includes the necessary function from the WordPress core. Line two includes the URL of the feed, followed by the number of RSS entries you want to display.

<?php include_once(ABSPATH.WPINC.'/rss.php');
wp_rss('http://feeds.feedburner.com/wprecipes', 3); ?>