如何在静态页面中显示最后3个帖子(最近的帖子)?
5 个回答
- 投票数
-
- 2017-02-01
我通常使用这种方法:
错误的方法
<?php query_posts( array( 'category_name' => 'news', 'posts_per_page' => 3, )); ?> <?php if( have_posts() ): while ( have_posts() ) : the_post(); ?> <?php the_excerpt(); ?> <?php endwhile; ?> <?php else : ?> <p><?php __('No News'); ?></p> <?php endif; ?>
在@swissspidy的帮助下正确的方法是这样的:
<?php // the query $the_query = new WP_Query( array( 'category_name' => 'news', 'posts_per_page' => 3, )); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php the_title(); ?> <?php the_excerpt(); ?> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> <p><?php __('No News'); ?></p> <?php endif; ?>
有关更多信息,请参见 @codex .
I usually use this approach:
wrong approach
<?php query_posts( array( 'category_name' => 'news', 'posts_per_page' => 3, )); ?> <?php if( have_posts() ): while ( have_posts() ) : the_post(); ?> <?php the_excerpt(); ?> <?php endwhile; ?> <?php else : ?> <p><?php __('No News'); ?></p> <?php endif; ?>
With the help of @swissspidy the correct way is this one:
<?php // the query $the_query = new WP_Query( array( 'category_name' => 'news', 'posts_per_page' => 3, )); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php the_title(); ?> <?php the_excerpt(); ?> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> <p><?php __('No News'); ?></p> <?php endif; ?>
See @codex for more info.
-
我喜欢参考http://wordpress.stackexchange.com/a/1755/12404,以说明为什么使用`query_posts()`几乎总是一个坏主意.I like to reference to http://wordpress.stackexchange.com/a/1755/12404 in order to show why using `query_posts()` is almost always a bad idea.
- 2
- 2017-02-01
- swissspidy
-
- 2011-12-14
这取决于您要做什么.如果要创建"帖子页面"(换句话说,创建一个新页面模板文件),则可以在该页面上创建辅助循环.
抄本有一个示例,这是另一个非常精简的示例.>
<?php /* Template Name: Page of Posts */ get_header(); ?> <?php while( have_posts() ): the_post(); /* start main loop */ ?> <h1><?php the_title(); ?></h1> <?php /* Start Secondary Loop */ $other_posts = new WP_Query( /*maybe some args here? */ ); while( $others_posts->have_posts() ): $other_posts->the_post(); ?> You can do anything you would in the main loop here and it will apply to the secondary loop's posts <?php endwhile; /* end secondary loop */ wp_reset_postdata(); /* Restore the original queried page to the $post variable */ ?> <?php endwhile; /* End the main loop */ ?>
如果您正在寻找可放入任何页面的内容,最好的解决方案是短代码一个>.您将需要创建一个短代码,以获取多个帖子并将其返回到列表中(或您想要的任何内容).一个例子:
<?php add_action( 'init', 'wpse36453_register_shortcode' ); /** * Registers the shortcode with add_shortcode so WP knows about it. */ function wpse36453_register_shortcode() { add_shortcode( 'wpse36453_posts', 'wpse36453_shortcode_cb' ); } /** * The call back function for the shortcode. Returns our list of posts. */ function wpse36453_shortcode_cb( $args ) { // get the posts $posts = get_posts( array( 'numberposts' => 3 ) ); // No posts? run away! if( empty( $posts ) ) return ''; /** * Loop through each post, getting what we need and appending it to * the variable we'll send out */ $out = '<ul>'; foreach( $posts as $post ) { $out .= sprintf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink( $post ), esc_attr( $post->post_title ), esc_html( $post->post_title ) ); } $out .= '</ul>'; return $out; }
It kind of depends on what you're going for. If you want to do a "page of posts" -- other words, create a new page template file -- you can create a secondary loop on that page.
The codex has an example of this and here's another, very stripped down example.
<?php /* Template Name: Page of Posts */ get_header(); ?> <?php while( have_posts() ): the_post(); /* start main loop */ ?> <h1><?php the_title(); ?></h1> <?php /* Start Secondary Loop */ $other_posts = new WP_Query( /*maybe some args here? */ ); while( $others_posts->have_posts() ): $other_posts->the_post(); ?> You can do anything you would in the main loop here and it will apply to the secondary loop's posts <?php endwhile; /* end secondary loop */ wp_reset_postdata(); /* Restore the original queried page to the $post variable */ ?> <?php endwhile; /* End the main loop */ ?>
If you're looking for something that you can drop into any page, the best solution would be a shortcode. You would need to create a shortcode that fetches several posts and returns them in a list (or whatever you want). An example:
<?php add_action( 'init', 'wpse36453_register_shortcode' ); /** * Registers the shortcode with add_shortcode so WP knows about it. */ function wpse36453_register_shortcode() { add_shortcode( 'wpse36453_posts', 'wpse36453_shortcode_cb' ); } /** * The call back function for the shortcode. Returns our list of posts. */ function wpse36453_shortcode_cb( $args ) { // get the posts $posts = get_posts( array( 'numberposts' => 3 ) ); // No posts? run away! if( empty( $posts ) ) return ''; /** * Loop through each post, getting what we need and appending it to * the variable we'll send out */ $out = '<ul>'; foreach( $posts as $post ) { $out .= sprintf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink( $post ), esc_attr( $post->post_title ), esc_html( $post->post_title ) ); } $out .= '</ul>'; return $out; }
-
我可以将其放入header.php还是应该将其放置在其他地方?Can I put this into header.php or should I put it somewhere else?
- 0
- 2011-12-15
- user385917
-
第一个示例可以放在主题的任何位置.第二个简短代码示例应放在"functions.php"中The first example can go anywhere in your theme. The second, shortcode, example should go in `functions.php`
- 0
- 2011-12-15
- chrisguitarguy
-
第一段代码没有循环播放3个帖子the first block of code doesn't have a to loop over 3 posts
- 0
- 2014-06-10
- Murhaf Sousli
-
- 2012-10-26
在wordpress Codex上有关于这种精确情况的指南.在此处查看: 我将代码粘贴到此处,因为它很短,有关更多信息,请访问wordpress.org网站.
<?php $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <div> <?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?> </div> <?php endforeach; ?>
There's a guide for this precise case at the wordpress codex. See it here: I paste the code here because it's quite short, for more information go to the wordpress.org site.
<?php $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <div> <?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?> </div> <?php endforeach; ?>
-
- 2011-12-14
Wordpress为此类请求提供了一个功能: query_posts().
query_posts()是更改默认查询的最简单方法 WordPress用于显示帖子.使用query_posts()显示 与通常显示在特定位置的帖子不同的帖子 网址.
例如,在首页上,您通常会看到最新的10 帖子.如果您只想显示5个帖子(并且不在乎 分页),您可以像这样使用query_posts():
query_posts('posts_per_page=5');
执行查询后,您可以按所需方式显示帖子.
Wordpress provides a function for that kind of request: query_posts().
query_posts() is the easiest way to alter the default query that WordPress uses to display posts. Use query_posts() to display different posts than those that would normally show up at a specific URL.
For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don't care about pagination), you can use query_posts() like so:
query_posts( 'posts_per_page=5' );
Once you've performed the query, you can display the posts the way you want.
-
- 2019-05-16
<?php $the_query = new WP_Query( 'posts_per_page=3' ); while ($the_query -> have_posts()) : $the_query -> the_post();?> <?php /*html in here etc*/ the_title(); ?> <?php endwhile;wp_reset_postdata();?>
<?php $the_query = new WP_Query( 'posts_per_page=3' ); while ($the_query -> have_posts()) : $the_query -> the_post();?> <?php /*html in here etc*/ the_title(); ?> <?php endwhile;wp_reset_postdata();?>
-
它的代码回答了问题-如何在静态页面中显示最后3个帖子(最近的帖子)?如果我说-"我通常使用这种方法:"会对您有帮助吗?its code that answers the question - How to display last 3 posts (recent posts) in a static page? Would it help you if I said - "I usually use this approach:"?
- 0
- 2019-05-16
- Jon
我想在静态页面中实现类似"最新帖子"的内容:
http://themes.codehunk.me/insignio/(位于页脚)>
如果没有小部件,我将如何做到这一点?