首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >get_posts不工作,但query_posts工作(Wordpress)

get_posts不工作,但query_posts工作(Wordpress)
EN

Stack Overflow用户
提问于 2011-01-15 07:56:47
回答 1查看 6K关注 0票数 0

我使用下面的代码来获得具有不同类型和类别的帖子。问题是页面的主帖子消失了(您在管理员菜单的page部分中写的那个)。

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

但是,每次我将所有的query_posts更改为get_posts时,这些帖子就不会出现:

代码语言:javascript
复制
<?php get_posts('category_name=Events&showposts=5'); ?>

page-events.php:

代码语言:javascript
复制
<?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(); ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-15 11:57:28

query_posts()get_posts()的主要区别在于,第一个只用于修改主页面循环,而后者用于创建多个自定义循环。

因此,为了显示文章,您可以使用get_posts()和它自己的自定义循环。示例:

代码语言:javascript
复制
<?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()编写两个自定义查询循环,就像上面的例子一样(只需为后者更改查询参数)。

希望能帮上忙。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4698576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档