wordpress主题制作中可能需要单独调用指定文章的标题、链接或内容等,可以通过get_post()函数指定文章ID来获取文章标题、文章内容、文章链接、时间等文章相关信息。
1 | <?php get_post( $post_id, $output );?> |
参数说明:
$post_id:文章ID。必须传递一个含有整数的变量(如$id),如果直接添加数字会报错,这个需要注意。默认值为空。
$output:需要返回的参数,可选参数。object对象 – (默认) 返回对象模式,ARRAY_A – Returns an associative array of field names to values(返回字段名称关联数组);ARRAY_N – returns a numeric array of field values(返回数字数组)
格式一:
1 2 3 4 5 | <?php //获取文章ID编号为10的标题名称,返回对象数据格式 $post_id = 1; // 文章ID echo get_post( $post_id )->post_content; // 输出文章的内容 ?> |
格式二:
1 2 3 4 5 6 | <?php //获取文章ID编号为10的标题名称,返回字段关联数组数据格式 $post_id = 10; $post_title = get_post($post_id, ARRAY_A); //这样返回的值变成了数组形式 $title = $post_title['post_title']; ?> |