すみません、初めの部分の記述がおかしくなってしまったので、再度掲載します。
<ul>
<?php $posts = get_posts('numberposts=-1&cat=数字'); global $post;?>
<?php foreach($posts as $post): ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
ご回答ありがとうございます。
tax_queryを使って下記のように書いてみましたが、
思ったような動きをしてくれません。
どこをどうすればいいでしょうか。
カテゴリー:レシピに含まれていて、かつタグで野菜が付いているものを
表示したい場合。
<ul>
<?php
$args = array(
'post_type' =>'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'recipe',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => '野菜',
),
),
);
$query = new WP_Query( $args ); ?>
<?php foreach($posts as $post): ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
引数は numberposts => -1 も指定しておけば、合っていると思います。
$query = new WP_Query( $args );
↓↓↓
$posts = get_posts( $args );
ループ部分
<?php foreach($posts as $post): ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
は、必ずしも間違いではないですが、
<?php foreach($posts as $post): ?>
<?php setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
としておいた方が、いらぬトラブルに巻き込まれません。
教えていただいた通りにループ部分も変更して書き換えたところ、5件は表示できるようになりました。
ありがとうございます!
numberposts => -1を下記のように記述したところ画面が真っ白になってしまいます。
$posts = get_posts( numberposts => -1,$args );
numberposts => -1はどこに記述したらいいでしょうか。
他にもいろいろな部分に書いてみましたが、画面が真っ白になるか
カテゴリとタグの指定関係なしで全記事を表示することしか出来ませんでした。
$args = array(
'post_type' =>'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'recipe',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => '野菜',
),
),
'numberposts' => -1
);
です。
pluto1234 様
一から十まで本当にお世話になりました!
思った通りの動きにすることが出来ました!!
今度から色々な動きをさせることが出来そうです。
ありがとうございました。
今後同じ事をしたい方のために最終的に出来上がったコードを掲載しておきます。
<ul class="kobunrui">
<?php
$args = array(
'post_type' =>'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'recipe',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => '野菜',
),
),
'numberposts' => -1
);
$posts = get_posts( $args ); ?>
<?php foreach($posts as $post): ?>
<?php setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</ul>