Subscribe to our feed...

http://feeds.feedburner.com/design-notes

  • Push Your Designs To Perfection

    goldSCHNITT is a small stand alone application for MAC that helps you apply golden ration to your design. Golden ratio(1.61803) is believed as a ratio of perfection. It has been used by many artists and architects through the history.

    Now we have this small app called goldSCHNITT to help you implement golden ration into your design.

30+ Wordpress snippets collection (1)

Under: Wordpress 7 comments

Its painful to look everywhere for a little piece of code. Here I listed 30 commonly used snippets. Hope this helps people develop their themes.

Header

Meta

<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" />

Dynamic title

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

Link file:

<script type="text/javascript" src="<?php echo bloginfo('template_directory') .'/lib/jquery-1.3.2.js'; ?>"></script>

RSS

<div id="rss"> <a href="<?php bloginfo('rss2_url'); ?>" title="Subscribe to us"></a> </div>

List parent category only:

<?php wp_list_categories('sort_column=menu_order&depth=1&title_li=');?>

Display child category:

<?php if (is_category()){wp_list_categories('orderby=id&show_count=0&title_li=0&child_of='.$cat); }?>

List pages

<?php wp_list_pages('title_li=&depth=1') ?>

Display subpage:

<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
<?php echo $children; ?>
<?php } ?>
 
<?php if (is_category()){wp_list_categories('orderby=id&show_count=0&title_li=0&child_of='.$cat); }?>

Index

The Loop

<?php if (have_posts()) : ?>
 
<?php while (have_posts()) : the_post(); ?>
 
.......
 
<?php endwhile; ?>
 
<div class="navigation"> <span class="previous-entries">
<?php next_posts_link('Older Entries') ?>
</span> <span class="next-entries">
<?php previous_posts_link('Newer Entries') ?>
</span> </div>
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>

Post title plus permarlink

<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a></h1>

Display date categories and comment counts

<?php the_time('F jS, Y') ?>
<?php the_category(',') ?>
<?php comments_number('no comment','1 comment','% comments'); ?>

Edit post

<?php edit_post_link('edit','',''); ?>

Display highlight images with excerpt. This code displays random thumbnail images from media gallery.

<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'rand'
);
$attachments = get_posts($args);
$noimages = count($attachments);
 
if ($attachments) {
 
foreach ($attachments as $attachment) {
$alttxt = $attachment->post_title;
$imgid = $attachment->ID;
$fileurl = $attachment->guid;
 
$meta = wp_get_attachment_metadata($imgid);
$imgw = $meta['sizes']['thumbnail']['width'];
$imgh = $meta['sizes']['thumbnail']['height'];
 
$imgext = substr($fileurl, -4);
$fileurl = substr($fileurl, 0, -4);
$fileurl = $fileurl."-".$imgw."x".$imgh.$imgext;
 
// construct the image
echo "<img src='".$fileurl."' alt='".$alttxt."' class='alignleft highlightimg' />";
break;
}
}
the_excerpt('');
?>

Display tags

<?php the_tags('Tagged with: ',' &bull; ','<br />'); ?>

Spread the word on twitter

<a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Click to send this page to Twitter!" target="_blank">Twitter</a>

Comment RSS

<a href="<?php comments_rss_link('RSS 2.0'); ?>" title="Comment RSS" target="_blank"> Follow responses</a>

Jump to comment form

<a href="#respond" title="To comment form" target="_blank">leave a response</a>

Trackback

<a href="<?php trackback_url(display); ?>" title="Trackback" target="_blank">Trackback</a>

Sidebar

Dynamic Sidebar

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('right-sidebar') ) : ?>
 
<?php endif; ?>

Your own tag cloud code: the texts varies from 8px to 36px

 <h2>Tags</h2>
<?php wp_tag_cloud('smallest=8&largest=36&'); ?>

Monthly Archive

 <?php wp_get_archives('type=monthly'); ?>

Footer

<div id="footer">
<div class="foot">© Copryright
<?php bloginfo('title') ?>
. All Rights Reserved.</div>
<?php wp_footer(); ?>
</div>

Other

Display certain page named ‘About’

<?php
query_posts('pagename=about');
?>

Include custom file

<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>

Get posts in category 1 from out side a loop and store them as ‘feature posts’

<?php
$feature_posts = get_posts('category=1&numberposts=5&orderby=post_name&order=DSC');
foreach($feature_posts as $post) : setup_postdata($post);
?>
 
...<?php endforeach; ?>

Get custom field(image url in this case):

<?php $image = get_post_meta($post->ID, 'image', TRUE); ?>
<?php if($image) { ?>
<img src="<?php echo $image; ?>" alt="Alt Text" />
<?php } ?>
<?php //get article_image (custom field) ?>
<?php $image = get_post_meta($post->ID, 'article_image', true); ?>
<a href="<?php the_permalink() ?>"><img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" /></a>

List Radom Posts

<ul>
 
<?php
$rand_posts = get_posts('numberposts=10&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php the_time('M d Y'); ?>
</li>
<?php endforeach; ?>
</a>
</ul>

List Posts in Category 1 and offset 1

<ul>
 
<?php
global $post;
$myposts = get_posts('numberposts=10&offset=1&category=1');
foreach($myposts as $post) :
?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php the_time('M d Y'); ?>
</li>
<?php endforeach; ?>
</a>
</ul>

List posts with certain tag

<ul>
 
<?php
$myposts = get_posts('numberposts=10&tag=yourtagname');
foreach( $myposts as $post) :
?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php the_time('M d Y'); ?>
</li>
<?php endforeach; ?>
</a>
</ul>

List posts by dates (time followed by posts title):

It is good to use in date.php:

<li>
<?php the_time('d.m.y') ?> | <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
 
</li>

Images

list all image attachments

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 
<ul><?php
$post_parent = get_post($post->ID, ARRAY_A);
$parent = $post_parent['post_parent'];
 
$attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
foreach($attachments as $id => $attachment) :
echo wp_get_attachment_link($id, 'thumbnail', true);
endforeach;
?>
</ul>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Functions

Register sidebars with names

<?php
if ( function_exists('register_sidebars') )
register_sidebar(array(
'name' => 'sidebar',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>",
'before_title' => "<h3 class="widgettitle">",
'after_title' => "</h3>",
));
 
register_sidebar(array(
'name' => 'yourname1',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>",
'before_title' => "<h3 class="widgettitle">",
'after_title' => "</h3>",
));
 
register_sidebar(array(
'name' => 'sidebar2',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>",
'before_title' => "<h3 class="widgettitle">",
'after_title' => "</h3>",
));
?>

Code resources

ADD TOAdd To Evernote

If this helps, why not tell your friends?

  • Reddit
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Technorati
  • Design Float
  • Identi.ca
  • FriendFeed
  • Yahoo! Buzz
  • Ping.fm

7 Responses to “ 30+ Wordpress snippets collection (1) ”

  • Justin says:

    Great stuff, especially the sidebar registering. I’ve always wondered how to get more hooks into the widgets!

    Only thing is, there’s a misc. ‘n’ at the end of the ‘after_title’ value.

    Thanks so much!

    [Reply]

    alexandra Reply:

    Thanks for pointing out the mistake.

    [Reply]

  • JohnB says:

    Great source

    Is it my eyes or is the code for
    List parent category only: the same as Display child category: and should it be??

    I’m am very much a php learner!

    [Reply]

    alexandra Reply:

    thanks for pointing out. This post was messed up by a plug-in, I fixed it really quickly and made a mistake. I listed the child category code now.

    [Reply]

  • paul says:

    great

    would be great to make it a printable PDF!

    [Reply]

  • Leave a Reply

    All fields marked with "*" are required.