wordpress给文章增加相关推荐的方法汇总

de

cl

ss="ent

y-he

dest-no-met

"> de

>

n id="toc">代码版方法一<

sp

n><

h2>
ong>修改主题文件前记得先备份,有问题再还原。<

st

ong> 1、在主题函数文件functions.php里面添加下面的内容 e> n cl

ss="hljs-comment">/**
*

el

ted posts
*
* n cl

ss="hljs-doct

g">@glob

l n> object $post * n cl

ss="hljs-doct

g">@p

m<

sp

n> y $gs * n cl

ss="hljs-doct

g">@

etu

n n> *

n> n cl

ss="hljs-function">n cl

ss="hljs-key

o

d">function<

sp

n> n cl

ss="hljs-title">

c

_

el

ted_posts<

sp

n>n cl

ss="hljs-p

ms">($

gs =

y()) n> <

sp

n>{ n cl

ss="hljs-key

o

d">glob

l n> $post; n cl

ss="hljs-comment">

/ def

ult gs<

sp

n> $gs =

p_p

se_

gs($

gs, n cl

ss="hljs-key

o

d">

y n>( n cl

ss="hljs-st

ing">'post_id' n> => !n cl

ss="hljs-key

o

d">empty<

sp

n>($post) ? $post->ID : n cl

ss="hljs-st

ing">'' n>, n cl

ss="hljs-st

ing">'t

xonomy'<

sp

n> => n cl

ss="hljs-st

ing">'c

tego

y' n>, n cl

ss="hljs-st

ing">'limit' n> => n cl

ss="hljs-numbe

">3 n>, n cl

ss="hljs-st

ing">'post_type' n> => !n cl

ss="hljs-key

o

d">empty<

sp

n>($post) ? $post->post_type : n cl

ss="hljs-st

ing">'post' n>, n cl

ss="hljs-st

ing">'o

de

by' n> => n cl

ss="hljs-st

ing">'d

te'<

sp

n>, n cl

ss="hljs-st

ing">'o

de

' n> => n cl

ss="hljs-st

ing">'DESC' n> )); n cl

ss="hljs-comment">

/ check t

xonomy<

sp

n> n cl

ss="hljs-key

o

d">if<

sp

n> (!t

xonomy_exists($

gs[ n cl

ss="hljs-st

ing">'t

xonomy' n>])) { n cl

ss="hljs-key

o

d">

etu

n<

sp

n>; } n cl

ss="hljs-comment">

/ post t

xonomies<

sp

n> $t

xonomies =

p_get_post_te

ms($

gs[ n cl

ss="hljs-st

ing">'post_id'<

sp

n>], $gs[n cl

ss="hljs-st

ing">'t

xonomy'<

sp

n>], n cl

ss="hljs-key

o

d">y<

sp

n>(n cl

ss="hljs-st

ing">'fields' n> => n cl

ss="hljs-st

ing">'ids' n>)); n cl

ss="hljs-key

o

d">if<

sp

n> (n cl

ss="hljs-key

o

d">empty<

sp

n>($t

xonomies)) {
n cl

ss="hljs-key

o

d">

etu

n n>; } n cl

ss="hljs-comment">

/ que

y<

sp

n> $

el

ted_posts = get_posts(n cl

ss="hljs-key

o

d">y<

sp

n>( n cl

ss="hljs-st

ing">'post__not_in' n> => (n cl

ss="hljs-key

o

d">y<

sp

n>) $gs[n cl

ss="hljs-st

ing">'post_id' n>], n cl

ss="hljs-st

ing">'post_type' n> => $gs[n cl

ss="hljs-st

ing">'post_type' n>], n cl

ss="hljs-st

ing">'t

x_que

y' n> => n cl

ss="hljs-key

o

d">y<

sp

n>( n cl

ss="hljs-key

o

d">y<

sp

n>( n cl

ss="hljs-st

ing">'t

xonomy'<

sp

n> => $gs[n cl

ss="hljs-st

ing">'t

xonomy'<

sp

n>], n cl

ss="hljs-st

ing">'field' n> => n cl

ss="hljs-st

ing">'te

m_id'<

sp

n>, n cl

ss="hljs-st

ing">'te

ms'<

sp

n> => $t

xonomies
),
),
n cl

ss="hljs-st

ing">'posts_pe

_p

ge'<

sp

n> => $gs[n cl

ss="hljs-st

ing">'limit' n>], n cl

ss="hljs-st

ing">'o

de

by' n> => $gs[n cl

ss="hljs-st

ing">'o

de

by' n>], n cl

ss="hljs-st

ing">'o

de

' n> => $gs[n cl

ss="hljs-st

ing">'o

de

' n>] )); n cl

ss="hljs-key

o

d">include<

sp

n>( loc

te_templ

te(n cl

ss="hljs-st

ing">'

el

ted-posts-templ

te.php'<

sp

n>, n cl

ss="hljs-key

o

d">f

lse n>, n cl

ss="hljs-key

o

d">f

lse n>) );

p_

eset_postd

t

(); }<

p

e> 2、新建一个

el

ted-posts-templ

te.php文件,添加以下内容:

n cl

ss="php">n cl

ss="hljs-met

"><?php n> n cl

ss="hljs-key

o

d">if<

sp

n> (!n cl

ss="hljs-key

o

d">empty<

sp

n>($

el

ted_posts)) { n cl

ss="hljs-met

">?> n><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">div<

sp

n> n cl

ss="hljs-

tt

">cl

ss n>=n cl

ss="hljs-st

ing">"

el

ted-posts" n>><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">h3<

sp

n> n cl

ss="hljs-

tt

">cl

ss n>=n cl

ss="hljs-st

ing">"

idget-title"<

sp

n>><

sp

n>n cl

ss="php">n cl

ss="hljs-met

"><?php n> _e(n cl

ss="hljs-st

ing">'相关文章' n>, n cl

ss="hljs-st

ing">'' n>); n cl

ss="hljs-met

">?> n><

sp

n>n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">h3<

sp

n>><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">ul<

sp

n> n cl

ss="hljs-

tt

">cl

ss n>=n cl

ss="hljs-st

ing">"

el

ted-posts-list" n>><

sp

n> n cl

ss="php">n cl

ss="hljs-met

"><?php n> n cl

ss="hljs-key

o

d">fo

e

ch<

sp

n> ($

el

ted_posts n cl

ss="hljs-key

o

d">

s n> $post) { setup_postd

t

($post); n cl

ss="hljs-met

">?> n><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">li<

sp

n>><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">

n> n cl

ss="hljs-

tt

">cl

ss n>=n cl

ss="hljs-st

ing">"title" n> n cl

ss="hljs-

tt

">h

ef n>=n cl

ss="hljs-st

ing">"<?php%20the_pe

m

link();%20?>" n> n cl

ss="hljs-

tt

">title<

sp

n>=n cl

ss="hljs-st

ing">"<?php the_title_

tt

ibute(); ?>" n>><

sp

n> n cl

ss="php">n cl

ss="hljs-met

"><?php n> n cl

ss="hljs-key

o

d">if<

sp

n> (h

s_post_thumbn

il()) { n cl

ss="hljs-met

">?> n><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">div<

sp

n> n cl

ss="hljs-

tt

">cl

ss n>=n cl

ss="hljs-st

ing">"thumb" n>><

sp

n> n cl

ss="php">n cl

ss="hljs-met

"><?php n> n cl

ss="hljs-key

o

d">echo<

sp

n> get_the_post_thumbn

il( n cl

ss="hljs-key

o

d">null n>, n cl

ss="hljs-st

ing">'medium' n>, n cl

ss="hljs-key

o

d">y<

sp

n>(n cl

ss="hljs-st

ing">'

lt'<

sp

n> => the_title_

tt

ibute(n cl

ss="hljs-key

o

d">y<

sp

n>(n cl

ss="hljs-st

ing">'echo' n> => n cl

ss="hljs-key

o

d">f

lse n>)))); n cl

ss="hljs-met

">?> n><

sp

n> n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">div<

sp

n>><

sp

n> n cl

ss="php">n cl

ss="hljs-met

"><?php n> } n cl

ss="hljs-met

">?> n><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">h4<

sp

n>><

sp

n>n cl

ss="php">n cl

ss="hljs-met

"><?php n> the_title(); n cl

ss="hljs-met

">?> n><

sp

n>n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">h4<

sp

n>><

sp

n> n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">

n>><

sp

n> n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">li<

sp

n>><

sp

n> n cl

ss="php">n cl

ss="hljs-met

"><?php n> } n cl

ss="hljs-met

">?> n><

sp

n> n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">ul<

sp

n>><

sp

n> n cl

ss="hljs-t

g">< n cl

ss="hljs-n

me">div<

sp

n> n cl

ss="hljs-

tt

">cl

ss n>=n cl

ss="hljs-st

ing">"cle

fix" n>><

sp

n>n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">div<

sp

n>><

sp

n> n cl

ss="hljs-t

g"></ n cl

ss="hljs-n

me">div<

sp

n>><

sp

n> n cl

ss="php">n cl

ss="hljs-met

"><?php n> }<

sp

n><

p

e> io

ity="high" decoding="

sync" cl

ss="

lignnone size-full

p-im

ge-247" s

c="https:

/

.youfu188.comp-content

uplo

ds

2023/04/1558029365-n

ib

biji.com_2019-05-17_01-49-53-comp

esso

.png"

idth="328" height="235"

lt="相关文章" s

cset="https:

/

.youfu188.comp-content

uplo

ds

2023/04/1558029365-n

ib

biji.com_2019-05-17_01-49-53-comp

esso

.png 328

, https:

/

.youfu188.comp-content

uplo

ds

2023/04/1558029365-n

ib

biji.com_2019-05-17_01-49-53-comp

esso

-300x215.png 300

" sizes="(m

x-

idth: 328px) 100v

, 328px" />

n id="toc-2">方法一高级调用方法<

sp

n><

h3>
如果要调用相关文章代码,只需要在你主题合适的位置,通常是添加到single.php文件里面。

1、显示3个相关文章内容代码

n cl

ss="hljs-met

"><?php n>

c

_

el

ted_posts(); n cl

ss="hljs-met

">?> n><

p

e> 这种相关文章是根据分类来展示的。 2、根据文章T

G来展示相关文章

n cl

ss="hljs-met

"><?php n>

c

_

el

ted_posts(n cl

ss="hljs-key

o

d">y<

sp

n>( n cl

ss="hljs-st

ing">'t

xonomy'<

sp

n> => n cl

ss="hljs-st

ing">'post_t

g'<

sp

n>, n cl

ss="hljs-st

ing">'limit' n> => n cl

ss="hljs-numbe

">6 n> )); n cl

ss="hljs-met

">?> n><

p

e> 数字6自行修改,为展示文章数量。 3、根据分类的热门评论文章展示 e> n cl

ss="hljs-met

"><?php<

sp

n>

c

_

el

ted_posts(n cl

ss="hljs-key

o

d">y<

sp

n>( n cl

ss="hljs-st

ing">'limit' n> => n cl

ss="hljs-numbe

">6 n>, n cl

ss="hljs-st

ing">'o

de

by' n> => n cl

ss="hljs-st

ing">'comment_count' n>, n cl

ss="hljs-st

ing">'o

de

' n> => n cl

ss="hljs-st

ing">'

SC'<

sp

n> )); n cl

ss="hljs-met

">?> n><

p

e> 4、根据特定文章的T

G展示相关文章

n cl

ss="hljs-met

"><?php n>

c

_

el

ted_posts(n cl

ss="hljs-key

o

d">y<

sp

n>( n cl

ss="hljs-st

ing">'limit' n> => n cl

ss="hljs-numbe

">6 n>, n cl

ss="hljs-st

ing">'t

xonomy'<

sp

n> => n cl

ss="hljs-st

ing">'post_t

g'<

sp

n>, n cl

ss="hljs-st

ing">'post_id' n> => n cl

ss="hljs-numbe

">145 n> )); n cl

ss="hljs-met

">?> n><

p

e> 最后,你可能还需要自己修改下css样式让相关文章更好看。

n id="toc-3">代码版方法二 n><

h2>
这种方法相对来说简单点,调用特色图的相关文章,如果你不是每篇文章都有特色图,最好自己添加一张,或者安装<

h

ef="https://blog.n

ib

biji.com/tuto

i

l/

o

dp

ess-ndom-post-thumbn

ils.html">

o

dP

ess随机显示特色图片插件:ndom Post Thumbn

ils > ss="

lignnone size-full

p-im

ge-248" s

c="https:

/

.youfu188.comp-content

uplo

ds

2023/04/1558030605-n

ib

biji.com_2019-05-17_02-15-18-comp

esso

.png"

idth="647" height="241"

lt="

o

dP

ess相关文章 " s

cset="https://

.youfu188.comp-content

uplo

ds

2023/04/1558030605-n

ib

biji.com_2019-05-17_02-15-18-comp

esso

.png 647

, https:

/

.youfu188.comp-content

uplo

ds

2023/04/1558030605-n

ib

biji.com_2019-05-17_02-15-18-comp

esso

-300x112.png 300

" sizes="(m

x-

idth: 647px) 100v

, 647px" />

在你主题文章页面合适的位置添加下面这段代码

<div cl

ss="

el

tedposts">
<h3>相关文章</h3>
<?php
$o

ig_post = $post; glob

l $post;
$t

gs =

p_get_post_t

gs($post->ID); if ($t

gs) {
$t

g_ids = y(); fo

e

ch($t

gs

s $individu

l_t

g) $t

g_ids[] = $individu

l_t

g->te

m_id; $gs=y( 't

g__in' => $t

g_ids, 'post__not_in' => y($post->ID), 'posts_pe

_p

ge'=>4,

/ 相关文章显示数量.
'c

lle

_get_posts'=>1
);

$my_que

y = nep_que

y( $

gs );

hile( $my_que

y->h

ve_posts() ) { $my_que

y->the_post();
?>

<div cl

ss="

el

tedthumb"> <el="exte

n

l" h

ef="<?%20the_pe

m

link()?>"><?php the_post_thumbn

il(y(150,100)); ?><b

/>
<?php the_title(); ?>
</

> <

div>

<? }
}
$post = $o

ig_post;

p_

eset_que

y();
?>
</div>

上面代码中the_post_thumbn

il(

y(150,100)的150,100是控制缩略图大小的。

.

el

tedposts {

idth: 640px; m

gin: 0 0 20px 0; flo

t: left; font-size: 12px;} .

el

tedposts h3 {font-size: 20px; mgin: 0 0 5px 0; } .

el

tedthumb {mgin: 0 1px 0 1px; flo

t: left; }
.

el

tedthumb img {m

gin: 0 0 3px 0; p

dding: 0;} .

el

tedthumb

{colo

:#333; text-decotion: none; displ

y:block; p

dding: 4px;

idth: 150px;}
.

el

tedthumb

:hove

{b

ckg

ound-colo

: #ddd; colo

: #000;}

CSS文件,

idth: 640px是宽度。

使用这种代码的话,最好在

P后台的设置-媒体处把缩略图大小设置为一样的。

n id="toc-4">代码版方法三 n><

h2>
这个代码是展示同分类的其他文章

<?php

/ 默认参数
$

gs =

y(
'posts_pe

_p

ge' => 4, // 要显示的项目数
'post__not_in' =>

y( get_the_ID() ), // 排除当前帖子
'no_found_

o

s' => t

ue, );

/ 检查当前的帖子类别,并将t

x_que

y添加到查询参数中
$c

ts =

p_get_post_te

ms( get_the_ID(), 'c

tego

y' ); $c

ts_ids =

y();
fo

e

ch( $c

ts

s $

pex_

el

ted_c

t ) {
$c

ts_ids[] = $

pex_

el

ted_c

t->te

m_id;
}
if ( ! empty( $c

ts_ids ) ) { $gs['c

tego

y__in'] = $c

ts_ids;
}

// 查询文章
$

pex_que

y = ne

p_que

y( $gs );

/ 输出文章
fo

e

ch( $

pex_que

y->posts

s $post ) : setup_postd

t

( $post ); ?> <div id="

el

ted_posts"> <ul cl

ss="live">
<li><

h

ef="<?php%20the_pe

m

link();%20?>" title="<?php echo esc_

tt

( the_title_

tt

ibute( 'echo=0' ) ); ?>"><?php the_title(); ?></

><

li>
</ul>
</div>
<?php
// 结束循环
endfo

e

ch;

p_

eset_postd

t

(); ?>

使用此代码需要自定义.live的样式,下面是奶爸建站笔记目前使用的样式 e>.live {
ove

flo

: hidden;
m

gin: 0!impo

t

nt;
}
.live

{ text-decotion: none; colo

: #32373c;
}
.live

:hove

{
colo

: #009cee; } .live > li { list-style: none; position:

el

tive; p

dding: 0 0 0 2em;
m

gin: 0 0 .5em 10px;
-

ebkit-tnsition: .12s; tnsition: .12s; } .live > li::befo

e {
position:

bsolute; content: '\2022'; font-f

mily:

i

l; colo

: #111;
top: 0;
left: 0;
text-

lign: cente

;
font-size: 2em;
op

city: .5; line-height: .75; -

ebkit-t

nsition: .5s;
t

nsition: .5s;
}
.live > li:hove

{ colo

: #333;
}
.live > li:hove

::befo

e {
-

ebkit-tnsfo

m: sc

le(2); -ms-tnsfo

m: sc

le(2); tnsfo

m: sc

le(2); op

city: 1;
text-sh

do

: 0 0 4px;
-

ebkit-tnsition: .1s; tnsition: .1s; }<

p

e>  

n id="toc-5">插件版方法 n><

h3>
插件版的方法相对来说适合新手一些,热门的

o

dP

ess相关文章插件有以下几个。 <

h

ef="https:

/

o

dp

ess.o

g/plugins/

el

ted-posts-fo

-

p/" t

get="_bl

nk"

el="noopene

no

efe

e

">

el

ted Posts foo

dP

ess<> <

h

ef="https:

/

o

dp

ess.o

g/plugins/

o

dp

ess-23-

el

ted-posts-plugin

" t

get="_bl

nk"

el="noopene

no

efe

e

">

o

dP

ess

el

ted Posts > <

h

ef="https:

/blog.n

ib

biji.com/files/

o

dp

ess-

pj

m-b

sic.html">

PJ

M-B

sic<>(中文插件,功能强) <

div>

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容