次のコードを使用して、さまざまなタイプとカテゴリが割り当てられた投稿を取得しています。問題は、ページのメインポストが消えてしまったことです(管理者メニューのページセクションに書いたもの)。get_postsは動作しませんが、query_postsは(Wordpress)
私はWordpressのドキュメントを読んでいて、ページのメインポストに干渉しないようにget_postを使うべきだと言っていました。
は、しかし、毎回私はすべてのquery_posts
get_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(); ?>
ありがとう、私にはまだ分かりませんが、 '$ post'変数を使用するだけです。あなたはそれの使い方を教えてくれますか? (Wordpressのデフォルトの妥協しない権利ですか?) – alexchenco
@janoChen $ postオブジェクトは、Wordpressの投稿やページに関する情報を保持します。グローバル$ postオブジェクトはメインクエリ(単一の投稿テンプレートの場合はsingle.php)についての情報を保持し、ループのローカル$ postはカスタムクエリの現在のポストに関する情報を保持します。たいていの場合、テンプレートタグを使用するだけでその情報にアクセスできるので、対話する必要はありません。クイックリファレンスは次のとおりです。http://www.rlmseo.com/blog/wordpress-post-variable-quick-reference/ – achairapart