Bhupal Sapkota Software Consultant Helping founders build
and launch their ideas with code & AI

Bhupal's Newsletter

I send out occassional email with programming resources, summaries of my readings on art, science, and commerce behind technology, my writings on growing an online business etc. Feel free to subscribe, no spam!

Home

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
//category_name=manasha  -> to display posts from category slug
?>
<?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

Category: technology
Tagged with:

Copy & Share

WordPress Themes / Templates – Useful Codes
https://bhupalsapkota.com/wordpress-themes-templates-useful-codes/

Your thoughts? Please leave a comment

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