cl y-he ">
>
n id="toc">代码版方法一<sp
n><h2>
ong>修改主题文件前记得先备份,有问题再还原。<st
ong>
1、在主题函数文件functions.php里面添加下面的内容
e>
n clss="hljs-comment">/**
*
elted posts
*
*
n clss="hljs-doct
g">@globl
n> object $post
* n cl
ss="hljs-doctg">@p
m<sp
n> y $gs
* n cl
ss="hljs-doctg">@
etun
n>
*
sp
n><h2> st e> ss="hljs-comment">/** ted posts ss="hljs-doct l g">@p sp g">@ n
*
*
*
n>
o
d">function<sp
n>c
_el
ted_posts<sp
n>ms">($
gs =
y())
sp
n>{o
d">globl
/ def
ult gs<sp
n> $gs =p_p
se_
gs($
gs, ss="hljs-key d"> y
ing">'post_id'
o
d">empty<sp
n>($post) ? $post->ID :ing">''
ing">'t
xonomy'<sp
n> =>ing">'c
tegoy'
ing">'limit'
">3
ing">'post_type'
o
d">empty<sp
n>($post) ? $post->post_type :ing">'post'
ing">'o
deby'
ing">'d
te'<sp
n>,ing">'o
de'
ing">'DESC'
/ check t
xonomy<sp
n>o
d">if<sp
n> (!txonomy_exists($
gs[ ss="hljs-st xonomy'
o
d">etu
n<sp
n>; }/ post t
xonomies<sp
n> $txonomies =
p_get_post_tems($
gs[ ss="hljs-st sp
ing">'t
xonomy'<sp
n>],o
d">y<sp
n>(ing">'fields'
ing">'ids'
o
d">if<sp
n> (o
d">empty<sp
n>($txonomies)) { ss="hljs-key d"> n
/ que
y<sp
n> $el
ted_posts = get_posts(o
d">y<sp
n>(ing">'post__not_in'
o
d">y<sp
n>) $gs[ing">'post_id'
ing">'post_type'
ing">'post_type'
ing">'t
x_quey'
o
d">y<sp
n>(o
d">y<sp
n>(ing">'t
xonomy'<sp
n> => $gs[ing">'t
xonomy'<sp
n>],ing">'field'
ing">'te
m_id'<sp
n>,ing">'te
ms'<sp
n> => $txonomies ss="hljs-st _p sp
),
),
ing">'limit'
ing">'o
deby'
ing">'o
deby'
ing">'o
de'
ing">'o
de'
o
d">include<sp
n>( locte_templ
te(ing">'
elted-posts-templ
te.php'<sp
n>,o
d">flse
o
d">flse
p_
eset_postdt
(); }<p
e> 2、新建一个el
ted-posts-templte.php文件,添加以下内容:
"><?php
o
d">if<sp
n> (!o
d">empty<sp
n>($el
ted_posts)) {">?>
sp
n>g">< ss="hljs-n sp
tt
">clss
ing">"
elted-posts"
sp
n>g">< ss="hljs-n sp
tt
">clss
ing">"
idget-title"<sp
n>><sp
n>"><?php
ing">'相关文章'
ing">''
">?>
sp
n>g"></ ss="hljs-n sp sp
g">< ss="hljs-n sp
tt
">clss
ing">"
elted-posts-list"
sp
n>"><?php
o
d">foe
ch<sp
n> ($el
ted_postso
d">s
t
($post);">?>
sp
n>g">< ss="hljs-n sp sp
g">< ss="hljs-n
tt
">clss
ing">"title"
tt
">hef
ing">"<?php%20the_pe
mlink();%20?>"
tt
">title<sp
n>=ing">"<?php the_title_
ttibute(); ?>"
sp
n>"><?php
o
d">if<sp
n> (hs_post_thumbn
il()) {">?>
sp
n>g">< ss="hljs-n sp
tt
">clss
ing">"thumb"
sp
n>"><?php
o
d">echo<sp
n> get_the_post_thumbnil( ss="hljs-key d">null
ing">'medium'
o
d">y<sp
n>(ing">'
lt'<sp
n> => the_title_tt
ibute(o
d">y<sp
n>(ing">'echo'
o
d">flse
">?>
sp
n>g"></ ss="hljs-n sp sp
"><?php
">?>
sp
n>g">< ss="hljs-n sp sp
"><?php
">?>
sp
n>g"></ ss="hljs-n sp sp
g"></ ss="hljs-n
sp
n>g"></ ss="hljs-n sp sp
"><?php
">?>
sp
n>g"></ ss="hljs-n sp sp
g">< ss="hljs-n sp
tt
">clss
ing">"cle
fix"
sp
n>g"></ ss="hljs-n sp sp
g"></ ss="hljs-n sp sp
"><?php
sp
n><p
e> io ity="high" decoding="sync" cl
ss="lignnone size-full
p-imge-247" s
c="https:/
.youfu188.comp-contentuplo
ds2023/04/1558029365-n
ibbiji.com_2019-05-17_01-49-53-comp
esso.png"
idth="328" height="235"lt="相关文章" s
cset="https:/
.youfu188.comp-contentuplo
ds2023/04/1558029365-n
ibbiji.com_2019-05-17_01-49-53-comp
esso.png 328
, https:/
.youfu188.comp-contentuplo
ds2023/04/1558029365-n
ibbiji.com_2019-05-17_01-49-53-comp
esso-300x215.png 300
" sizes="(mx-
idth: 328px) 100v, 328px" />
n id="toc-2">方法一高级调用方法<sp
n><h3>
如果要调用相关文章代码,只需要在你主题合适的位置,通常是添加到single.php文件里面。
1、显示3个相关文章内容代码
n cl
ss="hljs-met"><?php
n>
sp
n><h3>
如果要调用相关文章代码,只需要在你主题合适的位置,通常是添加到single.php文件里面。
1、显示3个相关文章内容代码
"><?php
c
_el
ted_posts();">?>
p
e> 这种相关文章是根据分类来展示的。 2、根据文章TG来展示相关文章
"><?php
c
_el
ted_posts(o
d">y<sp
n>(ing">'t
xonomy'<sp
n> =>ing">'post_t
g'<sp
n>,ing">'limit'
">6
">?>
p
e> 数字6自行修改,为展示文章数量。 3、根据分类的热门评论文章展示e> ss="hljs-met sp c el
o
d">y<sp
n>(ing">'limit'
">6
ing">'o
deby'
ing">'comment_count'
ing">'o
de'
ing">'
SC'<sp
n> ));">?>
p
e> 4、根据特定文章的TG展示相关文章
"><?php
c
_el
ted_posts(o
d">y<sp
n>(ing">'limit'
">6
ing">'t
xonomy'<sp
n> =>ing">'post_t
g'<sp
n>,ing">'post_id'
">145
">?>
p
e> 最后,你可能还需要自己修改下css样式让相关文章更好看。n id="toc-3">代码版方法二
n><
h2>
这种方法相对来说简单点,调用特色图的相关文章,如果你不是每篇文章都有特色图,最好自己添加一张,或者安装<
ef="https://blog.n
ibbiji.com/tuto
il/
odp
ess-ndom-post-thumbnils.html">
odP
ess随机显示特色图片插件:ndom Post Thumbnils
> ss="lignnone size-full
p-imge-248" s
c="https:/
.youfu188.comp-contentuplo
ds2023/04/1558030605-n
ibbiji.com_2019-05-17_02-15-18-comp
esso.png"
idth="647" height="241"lt="
odP
ess相关文章 " scset="https://
.youfu188.comp-contentuplo
ds2023/04/1558030605-n
ibbiji.com_2019-05-17_02-15-18-comp
esso.png 647
, https:/
.youfu188.comp-contentuplo
ds2023/04/1558030605-n
ibbiji.com_2019-05-17_02-15-18-comp
esso-300x112.png 300
" sizes="(mx-
idth: 647px) 100v, 647px" />
在你主题文章页面合适的位置添加下面这段代码
<div cl
ss="
eltedposts">
<h3>相关文章</h3>
<?php
$o
l $post;
$t
p_get_post_t
gs($post->ID); if ($tgs) {
$t
e
ch($tgs
s $individul_t
g) $tg_ids[] = $individu
l_tg->te
m_id; $gs=y( 'tg__in' => $t
g_ids, 'post__not_in' => y($post->ID), 'posts_pe_p
ge'=>4,/ 相关文章显示数量.
'c
_get_posts'=>1
);
$my_que
y = nep_quey( $
gs );
hile( $my_quey->h
ve_posts() ) { $my_quey->the_post();
?>
<div cl
ss="el
tedthumb"> <el="exten
l" hef="<?%20the_pe
mlink()?>"><?php the_post_thumbn
il(y(150,100)); ?><b />
<?php the_title(); ?>
</
div>
<? }
}
$post = $o
p_
eset_quey();
?>
</div>
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; flot: left; }
.
tedthumb img {m
gin: 0 0 3px 0; p
dding: 0;} .el
tedthumb{colo
:#333; text-decotion: none; disply:block; p
dding: 4px;idth: 150px;}
.
tedthumb
:hove{b
ckgound-colo
: #ddd; colo: #000;}
CSS文件,idth: 640px是宽度。
使用这种代码的话,最好在
P后台的设置-媒体处把缩略图大小设置为一样的。n id="toc-4">代码版方法三
n><
h2>
这个代码是展示同分类的其他文章
<?php
/ 默认参数
$
gs =
y(
'posts_pe
ge' => 4, // 要显示的项目数
'post__not_in' =>
y( get_the_ID() ), // 排除当前帖子
'no_found_
s' => t
ue, );/ 检查当前的帖子类别,并将t
x_quey添加到查询参数中
$c
p_get_post_te
ms( get_the_ID(), 'ctego
y' ); $cts_ids =
y();
fo
ch( $c
tss $
pex_el
ted_ct ) {
$c
pex_
elted_c
t->tem_id;
}
if ( ! empty( $c
tego
y__in'] = $cts_ids;
}
// 查询文章
$
y = ne
p_que
y( $gs );/ 输出文章
fo
ch( $
pex_quey->posts
s $post ) : setup_postdt
( $post ); ?> <div id="el
ted_posts"> <ul clss="live">
<li><
ef="<?php%20the_pe
mlink();%20?>" title="<?php echo esc_
tt( the_title_
ttibute( 'echo=0' ) ); ?>"><?php the_title(); ?></
><li>
</ul>
</div>
<?php
// 结束循环
endfo
ch;
p_eset_postd
t(); ?>
使用此代码需要自定义.live的样式,下面是奶爸建站笔记目前使用的样式e>.live {
ove
: hidden;
m
gin: 0!impo
tnt;
}
.live
: #32373c;
}
.live
{
colo
el
tive; pdding: 0 0 0 2em;
m
gin: 0 0 .5em 10px;
-
e {
position:
mily:
i
l; colo: #111;
top: 0;
left: 0;
text-
;
font-size: 2em;
op
ebkit-t
nsition: .5s;
t
nsition: .5s;
}
.live > li:hove
: #333;
}
.live > li:hove
e {
-
m: sc
le(2); -ms-tnsfom: sc
le(2); tnsfom: sc
le(2); opcity: 1;
text-sh
: 0 0 4px;
-
p
e>n id="toc-5">插件版方法
n><
h3>
插件版的方法相对来说适合新手一些,热门的
dP
ess相关文章插件有以下几个。 <h
ef="https:/
odp
ess.og/plugins/
elted-posts-fo
-p/" t
get="_bl
nk"el="noopene
noefe
e
">el
ted Posts foodP
ess<> <h
ef="https:/
odp
ess.og/plugins/
odp
ess-23-el
ted-posts-plugin" t
get="_bl
nk"el="noopene
noefe
e
">o
dPess
elted Posts
> <h
ef="https:/blog.n
ibbiji.com/files/
odp
ess-pj
m-bsic.html">
PJM-B
sic<>(中文插件,功能强) <div>
暂无评论内容