简体   繁体   English

在Wordpress上显示x最近的帖子,包括标题,日期和摘录

[英]Show x recent posts on Wordpress with title, date and excerpt

I'm looking to find a way to show the last 5 posts on the homepage of my Wordpress website, so i assume i will need to utilise shortcodes to do this, however i cannot find any pre-existing code to add to functions.php that will show the title, date and post excerpt. 我正在寻找一种方法来显示我的Wordpress网站主页上的最后5个帖子,所以我假设我需要利用短代码来做到这一点,但是我找不到任何预先存在的代码来添加到functions.php这将显示标题,日期和帖子摘录。

Could someone possibly help me with this? 有人可能会帮我这个吗?

There are a variety of ways to do this, but the basic idea is similar to the code you'll see in your theme's INDEX.PHP page. 有很多方法可以做到这一点,但基本的想法类似于你在主题的INDEX.PHP页面中看到的代码。 You do a query, loop through to show the posts, then reset the query at the end so what you did doesn't interfere with your main page. 您执行查询,循环显示帖子,然后在结束时重置查询,这样您所做的不会干扰您的主页。

function Last5posts()
{
    $args = array( "showposts" => 5 );                  
    query_posts($args);

    $content = "";

    if( have_posts() ) : 

        while( have_posts() ) :

            the_post();
            $link = get_permalink();
            $title = get_the_title();
            $date = get_the_date();                              

            $content .= "<div style='padding: 5px; border: 1px solid red'>";
            $content .= "<h3><a href='$link' target='_top'>$title / $date</a></h3>\n";
            $content .= "<p class='excerpt'>" . get_the_excerpt() . "</p>";
            $content .= "</div>";

        endwhile;

        wp_reset_query();

    endif;

    // Leave one line commented out depending on usage
    echo $content;   // For use as widget
    //return $content; // for use as shortcode
}

To register this as a widget, enable the "echo" at the end, then add this line to the bottom of your file: 要将其注册为窗口小部件,请在末尾启用“echo”,然后将此行添加到文件的底部:

register_sidebar_widget(__('Last 5 Posts'), 'Last5posts');

You may want to add some extra code to place the output into a widget wrapper DIV like your other sidebar widgets. 您可能希望添加一些额外的代码,以将输出放置到窗口小部件包装器DIV中,就像其他侧边栏小部件一样。 (Or, not if you use it somewhere besides a traditional sidebar.) (或者,如果你在传统的侧边栏​​之外的某处使用它,那就不行了。)

You could also register this as a shortcode handler using the line below. 您还可以使用下面的行将其注册为短代码处理程序。 Comment out the "echo" at the end and uncomment the "return". 最后注释出“回声”并取消注释“回归”。

add_shortcode('Last5Posts', 'Last5posts' );         

You'll want to make sure that you don't use the shortcode in blog posts or you might end up calling this recursively. 您需要确保不在博客文章中使用短代码,否则最终可能会以递归方式调用此代码。 Probably a bad thing. 可能是一件坏事。

You'll probably also want to add a theme-specific prefix to the function name to avoid namespace collisions. 您可能还希望在函数名称中添加特定于主题的前缀,以避免命名空间冲突。

<?php 

    $args = array('numberposts' => 5);

    $recent_posts = wp_get_recent_posts($args);

    foreach( $recent_posts as $recent ){
      echo $recent["post_title"].' '.$recent['post_date'].' '.$recent['post_excerpt'].'<br />';
    }
?>

The Wordpress Codex isn't that bad. Wordpress Codex并没有那么糟糕。

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

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