Sunday, March 6, 2011

Wordpress Themes / Templates - Useful Codes

Here are few fairly basic but handy wordpress codes that a wordpress theme or template developer/designers could use as reference. i personally use these a lot while working with wordpress.

 

Wordpress Site Name:

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

 

Wordpress Site Description:

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

 

Wordopress: Home Page Link:


<a href="<?php get_option('home'); ?>">Home</a></p>
or
<a href="<?php bloginfo('url'); ?>">Home</a>

 

Wordpress : Navigation – Display List of Pages in <li>

<?php wp_list_pages('title_li=&depth=1&exclude=2,7'); ?>

 

Wordpress Theme/Template Path:

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

 

Wordpress : Inside The Loop

<?php the_permalink() ?>
<?php the_title_attribute(); ?>
Post time: <?php the_time('F jS, Y') ?>
Post Author: <?php the_author() ?>
Post Content: <?php the_content('Read the rest of this entry &raquo;'); ?>
Post Tags: <?php the_tags('Tags: ', ', ', '<br />'); ?>
Post Category: <?php the_category(', ') ?>
Post Edit Link: <?php edit_post_link('Edit', '', ' | '); ?>

 

Wordpress: Display Specific Page/Post Content

display specific  page content with page name

<?php query_posts('pagename=pname'); //pname = your page name?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>    
<?php endwhile; ?>

 display specific  page content with using page_id

<?php query_posts('page_id=1'); //put page id of Home or Stations  in your case?>
<?php while (have_posts()) : the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<p>
<?php
the_content('Read the rest of this entry &raquo;')
?>
</p>
<?php endwhile; ?>

display specific  post content using post id

<?php
// retrieve one post with an ID of 5
query_posts( 'p=5' );
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
the_content( 'Read the full post »' );
endwhile;
?>

 

Wordpress : Get Custom Value

<?php
$cvalArr = get_post_custom_values("custom-val");   
$cval = $cvalArr[0];     
echo $cval
?>

 

Wordpress : Display X Latest Post from Category Y

<?php query_posts('posts_per_page=5&cat=2');
//post_per_page = number of post to grab
//cat = 2 –> from category 2, find your category id from admin
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //the_title(); ?>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" title="Read full view of
<?php the_title_attribute(); ?>">Read more</a>
<?php endwhile; wp_reset_query(); ?>

i am thinking to put series of posts live describing wordpress development, let’s see.

Refs: Codex, Wordpress

http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Function_Reference/WP_Query

0 comments :

Post a Comment