stick_post

函式
stick_post ( $post_id )
引數
  • (int) $post_id Post ID.
    Required:
定義位置
相關方法
unstick_postsanitize_postthe_postget_poststicky_class
引入
2.7.0
棄用
-

WordPress中的stick_post函式是用來把一個文章粘在部落格首頁的頂部的: 當一個文章被”粘”住時,它就會保持在部落格首頁的頂部,不管它是什麼時候釋出的,直到它被手動”解粘”。

使一個文章置頂。

置頂文章應該顯示在首頁的頂部。

function stick_post( $post_id ) {
	$post_id  = (int) $post_id;
	$stickies = get_option( 'sticky_posts' );
	$updated  = false;

	if ( ! is_array( $stickies ) ) {
		$stickies = array();
	} else {
		$stickies = array_unique( array_map( 'intval', $stickies ) );
	}

	if ( ! in_array( $post_id, $stickies, true ) ) {
		$stickies[] = $post_id;
		$updated    = update_option( 'sticky_posts', array_values( $stickies ) );
	}

	if ( $updated ) {
		/**
		 * Fires once a post has been added to the sticky list.
		 *
		 * @since 4.6.0
		 *
		 * @param int $post_id ID of the post that was stuck.
		 */
		do_action( 'post_stuck', $post_id );
	}
}

常見問題

FAQs
檢視更多 >