Programming in WordPress Way

February 1, 2008 by hejian

Insert a article

// Create post object
$my_post = array();
$my_post['post_title'] = ‘My post’;
$my_post['post_content'] = ‘This is my post.’;
$my_post['post_status'] = ‘publish’;
$my_post['post_author'] = 1;
$my_post['post_category'] = array(8,39);
// Insert the post into the database
wp_insert_post( $my_post );

Get Links
Home page link:

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

Category link:

<a href="<?php echo get_category_link($category_id); ?>">category name</a>

Post link:

<a href="<?php echo get_permalink($post_id); ?>">post name</a>

Template directory:

<input id="searchsubmit" type="image" src="<?php bloginfo('template_directory'); ?>/images/btn_search.gif" alt="Submit" />

Display lists
Page list:

wp_list_pages('title_li=' );

Category list:

wp_list_categories('child_of=[id]&title_li=&hide_empty=0&orderby=id’);

Post list:

$r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0&post_status=publish&category_name=Tours");
if ($r->have_posts()) :
?>
<ul>
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?> </a></li>
<?php endwhile; ?>
</ul>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
endif;
?>

Check if user has login

if ($user_ID) {
/* user has login
}

Custom FIelds
Display custom fIelds in template:

<?php the_meta(); ?>

Display specified custom field:

<?php echo stripslashes(get_post_meta(get_the_ID(), $key, true)); ?>

Check permission

if(!current_user_can('publish_posts'))
$this->auth_required('Sorry, you do not have the right to edit/publish new
posts.');

Leave a Reply

You must be logged in to post a comment.

Wordpress template made by HeJian