我如何获得页面


Answers:


18

在循环中,您可以执行以下操作:


global $post;

echo $post->post_name;


18

循环外:

<?php
$post_id = 11;
$post = get_post($post_id); 
$slug = $post->post_name;
?> 

1

根据其他答案,子弹存储在post_name属性中。虽然可以直接访问它,但我更喜欢(未充分利用)get_post_field()函数来访问没有适当API的访问帖子属性。

它需要显式提供的帖子,并且不默认为当前帖子。

如果您想在循环之外获取帖子的信息,请使用:

$post_id = 20; //specify post id here
$post = get_post($post_id); 
$slug = $post->post_name;

如果要从循环中获取该帖子的内容,请使用:

global $post;
echo $post->post_name;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.