【优化侧边栏调用】修改wordpress侧边栏主题模板,让它实现各种调用规则 。

目标

修改当前主题的侧边栏模板

 

实现如下功能

  1. 禁用小工具设置项和框架外多余的代码。
  2. 首页侧边栏自动调用全站总热门榜,月榜,周榜。
  3. 分类页侧边栏自动调用当前分类的总热门榜,月榜,周榜。
  4. 文章页侧边栏自动调用当前分类的热门榜,tag相关文章列表,随机文章列表(随机推荐)。

 

 

代码如下:

<! -- 侧边栏上级层级框架开始 -->
//多余Code部分注释掉
<div id="fixed">
<aside class="main-right" id="right">
<?php if (is_home() || is_front_page()) : ?>
<!-- 总热门 -->
<section class="widget hot-posts">
<h3 class="widget-title">全站热门文章</h3>
<ul>
<?php
$hot_query = new WP_Query(array(
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
));
while ($hot_query->have_posts()) : $hot_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<!-- 月榜 -->
<section class="widget month-hot-posts">
<h3 class="widget-title">本月热门</h3>
<ul>
<?php
$month_query = new WP_Query(array(
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'year' => date('Y'),
'month' => date('n'),
),
),
'ignore_sticky_posts' => 1,
));
while ($month_query->have_posts()) : $month_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<!-- 周榜(最近7天) -->
<section class="widget week-hot-posts">
<h3 class="widget-title">本周热门</h3>
<ul>
<?php
$week_query = new WP_Query(array(
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'after' => '7 days ago',
'inclusive' => true,
),
),
'ignore_sticky_posts' => 1,
));
while ($week_query->have_posts()) : $week_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<?php
// 分类页
elseif (is_category()) :
$cat_id = get_queried_object_id();
?>
<!-- 分类热门 -->
<section class="widget cat-hot-posts">
<h3 class="widget-title">本分类热门</h3>
<ul>
<?php
$cat_hot_query = new WP_Query(array(
'cat' => $cat_id,
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
));
while ($cat_hot_query->have_posts()) : $cat_hot_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<!-- 分类月榜 -->
<section class="widget cat-month-hot-posts">
<h3 class="widget-title">本分类月榜</h3>
<ul>
<?php
$cat_month_query = new WP_Query(array(
'cat' => $cat_id,
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'year' => date('Y'),
'month' => date('n'),
),
),
'ignore_sticky_posts' => 1,
));
while ($cat_month_query->have_posts()) : $cat_month_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<!-- 分类周榜(最近7天) -->
<section class="widget cat-week-hot-posts">
<h3 class="widget-title">本分类日榜</h3>
<ul>
<?php
$cat_week_query = new WP_Query(array(
'cat' => $cat_id,
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'after' => '7 days ago',
'inclusive' => true,
),
),
'ignore_sticky_posts' => 1,
));
while ($cat_week_query->have_posts()) : $cat_week_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<?php
// 文章页
elseif (is_single()) :
$post_id = get_the_ID();
$cats = get_the_category($post_id);
$cat_id = $cats ? $cats[0]->term_id : 0;
$tags = wp_get_post_tags($post_id);
$tag_ids = $tags ? wp_list_pluck($tags, 'term_id') : array();
?>
<!-- 本分类热门 -->
<section class="widget single-cat-hot-posts">
<h3 class="widget-title">本分类热门</h3>
<ul>
<?php
$single_cat_hot_query = new WP_Query(array(
'cat' => $cat_id,
'posts_per_page' => 10,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'post__not_in' => array($post_id),
));
while ($single_cat_hot_query->have_posts()) : $single_cat_hot_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<!-- 相关文章(tag) -->
<?php if ($tag_ids): ?>
<section class="widget related-posts">
<h3 class="widget-title">相关文章</h3>
<ul>
<?php
$related_query = new WP_Query(array(
'tag__in' => $tag_ids,
'posts_per_page' => 10,
'post__not_in' => array($post_id),
'ignore_sticky_posts' => 1,
));
while ($related_query->have_posts()) : $related_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<?php endif; ?>
<!-- 你可能感兴趣 -->
<section class="widget random-posts">
<h3 class="widget-title">你可能感兴趣</h3>
<ul>
<?php
$random_query = new WP_Query(array(
'posts_per_page' => 10,
'orderby' => 'rand',
'post__not_in' => array($post_id),
'ignore_sticky_posts' => 1,
));
while ($random_query->have_posts()) : $random_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</section>
<?php endif; ?>
</aside>
</div>
<?php endif; ?>
<! -- 侧边栏上级层级框架结束 -->

 

 

 

样式

根据当前主题去手动添加合适的样式,这里就不再提供了。

 

 

 

 

 

未经允许不得转载:泥人传说 » 【优化侧边栏调用】修改wordpress侧边栏主题模板,让它实现各种调用规则 。
分享到:
赞(0)

评论抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址