0

WordPressのカテゴリ用に以下のコードがあります。投稿のタイトルを表示したいときに、カテゴリの名前を表示します。適切な投稿のタイトルを表示するにはどうすればよいですか。

<? query_posts('category_name=implants');
?>
<h3><?php single_cat_title(); ?></h3>
<?php if (have_posts()) : while (have_posts()) : 

the_post(); ?>
<?php the_content( __('Read the 

rest of this page »', 'template')); ?>
<?php endwhile; endif; ?></p>
4

1 に答える 1

2
  1. デフォルトの Wordpress ループを変更するつもりがない限り、query_posts を使用しないでください。標準の Wordpress クエリの代わりにWP_Queryを使用します。
  2. コードを見てください。single_cat_title() を呼び出しています。それはまさにそれがどのように見えるかを意味します: クエリされたカテゴリのタイトルを取得しています。投稿のタイトルを取得するには、the_title() を呼び出します。
  3. 上記ほど重要ではありませんが、開始タグは <? <?php ではなく。最初はわからなくても、サーバー側の言語を指定する習慣をつけて、潜在的な将来の問題を回避する必要があります。

修正したループは次のようになります。

<?php
$query = new WP_Query('category_name=implants');
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
the_content( __('Read the rest of this page »', 'template'));
endwhile; endif;
wp_reset_postdata();
?>
于 2012-05-31T18:23:32.397 に答える