调用最新、热门、随机文章、评论、类别、月份、标签、页面

收集了一下wordpress主题使用过程中最新、热门、随机文章、评论、类别、月份、标签、页面的相关调用,可以使用wordpress自带的widget侧边栏的小工具功能来实现,当然也有一些可以使用插件来实现这个功能的,不过使用插件始终对SEO方面有一定的影响,下面整理归纳一下

wordpress最新、热门、随机文章的调用方法。

最新文章

<?php
$postslist = get_posts('numberposts=10&order=DESC&orderby=id');
foreach( $postslist as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>(<?php echo $post->comment_count; ?>)</li>
<?php endforeach; ?>

热门文章

<?php
 $postslist = get_posts('numberposts=10&order=DESC&orderby=comment_count');
 foreach( $postslist as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>(<?php echo $post->comment_count; ?>)</li>
<?php endforeach; ?>

浏览最多文章

在 functions.php文件的最后一个 ?> 前面添加下面的代码:

/// get_most_viewed_format
/// 函数作用:取得阅读最多的文章
function get_most_viewed_format($mode = '', $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') {
  global $wpdb, $post;
  $output = '';
  $mode = ($mode == '') ? 'post' : $mode;
  $type_sql = ($mode != 'both') ? "AND post_type='$mode'" : '';
  $term_sql = (is_array($term_id)) ? "AND $wpdb->term_taxonomy.term_id IN (" . join(',', $term_id) . ')' : ($term_id != 0 ? "AND $wpdb->term_taxonomy.term_id = $term_id" : '');
  $term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : '';
  $inr_join = $term_id ? "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)" : '';

  // database query
  $most_viewed = $wpdb->get_results("SELECT ID, post_date, post_title, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) $inr_join WHERE post_status = 'publish' AND post_password = '' $term_sql $type_sql AND meta_key = 'views' GROUP BY ID ORDER BY views DESC LIMIT $limit");
  if ($most_viewed) {
   foreach ($most_viewed as $viewed) {
    $post_ID    = $viewed->ID;
    $post_views = number_format($viewed->views);
    $post_title = esc_attr($viewed->post_title);
    $get_permalink = esc_attr(get_permalink($post_ID));
    $output .= "<li>$beforetitle$post_title$aftertitle";
    if ($show_date) {
      $posted = date(get_option('date_format'), strtotime($viewed->post_date));
      $output .= "$beforedate $posted $afterdate";
    }
    $output .= "$beforecount $post_views $aftercount</li>";
   }
  } else {
   $output = "<li>N/A</li>n";
  }
  echo $output;
}

然后使用下面的函数调用:

<?php get_most_viewed_format(); ?>

随机文章

<?php $rand_posts = get_posts('numberposts=10&orderby=rand');
foreach( $rand_posts as $post ) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

评论

<?php 
global $wpdb; 
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, 
comment_post_ID, comment_author, comment_date_gmt, comment_approved, 
comment_type,comment_author_url, 
SUBSTRING(comment_content,1,30) AS com_excerpt 
FROM $wpdb->comments 
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = 
$wpdb->posts.ID) 
WHERE comment_approved = '1' AND comment_type = '' AND 
post_password = '' 
ORDER BY comment_date_gmt DESC 
LIMIT 10"; 
$comments = $wpdb->get_results($sql); 
$output = $pre_HTML; 
$output .= "n<ul>"; 
foreach ($comments as $comment) { 
$output .= "n<li>".strip_tags($comment->comment_author) 
.":" . "<a href="" . get_permalink($comment->ID) . 
"#comment-" . $comment->comment_ID . "" title="on " . 
$comment->post_title . "">" . strip_tags($comment->com_excerpt) 
."</a></li>"; 
} 
$output .= "n</ul>"; 
$output .= $post_HTML; 
echo $output;?>

分类

<?php wp_list_cats('sort_column=name'); ?>

标签云

<?php wp_tag_cloud('smallest=8&largest=36&'); ?> 

归档

<?php wp_get_archives('type=monthly'); ?>

页面列表

<?php wp_list_pages('title_li='); ?>
0 0 投票数
文章评分
订阅评论
提醒
guest

6 评论
内联反馈
查看所有评论
没了|世界
7 年 前

🙁 看不懂代码什么的,也不知道往哪里放

没了|世界
7 年 前
回复给  弋牧

@弋牧 😉 看来以后要多请教你了。话说为什么163邮箱不给注册gra的那啥头像…126都可以…

没了|世界
7 年 前
回复给  弋牧

@弋牧 :mrgreen: 好主意,我回来试试