如何在wordpress侧边栏调用文章的当前分类的热门文章列表和调用热门文章的总排行榜列表呢? 其实,只要在侧边栏上面用PHP代码调用相关的函数即可实现。 调用文章的当前分类的热门文章列表的代码如下 优化过的代码: <?php // 获取当前文章的分类 $current_categories = get_the_category(); $current_category_id = 0; // 确保获取到主要分类(第一个分类) if (!empty($current_categories)) { $current_category_id = $current_categories[0]->term_id; // 获取第一个分类ID } // 查询热门文章 $args = array( 'cat' => $current_category_id, // 当前分类ID 'meta_key' => 'views', // 使用views作为排序依据 'orderby' => 'meta_value_num', // 按照数值排序 'order' => 'DESC', // 降序 'posts_per_page' => 10, // 显示10篇文章 ); $popular_posts = new WP_Query($args); if ($popular_posts->have_posts()) : ?> <div class="popular-posts"> <?php // <h3>热门文章</h3> // 注释掉标题 ?> <ul> <?php while ($popular_posts->have_posts()) : $popular_posts->the_post(); ?> <li> <img <?php the_post_thumbnail( array(60,60) ); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; ?> </ul> </div> <?php endif; wp_reset_postdata(); // 重置查询 ?> 调用热门文章的总排行榜列表 优化过的代码: <?php // 查询所有热门文章 $args = array( 'meta_key' => 'views', // 使用views作为排序依据 'orderby' => 'meta_value_num', // 按照数值排序 'order' => 'DESC', // 降序 'posts_per_page' => 10, // 显示10篇文章 ); $popular_posts = new WP_Query($args); if ($popular_posts->have_posts()) : ?> <div class="popular-posts"> <?php // <h3>热门文章</h3> // 注释掉标题 ?> <ul> <?php while ($popular_posts->have_posts()) : $popular_posts->the_post(); ?> <li> <img <?php the_post_thumbnail( array(60,60) ); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; ?> </ul> </div> <?php endif; wp_reset_postdata(); // 重置查询 ?> 看,是不是很简单: 未经允许不得转载:泥人传说 » 【纯代码实现】wordpress侧边栏调用文章当前分类的热门文章列表+侧边栏调用热门文章总排行榜列表 wordpress侧边栏调用热门文章wordpress侧边栏调用热门文章列表wordpress调用当前分类热门文章wordpress调用当前分类热门文章列表wordpress调用热门文章总排行榜纯代码实现侧边栏调用热门文章列表 分享到: 赞(0)