繁体   English   中英

如何使用WordPress获取最新帖子?

[英]How to use WordPress get recent posts?

我想知道如何使用Wordpress在帖子区域内获取最新帖子?

我从WordPress网站获得了以下代码以获取最新帖子:

 wp_get_recent_posts( $args, $output);

如果我在帖子页面正文(我写帖子的地方)内回显此功能,我只会得到显示为文本的确切php代码?

    <h2>Recent Posts</h2>
<ul>
<?php
    $args = array( 'numberposts' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
    }
?>
</ul>

用于显示最近5个帖子的其他代码也以文本形式呈现到帖子页面,我不知道为什么?

如何正确使用它?

我不确定您对“发布区域”的含义。 我假设使用“字符串文本输出”是指列表中未格式化的文本链接。

如果您需要更好地控制如何格式化输出(例如使其更像常规的帖子列表),请为此使用常规的WP Query。 您可以使用以下参数获取5个最新的博客条目:

$recent_args = array(
    "posts_per_page" => 5,
    "orderby"        => "date",
    "order"          => "DESC"
);      

$recent_posts = new WP_Query( $recent_args );

要遍历它们,只需使用常规的WordPress主循环结构:

if ( $recent_posts -> have_posts() ) :
    while ( $recent_posts -> have_posts() ) :

    $recent_posts -> the_post();

    // ... Use regular 'the_title()', 'the_permalink()', etc. loop functions here.

    endwhile;
endif;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM