【问题标题】:How to have the title of the last post in a static page?如何在静态页面中获得最后一篇文章的标题?
【发布时间】:2017-03-09 00:52:38
【问题描述】:

我有一个帖子页面和一个不同的静态主页,以及其他常见页面,例如关于、赞助商。

我试图在所有页面的标题中包含网站名称和最后一篇文章的标题。我尝试了不同的函数(the_title()、get_the_title())。它在帖子页面中运行良好,但对于剩下的所有页面,我都得到了页面的标题(“关于”或“主页”或“赞助商”)。

我知道到目前为止我使用的函数可能只在循环内工作,但想知道是否有一些全局方法可以让最后一篇文章的标题随处可见。

我必须: $posts_page = get_option( 'page_for_posts' ) 来获取帖子页面的 id,并试图从那里找到一种方法来获取最后一篇文章的标题,但到目前为止还没有成功。

有没有简单的方法来做到这一点?

【问题讨论】:

    标签: wordpress post title


    【解决方案1】:

    试试看:

    echo current( wp_get_recent_posts( array('numberposts' => 1, 'post_status' => 'publish') ) )['post_title'];
    

    【讨论】:

      【解决方案2】:

      您是指最新/最近的帖子吗?

      这对你有用吗?

      <?php
         $args = array(
             'posts_per_page'   => 1,
             'orderby'          => 'date',
             'order'            => 'DESC'
         );
         $post = get_posts( $args ); 
         $latest_post_title = $post[0]->post_title;
         echo $latest_post_title;
      ?>
      

      或者这可能更轻一些,不确定,但它们都做几乎相同的事情。

      <?php
         $args = array(
            'numberposts' => 1,
         );
      
         $recent = wp_get_recent_posts($args);
         $last_post_title = $recent[0]['post_title'];
         echo $last_post_title;
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        • 2021-10-17
        • 2019-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多