May 12th 2010

WordPress Cheat Sheet

Tags: ,
Alt Text

Listed below is the code that I use most in WordPress. I will be adding to it on an ongoing basis. If you have a question about how to use any of the code please feel free to add a comment at the bottom of the page.

Returns template directory URL to the active theme.
<?php echo bloginfo('template_directory') ?>

http://yourURL.com/wp-content/themes/yourThemeName

Example:

<img src="<?php echo bloginfo('template_directory').'/images/title.gif'; ?>">

is the same as:

<img src="http://yourURL.com/wp-content/themes/yourThemeName/images/title.gif">

Displays the posts/pages title
<?php the_title() ?>

Displays the content of the post/page
<?php the_content() ?>

Displays the excerpt of the current post/page
<?php the_excerpt() ?>

Displays the time of the current post/page
<?php the_time() ?>

Displays the date of a post or set of post/page
<?php the_date() ?>

Displays the URL for the permalink
<?php the_permalink() ?>

Displays the category of a post
<?php the_category() ?>

Displays the author of the post
<?php the_author(); ?>

Displays the categories
<?php wp_list_cats(); ?>

is_home()
When the user is on the blog home page

Get the Contents of a Custom Field
// Using the “TRUE” value makes sure that the function actually echos the value

<?php $image = get_post_meta($post->ID, 'image', TRUE); ?>

<?php if($image) { ?><img src="<?php echo $image; ?>" alt="Alt Text" /><?php } ?>

The Loop
Posts

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

<?php endwhile; // end of the loop. ?>

Add Sidebar Widgets
<?php include (TEMPLATEPATH . '/sidebar.php'); ?>

is_page(‘Contact’)
<?php if (is_page('Contact')){
If the page title is Contact then this will execute
}

The Category Based Loop

<?php query_posts('category_name=
Category&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
// The Stuff... Custom HTML & PHP Code
<?php endwhile;?>

1 Setting Up A Blog Using Categorys (Please Read Number 2)
1 The Blog Template

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('category_name=blog&showposts=10'.'&paged='.$paged);

while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<!--Print out the info -->
<div class="blogtitle"><a href="<?php the_permalink() ?>"><h2><?php the_title() ?></h2></a></div><div class="blogtime"><?php the_time() ?> | <?php the_date() ?> | <?php the_author(); ?> | <?php the_tags(); ?></div>

<?php $image = get_post_meta($post->ID, 'image_home', TRUE); ?>

<div class="blogimage"><a href="<?php the_permalink() ?>"><img src="<?php echo '/'.$td.'/scripts/timthumb.php?src=/'.$image.'&h=0&w=445&zc=1' ?>" /></a></div>

<div class="blogexcerpt"><?php the_excerpt() ?></div>

<div class="bloghr"><hr></hr></div>

<?php endwhile; ?>

<div class="blogpage"><?php pagination( $wp_query, "/" ); ?></div>

<?php $wp_query = null; $wp_query = $temp;?>

2 Setting Up A Blog Using Categorys
2 The Fuction Page

function pagination( $query, $baseURL ) {
$page = $query->query_vars["paged"];
if ( !$page ) $page = 1;
$qs = $_SERVER["QUERY_STRING"] ? "?".$_SERVER["QUERY_STRING"] : "";
// Only necessary if there's more posts than posts-per-page
if ( $query->found_posts > $query->query_vars["posts_per_page"] ) {
echo '<ul class="paging">';
// Previous link?
if ( $page > 1 ) {
echo '<li class="previous"><a href="'.$baseURL.'blog/page/'.($page-1).'/'.$qs.'">previous</a></li>';
}
// Loop through pages
for ( $i=1; $i <= $query->max_num_pages; $i++ ) {
// Current page or linked page?
if ( $i == $page ) {
echo '<li class="active">'.$i.'</li>';
} else {
echo '<li><a href="'.$baseURL.'blog/page/'.$i.'/'.$qs.'">'.$i.'</a></li>';
}
}
// Next link?
if ( $page < $query->max_num_pages ) {
echo '<li><a href="'.$baseURL.'blog/page/'.($page+1).'/'.$qs.'">next</a></li>';
}
echo '</ul>';
}
}

Template Name
Home

<?php /* Template Name: Home */ ?>

in_category function

if (in_category(‘Web’)

Author Avatar
Change the avatar for the author

In you functions.php file there is a function called “function your theme-name_comment”

Find the line:

<?php echo get_avatar( $comment, 40 ); ?>

Replace with:

$author = get_comment_author_link();

if ($author == "admin"){
echo get_avatar( 'your admin email', $size = '40', $default = 'url to image' );
}

if ($author != "admin"){
echo get_avatar( $comment, 40 );
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>