get_posts不起作用但query_posts不起作用(Wordpress)

ale*_*nco 0 wordpress

我正在使用以下代码来获取分配给它们的不同类型和类别的帖子.问题是页面的主要帖子消失了(您在管理员菜单的"页面"部分中编写的帖子).

我正在阅读Wordpress文档,他们说我应该使用get_post,这样它就不会干扰页面的主要帖子.

但每次我改变一切query_postsget_posts的信息也不会显示:

<?php get_posts('category_name=Events&showposts=5'); ?>
Run Code Online (Sandbox Code Playgroud)

页面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(); ?>
Run Code Online (Sandbox Code Playgroud)

ach*_*art 5

关于主要的区别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
Run Code Online (Sandbox Code Playgroud)

要保留原始帖子(您在该页面的"编辑"面板中插入的帖子),您可以在主循环之后编写两个自定义查询循环,get_posts()就像上面的示例一样(您只需要更改后者的查询参数) ).

希望能帮助到你.