get_posts doesn't work but query_posts does (W

2019-08-18 06:18发布

问题:

I'm using the following code to get posts with different types and categories assigned to them. The problem is that the main post of the page disappeared (the one you write in the Page section of the administrator menu).

I was reading the Wordpress documentation and they said that I should use get_post so that it wouldn't interfere with the main post of the page.

But everytime I change the all the query_posts to get_posts the posts don't appear:

<?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(); ?>

回答1:

The main difference about query_posts() and get_posts() is that the first is intended to be used to modify the main page loop only and the latter is used for creating multiple custom loops.

So, in order to show the posts you may use get_posts() with its own custom loop. Example:

<?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

To preserve your original post (the one you inserted in the Edit panel for that page), you can code, after the main loop, two custom query loops with get_posts() just like the example above (you only have to change the query arguments for the latter).

Hope it helps.



标签: wordpress