首页>建站相关>wordpress显示相关文章

wordpress显示相关文章

很多主题的文章页面尾部会显示一些与当前文章相关的内容,这样做一方面扩展了页面,让页面看起来更为丰富,同时也给予了站内其他文章更多的展示机会。要达成这样的效果,并不需要依靠插件,只需要在合适的位置添加一段代码:

参考代码

<h3>相关文章</h3>
<ul class="related_posts">
<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
$args = array(
'post_status' => 'publish',
'tag__in' => explode(',', $tags),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i < $post_num ) {
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num - $i
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>没有相关文章!</li>';
?>
</ul>

以上代码会根据文章标签去生成相关的文章,其中post_num定义的所需要展示的文章数量,如果根据标签所搜寻到的文章数量不足八篇,代码会尝试扩大搜索范围从当前文章所属目录中补足,循环结束最终生成一个h3标题与一个无序列表。

修改代码

因为自己的博客每篇文章只设置了一个标签作为小标题使用,所以不准备利用循环来提取标签,常规的编程语言中可以直接给数组加上序号来提取第一个元素,就象这样“变量[0]”,但php中非索引类的数组似乎不允许这样使用,提取数组第一个元素的内容可以使用函数“current()”,同时因为觉得同目录的文章相关性没有同标签的文章来的强,所以取消了从目录提取补足文章数量的这一段,因为缩小了搜索范围,很容易就出现没有相关文章的情况,干脆把整个模块放进了判断内,没有文章的时候不显示整个模块,所以这段代码里也没有了“没有相关文章”的提示:

<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags();
if($posttags){
$args = array(
'post_status' => 'publish',
'tag__in' => current($posttags)->term_id,
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,);
query_posts($args);
if(have_posts()){
echo '<h3 class="related-title">相关文章</h3>';
echo '<ul class="related-posts">';
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" class="related-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $exclude_id.=','.$post->ID;}
echo '</ul>';}
wp_reset_query();
}?>

添加元素

代码中我们只输出了相关文章的标题,事实上这段代码的“while(have_posts()){}”中的逻辑跟主循环是一致的,我们可以跟首页文章列表一样往里面添加一些其他的元素,比如文章摘要,输出文章摘要的语句如下:

<?php the_excerpt(); ?>

修改后的代码如下:

<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags();
if($posttags){
$args = array(
'post_status' => 'publish',
'tag__in' => current($posttags)->term_id,
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,);
query_posts($args);
if(have_posts()){
echo '<h3 class="related-title">相关文章</h3>';
echo '<ul class="related-posts">';
while( have_posts() ) { the_post(); ?>
<li>
<a rel="bookmark" class="related-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</li>
<?php $exclude_id.=','.$post->ID;}
echo '</ul>';}
wp_reset_query();
}?>

以上代码会根据标签提取最多8篇相关文章,输出文章的标题与摘要,如果该标签下没有相关文章,则不生成相关文章模块。

WP_Query的实现方式

因为WP_Query是更为推荐的实现方式,这里也附上了WP_Query的实现方案,供参考:

<?php $categories = get_the_category(get_the_ID()); ?>
<?php if($categories): ?>
    <?php $args = array(
        'category__in' => array($categories[0]->term_id),
        'post__not_in' => array(get_the_ID()),
        'posts_per_page' => 4,
        'ignore_sticky_posts' => 1,
    ); ?>
    <?php $postlists_query = new WP_Query($args); ?>
    <?php if ($postlists_query->have_posts()) :?>
        <?php $term = get_term_by('id', $categories[0]->term_id, 'category'); ?>
        <div class="content-module">
            <h4 class="content-module-title">相关阅读
                <a class="content-module-more" href="<?php echo get_term_link($term); ?>"><span class="content-module-more-i"></span></a>
            </h4>
            <div class="content-postlist">
            <?php while ($postlists_query->have_posts()) : $postlists_query->the_post(); ?>
                <article class="content-postlist-item">
                    <div class="content-postlist-img">
                        <?php if (has_post_thumbnail()): ?>
                            <?php the_post_thumbnail(); ?>
                        <?php else: ?>
                            <img src="<?php echo TBS_IMAGE_URI.'default_thumb.jpg'; ?>" alt="<?php echo get_the_title(); ?>">
                        <?php endif; ?>
                    </div>
                    <div class="content-postlist-info">
                        <h3 class="content-postlist-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <p class="content-postlist-excerpt"><?php echo get_the_excerpt(); ?></p>
                        <?php $tags = get_the_tags(); ?>
                        <?php if (!empty($tags)): ?>
                            <div class="content-postlist-meta">
                                <span class="content-postlist-tags">
                                <?php foreach ($tags as $tag): ?>
                                    <a href="<?php echo get_tag_link($tag->term_id); ?>">
                                        <i class="fa fa-fw fa-tag" aria-hidden="true" style=""></i><?php echo $tag->name; ?>
                                    </a>
                                <?php endforeach; ?>
                                </span>
                             </div>
                        <?php endif; ?>
                    </div>
                </article>
            <?php endwhile; ?>
            </div>
        </div>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
<?php endif; ?> 

标签: wordpress

移动端可扫我直达哦~

推荐阅读

wordpress 2025-03-31

wordpress 重新生成缩略图

测试wp缩略图功能的时候不小心把所有的缩略图都删掉了,但保留了原图,不想一个个重新上传以生成缩略图,就使用了这段代码,注意生成成功之后,这段代码就可以删掉了。忽然想起之前钉钉宣传的某个功能,阅后即焚~~function regener...

建站相关 wordpress

wordpress 2025-03-29

WordPress的主循环与WP_Query

WordPress的主循环和WP_Query是主题开发中最重要的两个概念,它们负责从数据库获取内容并显示在页面上。主循环 (The Loop)主循环是WordPress用来显示文章的核心机制。它是一个PHP代码结构,用于遍历当前页面请...

建站相关 wordpress

wordpress 2025-03-22

在phpstudy中为wordpress开启伪静态

原文修改主题都是在服务器上一边在线修改一边调试,用上了phpstudy后才发现自己之前的方式有多没有效率。但测试设置固定链接的时候遇到了一个问题,就是设置前也无风雨也无晴,设置后统一返回404。这个问题之前尝试搭建站点的时候也遇到过,...

建站相关 wordpress

wordpress 2025-03-17

WordPress分页中遇到404错误:posts_per_page

这个问题可能更多为主题开发者所遇见,一款推向市场的主题一般都会几经测试,应该不容易到客户手中才发现这个问题。所以网络上的相关讨论不多,博主也是调试了几天才大致有了一些思路:后台的默认参数在wordpress的后台设置里,是可以设置归档...

建站相关 wordpress

wordpress 2025-03-14

WORDPRESS HEADER模块常用函数

在 WordPress 开发中,header.php 文件是主题的重要组成部分,用于定义网站的头部内容。以下是一些在 header.php 中常用的 WordPress 函数及其用途,如果嫌部分函数生成的默认模板不需要的元素过多,也可...

建站相关 wordpress

wordpress 2025-03-12

wordpress的前后台数据交换ajax

ajax是个耳熟能详的词儿,但因为有点儿复杂,博主一直是规避学习的,今天刚好碰到一个前台jquery向wp后台申请数据的问题。躲不过,那就慢慢调试吧。钩子wp的ajax还区分了用户,对于不同的用户(登录与否)采用不同的钩子,不过这里只...

建站相关 wordpress

wordpress 2025-03-07

WordPress中add_meta_box函数参数详解

add_meta_box 是 WordPress 中用于在后台编辑页面添加自定义元框(Meta Box)的函数。它允许开发者在文章、页面、自定义文章类型等编辑页面中添加自定义字段或内容。以下是 add_meta_box 函数的参数及其...

建站相关 wordpress

wordpress 2025-03-07

wordpress 手动添加自定义字段

自定义字段可以扩展文章的信息,也有很多相关的成熟的插件,比如Advanced Custom Fields (ACF) 插件,如果希望添加的字段不多,也不愿意为此安装过多的插件,我们也可以考虑手动来添加它。为post文章添加字段// 添...

建站相关 wordpress

wordpress 2025-03-07

wordpress 自定义文章类型

在 WordPress 中,Post Type(文章类型)是指不同类型的内容,例如文章(Post)、页面(Page)、自定义文章类型等。每种文章类型可以使用不同的模板来显示其内容。以下是关于 WordPress 文章类型模板的基本信息...

建站相关 wordpress