如何在 WordPress 中顯示最新置頂帖子

wordpress 有一個非常酷的功能,稱為粘帖。將置頂帖子視為博客的特色帖子。當(dāng)您將帖子標(biāo)記為置頂時,它會顯示在您的新帖子上方,但前提是您的主題允許。在本教程中,我們將向您展示如何在 WordPress 中顯示最新的置頂帖子。

如何在 WordPress 中顯示最新置頂帖子

注意:這是一個中級教程,需要基本的 html / css 知識 + WordPress 主題知識。

視頻教程

https://www.youtube.com/embed/qI3S-tNKWng?version=3&rel=0&fs=1&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent訂閱 WPBeginner

https://www.youtube.com/subscribe_embed?usegapi=1&channel=wpbeginner&layout=default&count=default&origin=https%3A%2F%2Fwpbeginner.com&gsrc=3p&ic=1&jsh=m%3B%2F_%2Fscs%2Fapps-Static%2F_%2Fjs%2Fk%3Doz.gapi.en.vQiXRrxCe40.O%2Fam%3DAQ%2Fd%3D1%2Frs%3DAGLTcCMBxIGVyXSdvvcs43a64yHt_P7dfg%2Fm%3D__features__#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh&id=I0_1448178294715&parent=https%3A%2F%2Fwpbeginner.com

如果您不喜歡該視頻或需要更多說明,請繼續(xù)閱讀。

您需要做的第一件事是復(fù)制此代碼片段并將其粘貼到主題的functions.php文件或特定于站點的插件中。

functionwpb_latest_sticky() { /* Get all sticky posts */$sticky= get_option( 'sticky_posts');/* Sort the stickies with the newest ones at the top */rsort( $sticky);/* Get the 5 newest stickies (change 5 for a different number) */$sticky= array_slice( $sticky, 0, 5 );/* Query sticky posts */$the_query= newWP_Query( array( 'post__in'=> $sticky, 'ignore_sticky_posts'=> 1 ) );// The Loopif( $the_query->have_posts() ) {    $return.= '<ul>';    while( $the_query->have_posts() ) {        $the_query->the_post();        $return.= '<li><a href="'.get_permalink(). '" title="'. get_the_title() . '">'. get_the_title() . '</a><br />'. get_the_excerpt(). '</li>';            }    $return.= '</ul>';    } else{    // no posts found}/* Restore original Post Data */wp_reset_postdata();return$return; } add_shortcode('latest_stickies', 'wpb_latest_sticky');

在 WordPress 中一鍵使用

上面的代碼查詢 WordPress 數(shù)據(jù)庫以檢索 5 個最新的置頂帖子。然后,它以列表格式顯示每個置頂帖子的標(biāo)題和鏈接。我們已將所有內(nèi)容包裝在一個函數(shù)中并創(chuàng)建了一個短代碼。

現(xiàn)在,要顯示最新的置頂帖子,您可以在任何 WordPress 帖子、頁面甚至文本小部件中使用短代碼 [latest_stickies]。

如果您想在文本小部件中使用短代碼,那么您需要在主題的functions.php或特定于站點的插件中添加這行額外的代碼。

add_filter('widget_text', 'do_shortcode');

在 WordPress 中一鍵使用

此代碼片段和函數(shù)可以很好地用于特色滑塊或您希望在網(wǎng)站上顯示的任何其他高級功能。此代碼段主要面向具有自定義主頁或雜志風(fēng)格外觀的 WordPress 網(wǎng)站。

就這樣,我們希望本文能幫助您在 WordPress 博客上顯示最新的置頂帖子。您可能還想查看我們的教程,了解如何為 WordPress 中的置頂帖子添加過期日期。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊5 分享