飞龙博客

妙法莲华经

three ways display order posts meta value

feilong.org 修订于2010-11-06 12:25:42

This post shows three ways to display and order posts by meta value num in dl dt dd h4 and p.it retrieve some values of custom fields,coding by feilong on Nov.6,2010.this post url is http://feilong.org/three-ways-display-order-posts-meta-value.

In order to use these wordpress snippets,you should use wp-postviews plugin first.

And of course,these snippets requires the functon get_attchfirst() to display the first pic of the post

//In functions.php: 
//get the first attachment picture of a post in or out of the loop feilong.org 20101105
function get_attchfirst($postid,$size = 'medium') {
$args = array(
	'order'          => 'ASC',
	'post_type'      => 'attachment',
	'post_parent'    => $postid,
	'post_mime_type' => 'image',
	'post_status'    => null,
	'numberposts'    => 1,
);$attachments = get_posts($args);if ($attachments) {
$attchfirst = wp_get_attachment_image($attachments[0]->ID, $size, false, false);
} else {
$attchfirst = '

ONE WAY:directly write template tags in your wordpress theme,this is the basic way for beginers of wordpress themes



ID,'thumbnail'); ?>

$ID, 'price', true); ?> $ID, 'new_price', true); ?>

TWO WAY: query_posts, turn the template tags above into a function in order to reuse it in wordpress themes.

'."\n";
echo '
'.get_attchfirst($post->ID,$size).'
'."\n"; echo '

'; if(function_exists('the_views')) { the_views(); } //need wp-postviews plugin the_title(); echo '

'."\n"; echo '

'.$curr.get_post_meta($post->ID, 'price', true).' '.$curr.get_post_meta($post->ID, 'new_price', true).'

'."\n"; echo ''."\n"; endwhile; else : echo 'no posts';endif; wp_reset_query(); } //Sample usage: somewhere in your wordpress theme dlh4p_list ('large','¥',1,3,'meta_value_num','DESC','views'); ?>

THREE WAY:get_posts, turn the template tags into function,just another coding.

'.get_attchfirst($post->ID,$size)."
\n"; echo '

'; if(function_exists('the_views')) { the_views(); } the_title(); echo '

'.$curr.get_post_meta($post->ID, 'price', true).' '.$curr.get_post_meta($post->ID, 'new_price', true)."

\n"; endforeach;wp_reset_query(); } //Sample usage: somewhere in your wordpress theme specialbox ('medium','%',1,3,'meta_value_num','ASC','views'); ?>

更新网址:https://feilong.org/three-ways-display-order-posts-meta-value
最初发布:20101106 12:25:42 feilong.org 于广州

加入收藏夹,查看更方便。

所在分类: wordpress

新作:

旧文: