Lonesome主题增加相关文章

有朋友需要本站增加的Lonesome主题相关文章功能,实际我也是网上搜了修改的。现在放出给有需要的朋友。

1、以下代码保存为related-posts.php文件:

<div class="related-posts text-center clearfix row"> <h4 class="section-title col-md-12"><span><?php esc_html_e( '相关日志', 'Lonesome' ); ?></span></h4> <?php $post_num = 2; global $post; $tmp_post = $post; $tags = ''; $i = 0; if ( get_the_tags( $post->ID ) ) { foreach ( get_the_tags( $post->ID ) as $tag ) $tags .= $tag->name . ','; $tags = strtr(rtrim($tags, ','), ' ', '-'); $myposts = get_posts('numberposts='.$post_num.'&tag='.$tags.'&exclude='.$post->ID); foreach($myposts as $post) { setup_postdata($post); ?> <div class="related-post col-sm-6 col-xs-6"> <div class="related-posts-border"> <div class="entry-media"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <img src="<?php echo post_thumbnail_src(); ?>?imageView2/1/w/409/h/130/q/100" /> </a> </div> <div class="related-post-title"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> </div> <div class="related-post-date"> <?php the_time( get_option( 'date_format' ) ); ?> </div> </div> </div> <?php } } $post = $tmp_post; setup_postdata($post); ?></div>

2、single.php文件里增加以下代码:

get_template_part( 'template-parts/related-posts' );


增加后的代码如下:

<?php/** * * 文章页面 *  */get_header(); ?> <div class="row"> <div class="col-md-9" id="content"> <?php while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content', 'single' ); if ( get_the_author_meta( 'description' ) ) { get_template_part( 'template-parts/biography' ); } get_template_part( 'template-parts/related-posts' ); // If comments are open or we have at least one comment, load up the comment template. if ( comments_open() || get_comments_number() ) : comments_template(); endif; endwhile; // End of the loop. ?> </div> </div><?php get_footer('single'); ?>

3、css代码根据上面的内容到style里去拷贝。

以上是根据Lonesome主题Version: 2018.02.26

更多修改:

Lonesome主题修改记录

主题修改的太帅了! 博客之路中我正常使用过两个付费主题,一个是O'Connor,大发出品;另一个就是现在使用的Lonesome。最近Lonesome的作者更新到了2.2.0版本...

7 条评论

  1. 今天拿去用了,整体好着,就是调用图这里会出错,不知道啥原因,如果有同样错误的人,可以把
    <img src="?imageView2/1/w/409/h/130/q/100" />
    修改为:

    应该就好了

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注