前提・実現したいこと
WordPress である外部プラグインで提供されているフィルターフックをメインループのみに実行させたいです。
現在、カスタム投稿ページ(single-users)の個別ページには記事そのもののメインループがあり、
サブループにはその他のおすすめ記事のサムネが表示されます。
発生している問題・エラーメッセージ
functions.phpに条件を指定したつもりですが、サブループにも関数の処理が適用されてしまいます。
どうすればサブループを適用外にすることができますか?
メインループとサブループを判別する為のコードが分かれば問題解決すると思うのですが、なかなか見つからず。。。
PHP
1// functions.php 2add_filter( 'some_hook', 'my_func', 10, 4 ); 3function my_func($html) { 4 // メインクエリーの時だけ実行したい。 5 if ( is_singular('users') && is_main_query() ) { 6 // $html を編集しreturnする 7 } 8}
念のため、以下にループのコードも。。。
PHP
1// single-users.php 2// メインループ 3<main> 4 <?php while ( have_posts() ) : the_post(); ?> 5 <?php get_template_part( 'template-parts/content', 'users' ); ?> 6 <?php endwhile; ?> 7 <?php wp_reset_postdata(); ?> 8</main> 9 10// サブループ 11<?php 12$args = array( 13 'post_type' => 'users', 14 'taxonomy' => 'users_category', 15 'posts_per_page' => 6, 16 'orderby' => 'rand', 17 'post__not_in'=> array($post->ID) 18); 19 20if ( $the_query->have_posts() ): 21 $the_query = new WP_Query( $args ); 22 while ( $the_query->have_posts() ) : $the_query->the_post(); 23 get_template_part( 'template-parts/content', 'archive-users' ); 24 endwhile; 25endif; 26wp_reset_postdata(); 27?>
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
WordPress 5.1.4
回答4件
あなたの回答
tips
プレビュー