0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

特定のWP_Queryをあちこちで使い回す

Posted at

functions.phpに関数を書く

function myquery_args() {
  global $myquery_args;
  $myquery_args = array(
    'post_type' => '投稿タイプ',
    'posts_per_page' => -1,
    'tax_query' => array(
      array(
        'taxonomy' => 'タクソノミー名',
        'field' => 'slug',
        'terms' => 'ターム名',
      ),
    ),
  );
}
add_action( 'after_setup_theme', myquery_args );

出力箇所

<?php
  $myquery_query = new WP_Query($myquery_args);
  if($myquery_query->have_posts()): while($myquery_query->have_posts()): $myquery_query->the_post();
?>

header.php、footer.phpは関数をglobal宣言しないと使えないっぽい

 <?php 
  global $myquery_args;//←これ
  $myquery_query = new WP_Query($myquery_args);
  if($myquery_query->have_posts()): while($myquery_query->have_posts()): $myquery_query->the_post();
?>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?