【问题标题】:Wordpress posts loop show post_name / slugWordpress 帖子循环显示 post_name / slug
【发布时间】:2016-01-21 01:46:21
【问题描述】:

我正在运行这个 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
}

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

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以通过$post-&gt;post_title获取标题

    您可以通过$post-&gt;post_name获取名称/slug

    【讨论】:

      【解决方案2】:

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

      echo $post->post_name;
      

      我已经为你修改了代码:

      <?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
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2023-03-10
        • 1970-01-01
        • 2019-08-24
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        • 2011-02-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多