飞龙博客

feilong.org 前端开发教程

three ways display order posts meta value

飞龙更新于 2010-11-06 12:25 加入书签 CTRL+D 有 14 个朋友来过

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 = '<img src="'.get_bloginfo('template_directory').'/flroll/hot-8080.jpg"';
$attchfirst .= 'alt="no attach default" width="80" height="80" border="0" />';
}
return $attchfirst;
}

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

<?php $args = 'cat=1&tag=&orderby=meta_value_num&meta_key=views&order=ASC&showposts=3';
query_posts($args);if (have_posts()) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<dl>
<dt><a href="<?php the_permalink(); ?>"><?php echo get_attchfirst($post->ID,'thumbnail'); ?></a></dt>
<dd><h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?> "><?php if(function_exists('the_views')) { the_views(); } the_title(); ?></a></h4>
<p><span class="market">$<?php echo get_post_meta($post->ID, 'price', true); ?></span> <span class="vip">$<?php echo get_post_meta($post->ID, 'new_price', true); ?></span></p></dd>
</dl>
<?php endwhile; else : endif; wp_reset_query();?>

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

<?php
 
//In functions.php: 
//special box to display posts thumbnails titles and excerpt orderby custom value .dl-dtdd-h3p wwww.feilong.org 20101106
function dlh4p_list ($size='thumbnail',$curr='$', $catid=1,$showposts=8,$orderby='modified',$order='DESC',$meta_key ='',$tagid='') {
global $post;
$args = 'cat='.$catid.'&showposts='.$showposts.'&orderby='.$orderby.'&order='.$order.'&meta_key='.$meta_key.'&tag='.$tagid;
query_posts($args);if (have_posts()) :
while ( have_posts() ) : the_post();
echo '<dl>'."\n";
echo '<dt><a href="'.get_permalink().'">'.get_attchfirst($post->ID,$size).'</a></dt>'."\n";
echo '<dd><h4><a href="'.get_permalink().'" title="'.get_the_title().'">';
if(function_exists('the_views')) { the_views(); } //need wp-postviews plugin
the_title();
echo '</a></h4>'."\n";
echo '<p><span class="market">'.$curr.get_post_meta($post->ID, 'price', true).'</span> <span class="vip">'.$curr.get_post_meta($post->ID, 'new_price', true).'</span></p></dd>'."\n";
echo '</dl>'."\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.

<?php
 
//In functions.php: special box dl-dtdd-h3p wwww.feilong.org 20101106
function specialbox ($size='thumbnail',$curr='$',$catid=1,$numberposts=8,$orderby='modified',$order='DESC',$meta_key ='') {
global $post;global $wpdb;
$args = 'category='.$catid.'&numberposts='.$numberposts.'&orderby='.$orderby.'&order='.$order.'&meta_key='.$meta_key;
$listposts=get_posts($args);
foreach($listposts as $post) :
setup_postdata($post);
echo '<dl><dt><a href="';
the_permalink();
echo '">'.get_attchfirst($post->ID,$size)."</a></dt>\n";
echo '<dd><h4><a href="';
the_permalink();
echo '" title="';
the_title();
echo ' ">';
if(function_exists('the_views')) { the_views(); }
the_title();
echo '</a></h4><p><span class="market">'.$curr.get_post_meta($post->ID, 'price', true).'</span> <span class="vip">'.$curr.get_post_meta($post->ID, 'new_price', true)."</span></p></dd></dl>\n";
endforeach;wp_reset_query();
}
 
//Sample usage: somewhere in your wordpress theme
specialbox ('medium','%',1,3,'meta_value_num','ASC','views');
 
?>

维护小站,感谢赞赏。
联系飞龙,请转淘宝

飞龙初发:
2010-11-06 12:25
本文更新网址:
https://feilong.org/three-ways-display-order-posts-meta-value

所在目录: wordpress

新作:

旧文:

  • html css js
  • angular react vue
  • flutter python