LoginSignup
ishiguro3
@ishiguro3

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

固定ページにカスタム投稿をカテゴリーごとに記事を取得する方法

解決したいこと

固定ページに特定のカスタム投稿をカテゴリーごとに記事を取得したい。

例)
Dというカスタム投稿記事の『あ』『い』『う』『え』というカテゴリーを、
各カテゴリ別で設置していきたいんですけど上手くできません。

固定ページに特定のカスタム投稿を設置する所まではできました。
あとは上記のカテゴリー別けでもできるようにしたいです。

参考記事なんかで、
tax_queryというWordPressの関数が使えるのではと考えています。

でも、うまくいきません。
タクソノミーやタームの指定が間違っている可能性もあるのですが、
一度コードをチェックして欲しいです。
よろしくお願いいたします。

該当するソースコード

固定ページ

<ul class="">
<?php
	$player = array(
	'posts_per_page' => -1,
	'post_type' => 'player',
	'order' => 'DESC',
    'tax_query' => array(
      array(
        'taxonomy' => 'tax_player',
        'terms' => array('boxser'),
        'field' => 'slug'
      ),
    ),
	        );
	$the_query = new WP_Query( $player );
?>
<?php if ( $the_query->have_posts() ): while ( $the_query->have_posts() ): $the_query->the_post(); ?>
	<li>
		<figure class="img_photo">
			<?php 
			if ( has_post_thumbnail() ) {
				the_post_thumbnail();
			}else{ ?>
				<img src="<?php echo get_template_directory_uri(); ?>/images/info/pc/demo_sam.jpg" alt="<?php the_title(); ?>">
			<?php } ?>
		</figure>
		<div class="txt_box">
		    <a class="modal_open" data-target="<?php the_ID(); ?>">
		    	<h4 class="left_top"><?php the_title(); ?></h4>
		    </a>
		</div>
	</li>
<?php endwhile; endif; wp_reset_postdata(); ?>
</ul>

function.php

  //カスタム投稿D(ここから)
  register_post_type('player', array(
    'labels' => array(
    'name' => 'カスタム投稿D',
    'singular_name' => 'カスタム投稿D',
  ),
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => 5,
    'supports' => array('title', 'editor', 'thumbnail'),
    'menu_icon' => 'dashicons-edit'//追記
    ));
  // 追加した箇所
  register_taxonomy('tax_player',array('player'), array(
    'hierarchical' => true,
    'label' => 'カテゴリー',
    'show_ui' => true,
    'public' => true
  ));
  register_taxonomy_for_object_type( 'tax_player', 'player' );
  // 追加した箇所
  //カスタム投稿D(ここまで)

カテゴリのスラッグ

boxer

ちなみにモーダルも実装しないといけないです。
モーダルの方は他カスタム投稿A~Cまで実装済みで問題なく動くので、
あとはカスタム投稿Dのコードに合わせて修正すればいいかなと考えてます。

以下は、固定ページに設置した他カスタム投稿のコードです。

<!-- カスタム投稿一覧 -->
<ul class="voice_box flex_box">
<?php
	$voice = array(
	'posts_per_page' => -1,
	'post_type' => 'voice'
	        );
	$the_query = new WP_Query( $voice );
	if ( $the_query->have_posts() ) :?>
	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
	<li>
		<figure class="img_photo">
			<?php 
			if ( has_post_thumbnail() ) {
				the_post_thumbnail();
			}else{ ?>
				<img src="<?php echo get_template_directory_uri(); ?>/images/info/pc/demo_sam.jpg" alt="<?php the_title(); ?>">
			<?php } ?>
		</figure>
		<div class="txt_box">
		    <a class="modal_open" data-target="<?php the_ID(); ?>">
				<span>VOICE</span>
		    	<h4 class="left_top"><?php the_title(); ?></h4>
		    </a>
		</div>
	</li>
	<?php endwhile;?>
<?php wp_reset_postdata(); endif; ?>
</ul>
<!-- /カスタム投稿一覧 -->

<!-- モーダルウィンドウ -->
<div id="modal_voice" class="modal_voice" >
<?php
	$voice = array(
	'posts_per_page' => -1,
	'post_type' => 'voice'
	        );
	$the_query = new WP_Query( $voice );
	if ( $the_query->have_posts() ) :?>
	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
	  <div id="<?php the_ID(); ?>" class="modal js-modal">
	    <div class="modal-bg modal_close js-modal-close"></div>

	    <div class="modal_content">
			<div class="modal_box">
				<figure class="img_photo">
				<?php 
				if ( has_post_thumbnail() ) {
					the_post_thumbnail();
				}else{ ?>
					<img src="<?php echo get_template_directory_uri(); ?>/images/info/pc/demo_sam.jpg" alt="<?php the_title(); ?>">
				<?php } ?>
			    <h4 class="left_top"><?php the_title(); ?></h4>
				<?php the_content(); ?>
			</div>
			<!-- <p class="">close</p> -->
			<a class="modal-close modal_close"><img src="<?php echo get_template_directory_uri(); ?>/images/common/close.png" alt="閉じるボタン"></a>
	    </div>
	  </div>
	<?php endwhile;?>
<?php wp_reset_postdata(); endif; ?>
</div>
<!-- /モーダルウィンドウ -->

自分で試したこと

いろいろ参考記事を探して、
以下の方法が一番しっくりきたので実践して上手くいかなくて、悩んでます。

参考URL
https://wordpress-web.and-ha.com/tax-query-category-of-custom-posts/

0

1Answer

手詰まりだったので、他の参考記事は探してみました。
https://www.nowte.net/wordpress/wordpress-term-post-list/

こっちのコードで値を習得できたので、
なんとかこっちでやってみます。

モーダルは他のカスタム投稿のソースコードの使いまわしでできそうです。

各カテゴリ毎に項目内容の数が少し違うのですが、
そこはCSSなんかで無理やり項目を非表示にできたらいいんですけど。

0Like

Your answer might help someone💌