2011-01-15 9 views
0

次のコードを使用して、さまざまなタイプとカテゴリが割り当てられた投稿を取得しています。問題は、ページのメインポストが消えてしまったことです(管理者メニューのページセクションに書いたもの)。get_postsは動作しませんが、query_postsは(Wordpress)

私はWordpressのドキュメントを読んでいて、ページのメインポストに干渉しないようにget_postを使うべきだと言っていました。

は、しかし、毎回私はすべてのquery_postsget_postsへの投稿が表示されない変更:

<?php get_posts('category_name=Events&showposts=5'); ?> 

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

答えて

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 

は(あなたができるコードは、メインループの後に、2つのカスタムクエリは、ちょうど上記の例のようにget_posts()でループし、あなたのオリジナルのポスト(あなたがそのページの編集パネルに挿入されたもの)を保持するには、あなた後者のクエリ引数を変更するだけで済みます)。

希望します。

+0

ありがとう、私にはまだ分かりませんが、 '$ post'変数を使用するだけです。あなたはそれの使い方を教えてくれますか? (Wordpressのデフォルトの妥協しない権利ですか?) – alexchenco

+1

@janoChen $ postオブジェクトは、Wordpressの投稿やページに関する情報を保持します。グローバル$ postオブジェクトはメインクエリ(単一の投稿テンプレートの場合はsingle.php)についての情報を保持し、ループのローカル$ postはカスタムクエリの現在のポストに関する情報を保持します。たいていの場合、テンプレートタグを使用するだけでその情報にアクセスできるので、対話する必要はありません。クイックリファレンスは次のとおりです。http://www.rlmseo.com/blog/wordpress-post-variable-quick-reference/ – achairapart

関連する問題