我使用下面的代码来获得具有不同类型和类别的帖子。问题是页面的主帖子消失了(您在管理员菜单的page部分中写的那个)。
我正在阅读Wordpress文档,他们说我应该使用get_post,这样它就不会干扰页面的主帖子。
但是,每次我将所有的query_posts更改为get_posts时,这些帖子就不会出现:
<?php get_posts('category_name=Events&showposts=5'); ?>page-events.php:
<?php
/**
* Template Name: Events Template
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php // find all content that has the category of Events and then to loop through them. ?>
<?php query_posts('category_name=Events&showposts=5'); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #container -->
<div id="container">
<div id="content" role="main">
<?php // find all content that has the type of video and then to loop through them. ?>
<?php query_posts(array('post_type'=>'video')); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( is_front_page() ) { ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php } else { ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php } ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>发布于 2011-01-15 11:57:28
query_posts()和get_posts()的主要区别在于,第一个只用于修改主页面循环,而后者用于创建多个自定义循环。
因此,为了显示文章,您可以使用get_posts()和它自己的自定义循环。示例:
<?php
$customposts = get_posts('category_name=Events&showposts=5' ); // note: you assign your query to a custom post object ($customposts)
foreach( $customposts as $post ) : // start you custom loop
setup_postdata($post); ?>
// do your things...
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php the_content() ?>
....
<?php endforeach; ?> // end the custom loop为了保留原来的文章(您在该页面的编辑面板中插入的那篇文章),可以在主循环之后用get_posts()编写两个自定义查询循环,就像上面的例子一样(只需为后者更改查询参数)。
希望能帮上忙。
https://stackoverflow.com/questions/4698576
复制相似问题