Wordpress帖子循环显示post_name/slug

use*_*234 8 php wordpress

我正在运行这个PHP代码来遍历wordpress帖子:

$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post)
{
    setup_postdata( $post );
    the_date();
    echo '<br />'; ?>
    <a href="/blog2/"><?php the_title(); ?></a>    
    <?php the_excerpt(); ?>
    <br><hr /><br>
    <?php
}
Run Code Online (Sandbox Code Playgroud)

我希望能够显示每个帖子的post_name或'slug'

我尝试过使用echo $posts->post_name;但它没有显示任何内容

San*_*hra 13

你可以得到标题 $post->post_title

你可以得到名字/ slug $post->post_name


小智 8

您可以通过以下方式发布:

echo $post->post_name;
Run Code Online (Sandbox Code Playgroud)

我已经为你修改了代码:

<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) {
    setup_postdata( $post );
    the_date();
    echo '<br />'; ?>
    <a href="/blog2/"><?php the_title(); ?></a>  
    <?php  echo $post->post_name; ?>
   <?php the_excerpt(); ?>
    <br><hr /><br>
    <?php
}
?>
Run Code Online (Sandbox Code Playgroud)