文章页底部太单调添加过后说可增加用户滞留性XXX的。反正加就对了
参考了2个人
1、在主题函数文件functions.php里面添加下面的内容
- /**
- * Related posts
- *
- * @global object $post
- * @param array $args
- * @return
- */
- global $post;
-
- // default args
- 'taxonomy' => 'category',
- 'limit' => 10,
- 'orderby' => 'date',
- 'order' => 'DESC'
- ));
-
- // check taxonomy
- if (!taxonomy_exists($args['taxonomy'])) {
- return;
- }
-
- // post taxonomies
-
- return;
- }
-
- // query
- 'post_type' => $args['post_type'],
- 'taxonomy' => $args['taxonomy'],
- 'field' => 'term_id',
- 'terms' => $taxonomies
- ),
- ),
- 'posts_per_page' => $args['limit'],
- 'orderby' => $args['orderby'],
- 'order' => $args['order']
- ));
-
- include( locate_template('related-posts-template.php', false, false) );
-
- wp_reset_postdata();
- }
2、新建一个related-posts-template.php文件,添加以下内容:
- <div class="related-posts">
- <h3 class="widget-title"><?php _e('相关文章', ''); ?></h3>
-
- <ul class="tab-bd">
- <?php
- foreach ($related_posts as $post) {
- setup_postdata($post);
- ?>
- <li><span class="post_spliter">•</span>
- <a class="title" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
- <?php if (has_post_thumbnail()) { ?>
- <div class="thumb">
- </div>
- <?php } ?>
- <?php the_title(); ?>
- </a>
- </li>
- <?php } ?>
- </ul>
- <div class="clearfix"></div>
- </div>
- <?php
- }
如果要调用相关文章代码,只需要在你主题合适的位置,通常是添加到single.php文件里面。
1、显示3个相关文章内容代码
- <div class="kratos-hentry2">
- <div class="kratos-post-content">
- <?php wcr_related_posts(); ?>
- </div>
- </div>
接着参考另一人的CSS,不然这样很难看
- /*********相关文章***********/
- .related-posts{
- padding: 10px 0px 8px 0px;
- }
- .kratos-hentry2 {
- background-color: #fff;
- box-shadow: 0 2px 4px rgba(0,0,0,.1);
- border-radius: 0;
- -webkit-transition: all .6s ease;
- transition: all .6s ease
- }
- .post_spliter{float:left;
- margin-right:10px;
- margin-left:0px;
- color:#999}
- .tab-bd{padding:10px 20px;margin-top:-1px}
- .tab-bd li{float:left;
- text-align: left;
- width:50%;
- line-height:150%;
- white-space:nowrap;
- word-wrap:normal;
- text-overflow:ellipsis;
- overflow:hidden;
- padding:0 15px 5px 0px}
- .tab-bd li a{color:#555;font-size:15px;}
- .tab-bd li a:hover{color:#333;
大功告成,完美!
代码来自:https://blog.naibabiji.com/skill/wordpress-xiang-guan-wen-zhang.html
CSS扒自:https://guihet.com/page-and-page-wp.html