get_posts не работает, но query_posts работает (Wordpress) - PullRequest
0 голосов
/ 15 января 2011

Я использую следующий код для получения постов с разными типами и категориями.Проблема в том, что основной пост страницы исчез (тот, который вы пишете в разделе «Страница» в меню администратора).

Я читал документацию 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(); ?>

1 Ответ

5 голосов
/ 15 января 2011

Основное отличие от 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(), какпример выше (вам нужно только изменить аргументы запроса для последнего).

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...