判斷、添加和刪除WordPress置頂文章的相關PHP函數小結_php執行個體

來源:互聯網
上載者:User

判斷置頂文章
is_sticky() 函數用來判斷一篇文章是否為置頂文章。

用法

is_sticky( $post_id );

參數

$post_id

(整數)(可選)要判斷的文章 ID,預設是迴圈中的當前文章。

預設值:0(迴圈中的當前文章)

傳回值

(布爾)文章是否為置頂文章。

例子

if( is_sticky() ) echo //'當前文章是置頂文章';if( is_sticky( 68 ) ) echo //'ID 為 68 的文章是置頂文章';

其它

此函數位於:wp-includes/post.php

添加和移除置頂文章的函數
WordPress 預設支援文章置頂的功能,你可以把重要或精彩的文章在後台置頂,讓使用者優先看到。

在開發中,可能需要通過代碼來添加和移除置頂文章。WordPress 置頂文章的原理就是把置頂文章的 ID 存到 options 表裡,通過修改 sticky_posts 欄位即可控制置頂文章。

但是,WordPress 提供了兩個函數,可以更加輕鬆的添加和移除置頂文章,直接調用函數即可修改 sticky_posts 欄位。

stick_post()

stick_post() 函數用來把一篇文章置頂,例子:

stick_post( 68 );//置頂 ID 為 68 的文章stick_post( get_the_ID() );//置頂迴圈中的當前文章

unstick_post()

unstick_post() 和 stick_post() 函數相反,用來把一篇置頂文章取消置頂:

unstick_post( 425 );//取消置頂 ID 為 425 的文章unstick_post( get_the_ID() );//取消置頂迴圈中的當前文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.