
如何使用浏览器检查元素工具
get_post ( $post = null, $output = OBJECT, $filter = 'raw' )
get_post函数用于通过其ID检索特定的post对象。它接受一个参数,即post的ID。它返回一个包含所有文章数据的文章对象,如文章标题、内容、日期和作者。
给定一个文章ID或文章对象,检索文章数据。
参见sanitize_post()中的可选$filter值。另外,参数`$post`必须以变量形式给出,因为它是以引用形式传递的。
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { if ( empty( $post ) && isset( $GLOBALS['post'] ) ) { $post = $GLOBALS['post']; } if ( $post instanceof WP_Post ) { $_post = $post; } elseif ( is_object( $post ) ) { if ( empty( $post->filter ) ) { $_post = sanitize_post( $post, 'raw' ); $_post = new WP_Post( $_post ); } elseif ( 'raw' === $post->filter ) { $_post = new WP_Post( $post ); } else { $_post = WP_Post::get_instance( $post->ID ); } } else { $_post = WP_Post::get_instance( $post ); } if ( ! $_post ) { return null; } $_post = $_post->filter( $filter ); if ( ARRAY_A === $output ) { return $_post->to_array(); } elseif ( ARRAY_N === $output ) { return array_values( $_post->to_array() ); } return $_post; }