stick_post

函数
stick_post ( $post_id )
参数
  • (int) $post_id Post ID.
    Required:
定义位置
相关方法
unstick_postsanitize_postthe_postget_poststicky_class
引入
2.7.0
弃用
-

WordPress中的stick_post函数是用来把一个文章粘在博客首页的顶部的: 当一个文章被”粘”住时,它就会保持在博客首页的顶部,不管它是什么时候发布的,直到它被手动”解粘”。

使一个文章置顶。

置顶文章应该显示在首页的顶部。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 );
}
}
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 ); } }
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
查看更多 >