WordPress建站中显示最近几篇置顶文章的方法

前端之家收集整理的这篇文章主要介绍了WordPress建站中显示最近几篇置顶文章的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

中有个不错的功能叫置顶文章。置顶文章通常是一些有特色的文章,是站长推荐给用户阅读的。这篇文章介绍一下,如何在自己制作的模板中显示置顶文章。

打开主题文件夹里的 functions.php 文件,添加如下代码,之后使用短代码形式进行调用(WordPress 4.3版本,亲测,可行):

Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d642e599872948755-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-2">2
Crayon-num" data-line="Crayon-5c891d642e599872948755-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-4">4
Crayon-num" data-line="Crayon-5c891d642e599872948755-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-6">6
Crayon-num" data-line="Crayon-5c891d642e599872948755-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-8">8
Crayon-num" data-line="Crayon-5c891d642e599872948755-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-10">10
Crayon-num" data-line="Crayon-5c891d642e599872948755-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-12">12
Crayon-num" data-line="Crayon-5c891d642e599872948755-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-14">14
Crayon-num" data-line="Crayon-5c891d642e599872948755-15">15
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-16">16
Crayon-num" data-line="Crayon-5c891d642e599872948755-17">17
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-18">18
Crayon-num" data-line="Crayon-5c891d642e599872948755-19">19
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-20">20
Crayon-num" data-line="Crayon-5c891d642e599872948755-21">21
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-22">22
Crayon-num" data-line="Crayon-5c891d642e599872948755-23">23
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-24">24
Crayon-num" data-line="Crayon-5c891d642e599872948755-25">25
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-26">26
Crayon-num" data-line="Crayon-5c891d642e599872948755-27">27
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-28">28
Crayon-num" data-line="Crayon-5c891d642e599872948755-29">29
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-30">30
Crayon-num" data-line="Crayon-5c891d642e599872948755-31">31
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d642e599872948755-32">32
Crayon-num" data-line="Crayon-5c891d642e599872948755-33">33
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d642e599872948755-1">Crayon-t">functionCrayon-h"> Crayon-e">wpb_latest_stickyCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">{Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-2">
Crayon-line" id="Crayon-5c891d642e599872948755-3">Crayon-c">/* Get all sticky posts */
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-4">Crayon-sy">$Crayon-v">stickyCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_optionCrayon-sy">(Crayon-h"> Crayon-s">'sticky_posts'Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d642e599872948755-5">
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-6">Crayon-c">/* Sort the stickies with the newest ones at the top */
Crayon-line" id="Crayon-5c891d642e599872948755-7">Crayon-e">rsortCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-i">stickyCrayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-8">
Crayon-line" id="Crayon-5c891d642e599872948755-9">Crayon-c">/* Get the 5 newest stickies (change 5 for a different number) */
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-10">Crayon-sy">$Crayon-v">stickyCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">array_sliceCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">stickyCrayon-sy">,Crayon-h"> Crayon-cn">0Crayon-sy">,Crayon-h"> Crayon-cn">5Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d642e599872948755-11">
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-12">Crayon-c">/* Query sticky posts */
Crayon-line" id="Crayon-5c891d642e599872948755-13">Crayon-sy">$Crayon-v">the_queryCrayon-h"> Crayon-o">=Crayon-h"> Crayon-r">newCrayon-h"> Crayon-e">WP_QueryCrayon-sy">(Crayon-h"> Crayon-t">arrayCrayon-sy">(Crayon-h"> Crayon-s">'post__in'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-sy">$Crayon-v">stickyCrayon-sy">,Crayon-h"> Crayon-s">'ignore_sticky_posts'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-cn">1Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-14">Crayon-c">// The Loop
Crayon-line" id="Crayon-5c891d642e599872948755-15">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">the_queryCrayon-e">have_postsCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-16">Crayon-h"> Crayon-sy">$Crayon-st">returnCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-s">'
    'Crayon-sy">;
Crayon-line" id="Crayon-5c891d642e599872948755-17">Crayon-h"> Crayon-st">whileCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">the_queryCrayon-e">have_postsCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-18">Crayon-h"> Crayon-sy">$Crayon-v">the_queryCrayon-e">the_postCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891d642e599872948755-19">Crayon-h"> Crayon-sy">$Crayon-st">returnCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-s">'

  • '
    Crayon
    -h"> Crayon-sy">.Crayon-h"> Crayon-e">get_the_excerptCrayon-sy">(Crayon-sy">)Crayon-sy">.Crayon-h"> Crayon-s">'
  • '
    Crayon-sy">;
    Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-20">Crayon-h">
    Crayon-line" id="Crayon-5c891d642e599872948755-21">Crayon-h"> Crayon-sy">}
    Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-22">Crayon-h"> Crayon-sy">$Crayon-st">returnCrayon-h"> Crayon-sy">.Crayon-o">=Crayon-h"> Crayon-s">''Crayon-sy">;
    Crayon-line" id="Crayon-5c891d642e599872948755-23">Crayon-h">
    Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-24">Crayon-sy">}Crayon-h"> Crayon-st">elseCrayon-h"> Crayon-sy">{
    Crayon-line" id="Crayon-5c891d642e599872948755-25">Crayon-h"> Crayon-c">// no posts found
    Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-26">Crayon-sy">}
    Crayon-line" id="Crayon-5c891d642e599872948755-27">Crayon-c">/* Restore original Post Data */
    Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-28">Crayon-e">wp_reset_postdataCrayon-sy">(Crayon-sy">)Crayon-sy">;
    Crayon-line" id="Crayon-5c891d642e599872948755-29">
    Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-30">Crayon-st">returnCrayon-h"> Crayon-sy">$Crayon-st">returnCrayon-sy">;Crayon-h">
    Crayon-line" id="Crayon-5c891d642e599872948755-31">
    Crayon-line Crayon-striped-line" id="Crayon-5c891d642e599872948755-32">Crayon-sy">}Crayon-h">
    Crayon-line" id="Crayon-5c891d642e599872948755-33">Crayon-e">add_shortcodeCrayon-sy">(Crayon-s">'latest_stickies'Crayon-sy">,Crayon-h"> Crayon-s">'wpb_latest_sticky'Crayon-sy">)Crayon-sy">;

    在需要调用的地方添加代码文章或者页面添加

    Crayon-5c891d642e5a1331547522" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
    Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
    Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
    Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
    Crayon-button-icon">
    Crayon-button Crayon-plain-button" title="Toggle Plain Code">
    Crayon-button-icon">
    Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
    Crayon-button-icon">
    Crayon-button Crayon-expand-button" title="Expand Code">
    Crayon-button-icon">
    Crayon-button Crayon-copy-button" title="Copy">
    Crayon-button-icon">
    Crayon-button Crayon-popup-button" title="Open Code In New Window">
    Crayon-button-icon">
    Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
    Crayon-plain-wrap">
    Crayon-main" style="">Crayon-table">Crayon-row">
    Crayon-nums " data-settings="show">
    Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
    Crayon-num" data-line="Crayon-5c891d642e5a1331547522-1">1
    Crayon-code">
    Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
    Crayon-line" id="Crayon-5c891d642e5a1331547522-1">Crayon-sy">[Crayon-v">latest_stickiesCrayon-sy">]

    或者在需要调用PHP文件中,添加如下代码

    Crayon-5c891d642e5b2243247844" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
    Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">
    Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-mixed-highlight" title="Contains Mixed Languages">
    Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
    Crayon-button-icon">
    Crayon-button Crayon-plain-button" title="Toggle Plain Code">
    Crayon-button-icon">
    Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
    Crayon-button-icon">
    Crayon-button Crayon-expand-button" title="Expand Code">
    Crayon-button-icon">
    Crayon-button Crayon-copy-button" title="Copy">
    Crayon-button-icon">
    Crayon-button Crayon-popup-button" title="Open Code In New Window">
    Crayon-button-icon">
    Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
    Crayon-plain-wrap">
    Crayon-main" style="">Crayon-table">Crayon-row">
    Crayon-nums " data-settings="show">
    Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
    Crayon-num" data-line="Crayon-5c891d642e5b2243247844-1">1
    Crayon-code">
    Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
    Crayon-line" id="Crayon-5c891d642e5b2243247844-1">Crayon-ta">PHPCrayon-h"> Crayon-k ">echoCrayon-h"> Crayon-e">do_shortcodeCrayon-sy">(Crayon-s">'[latest_stickies]'Crayon-sy">)Crayon-sy">;

    如果想在小工具里使用短代码,请在 functions.PHP 文件添加下面的代码

    Crayon-5c891d642e5b3834957569" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
    Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
    Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
    Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
    Crayon-button-icon">
    Crayon-button Crayon-plain-button" title="Toggle Plain Code">
    Crayon-button-icon">
    Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
    Crayon-button-icon">
    Crayon-button Crayon-expand-button" title="Expand Code">
    Crayon-button-icon">
    Crayon-button Crayon-copy-button" title="Copy">
    Crayon-button-icon">
    Crayon-button Crayon-popup-button" title="Open Code In New Window">
    Crayon-button-icon">
    Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
    Crayon-plain-wrap">
    Crayon-main" style="">Crayon-table">Crayon-row">
    Crayon-nums " data-settings="show">
    Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
    Crayon-num" data-line="Crayon-5c891d642e5b3834957569-1">1
    Crayon-code">
    Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
    Crayon-line" id="Crayon-5c891d642e5b3834957569-1">Crayon-e">add_filterCrayon-sy">(Crayon-s">'widget_text'Crayon-sy">,Crayon-h"> Crayon-s">'do_shortcode'Crayon-sy">)Crayon-sy">;

    原文链接:https://www.f2er.com/wordpress/238531.html

    猜你在找的wordpress相关文章