分類法ごとにカスタム投稿タイプですべての投稿を一覧表示します
5 回答
- 投票
-
- 2012-09-25
これを試してください
$custom_terms = get_terms('custom_taxonomy'); foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' => 'custom_post_type', 'tax_query' => array( array( 'taxonomy' => 'custom_taxonomy', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; while($loop->have_posts()) : $loop->the_post(); echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>'; endwhile; } }
分類法のすべての用語を取得し、それらをループして、その用語に属する各投稿へのタイトルリンクを起動します.分類用語を並べ替える必要がある場合は、プラグインを使用して簡単に並べ替えることができます. 分類法の並べ替えだと思います. ただしこのプラグインはアクティブ化時にテーブルに別の列を追加(!)し、非アクティブ化時に列を削除しないことに注意してください!
Try this
$custom_terms = get_terms('custom_taxonomy'); foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array('post_type' => 'custom_post_type', 'tax_query' => array( array( 'taxonomy' => 'custom_taxonomy', 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { echo '<h2>'.$custom_term->name.'</h2>'; while($loop->have_posts()) : $loop->the_post(); echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>'; endwhile; } }
We get all the terms of a taxonomy, loop through them, and fire off a title link to each post that belongs to that term. If you need to reorder the taxonomy terms, you can do so with a plugin pretty easily. Reorder Taxonomy, I believe. But pay attention that this plugin adds(!) another column to your table on activation and does not remove it upon deactivation!
-
こんにちは@GhostToastこれはうまく機能します.直接質問があります.分類法でこれをフィルタリングするにはどうすればよいですか.テニス、ゴルフ、サッカー、バレーボールがあります.このコードは、分類法がチェックされた投稿をすべて表示します.投稿とともにサッカー分類のみを表示します.Hi @GhostToast This works great, I have a direct question, how can I filter this by taxonomy, I have tennis, golf, soccer, voleyball, this codes brings them all with their post that have the taxonomy checked, How can I filter to only show the Soccer Taxonomy with its posts.
- 0
- 2017-03-07
- Rodrigo Zuluaga
-
@RodrigoZuluagaは、基本的な単一のクエリになります.`$ custom_terms`と`foreach() `を取り除き、` 'terms'`をスラッグまたは必要なものに手動で定義するだけです.@RodrigoZuluaga that would be a basic single query then. take away `$custom_terms` and the `foreach()` and just define `'terms'` manually to the slug or whatever you want.
- 0
- 2017-03-07
- GhostToast
-
私はそれを少し違うと思いますが、あなたのコードは地獄です $ args=array( 'post_type'=> 'publica'、 'tax_query'=> array( アレイ( '分類法'=> 'comision-publicaciones'、 'フィールド'=> '名前'、 'terms'=> array($ter_name) )、 )、 );I got it little different I think but your code hell good $args = array('post_type' => 'publica', 'tax_query' => array( array( 'taxonomy' => 'comision-publicaciones', 'field' => 'name', 'terms' => array($ter_name) ), ), );
- 0
- 2017-03-08
- Rodrigo Zuluaga
-
- 2012-09-25
特に洗練されたソリューションではありませんが、特定の用語に対してそれぞれ複数のクエリを作成し、それらを出力することができます.うまくいけば、誰かが自動的に用語をプルして出力/ソートを変更するより良い方法を思い付くことができます.しかし、これでうまくいくでしょう.
<?php //First Query for Posts matching term1 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term1' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; } //RESET YOUR QUERY VARS wp_reset_query(); //Second Query for term2 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term2' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; }
Not a particularly elegant solution but you can create multiple queries each for the specific terms and then output them. Hopefully someone can come up with a nicer way of automatically pulling the terms to modify the output/sorting. But this would get you going.
<?php //First Query for Posts matching term1 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term1' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; } //RESET YOUR QUERY VARS wp_reset_query(); //Second Query for term2 $args = array( 'tax_query' => array( array( 'taxonomy' => 'taxonomy_1', 'field' => 'slug', 'terms' => array( 'term2' ) ), ), 'post_type' => 'my-post-type' ); $query = new WP_Query( $args ); if ( have_posts() ) { $term = $query->queried_object; echo 'All posts found in ' . $term->name; while ( have_posts() ) : the_post(); //Output what you want the_title(); the_content(); endwhile; }
-
- 2014-07-10
いいね! GhostOneのソリューションは私が探していたものでした. 私の状況では、カスタム投稿タイプは「minining_accidents」であり、これに関連するカスタム分類法は「accident-types」であり、その下に複数の用語がありました.私のアイデアは、このカスタム分類法の用語の下で投稿のリストを表示するカスタムウィジェットを作成することでした.私の試運転では、それは私が望むものを手に入れました.残りはトウヒでした.これが私のコードです:
function fn_get_list_of_mining_accident_types() { $custom_taxonomy='accident-types'; $custom_terms = get_terms($custom_taxonomy); $str_return='<ul>'; foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array( 'post_type' => 'minining_accidents', 'tax_query' => array( array( 'taxonomy' => $custom_taxonomy, 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); $term_name=$custom_term->name; $term_slug=$custom_term->slug; $term_link=get_term_link($term_slug, $custom_taxonomy); $str_return.='<li><a href="'.$term_link.'">'.$term_name.'</a>'; if($loop->have_posts()) { $str_return.='<ol>'; while($loop->have_posts()) : $loop->the_post(); $str_return.='<li><a href="'.get_permalink().'">'.get_the_title().'</a></li> '; endwhile; $str_return.='</ol>'; } $str_return.='</li>'; } $str_return.='</ul>'; return $str_return; }
はい!コードをさらに改善するオプションは常にあります.
Nice one! GhostOne's solution was what I had been looking for. In my situation the custom post type was 'minining_accidents' and custom taxonomies associated with this was 'accident-types' which had multiple terms under it. My idea was to create a custom widget to show list of posts under terms in this custom taxonomies. In my trial run it got what I wanted. Rest was spruce up. Here is my code:
function fn_get_list_of_mining_accident_types() { $custom_taxonomy='accident-types'; $custom_terms = get_terms($custom_taxonomy); $str_return='<ul>'; foreach($custom_terms as $custom_term) { wp_reset_query(); $args = array( 'post_type' => 'minining_accidents', 'tax_query' => array( array( 'taxonomy' => $custom_taxonomy, 'field' => 'slug', 'terms' => $custom_term->slug, ), ), ); $loop = new WP_Query($args); $term_name=$custom_term->name; $term_slug=$custom_term->slug; $term_link=get_term_link($term_slug, $custom_taxonomy); $str_return.='<li><a href="'.$term_link.'">'.$term_name.'</a>'; if($loop->have_posts()) { $str_return.='<ol>'; while($loop->have_posts()) : $loop->the_post(); $str_return.='<li><a href="'.get_permalink().'">'.get_the_title().'</a></li> '; endwhile; $str_return.='</ol>'; } $str_return.='</li>'; } $str_return.='</ul>'; return $str_return; }
Yes! There always is an option to further improve the code.
-
- 2020-06-14
これは私がこのスレッドに来る前に試した長い解決策です.これが誰かがよりよく理解するのに役立つことを願っています.
<?php // Get list of all taxonomy terms -- In simple categories title $args = array( 'taxonomy' => 'project_category', 'orderby' => 'name', 'order' => 'ASC' ); $cats = get_categories($args); // For every Terms of custom taxonomy get their posts by term_id foreach($cats as $cat) { ?> <a href="<?php echo get_category_link( $cat->term_id ) ?>"> <?php echo $cat->name; ?> <br> <?php // echo $cat->term_id; ?> <br> </a> <?php // Query Arguments $args = array( 'post_type' => 'portfolio', // the post type 'tax_query' => array( array( 'taxonomy' => 'project_category', // the custom vocabulary 'field' => 'term_id', // term_id, slug or name (Define by what you want to search the below term) 'terms' => $cat->term_id, // provide the term slugs ), ), ); // The query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<h2> List of posts tagged with this tag </h2>'; echo '<ul>'; $html_list_items = ''; while ( $the_query->have_posts() ) { $the_query->the_post(); $html_list_items .= '<li>'; $html_list_items .= '<a href="' . get_permalink() . '">'; $html_list_items .= get_the_title(); $html_list_items .= '</a>'; $html_list_items .= '</li>'; } echo $html_list_items; echo '</ul>'; } else { // no posts found } wp_reset_postdata(); // reset global $post; ?> <?php } ?>
This is long solution i tried before coming to this thread. Hope this may help someone to understand better.
<?php // Get list of all taxonomy terms -- In simple categories title $args = array( 'taxonomy' => 'project_category', 'orderby' => 'name', 'order' => 'ASC' ); $cats = get_categories($args); // For every Terms of custom taxonomy get their posts by term_id foreach($cats as $cat) { ?> <a href="<?php echo get_category_link( $cat->term_id ) ?>"> <?php echo $cat->name; ?> <br> <?php // echo $cat->term_id; ?> <br> </a> <?php // Query Arguments $args = array( 'post_type' => 'portfolio', // the post type 'tax_query' => array( array( 'taxonomy' => 'project_category', // the custom vocabulary 'field' => 'term_id', // term_id, slug or name (Define by what you want to search the below term) 'terms' => $cat->term_id, // provide the term slugs ), ), ); // The query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<h2> List of posts tagged with this tag </h2>'; echo '<ul>'; $html_list_items = ''; while ( $the_query->have_posts() ) { $the_query->the_post(); $html_list_items .= '<li>'; $html_list_items .= '<a href="' . get_permalink() . '">'; $html_list_items .= get_the_title(); $html_list_items .= '</a>'; $html_list_items .= '</li>'; } echo $html_list_items; echo '</ul>'; } else { // no posts found } wp_reset_postdata(); // reset global $post; ?> <?php } ?>
-
- 2014-07-29
カスタム分類法からのカスタム投稿のリストを表示するには
$args = array( 'tax_query' => array( array( 'taxonomy' => 'your-custom-taxonomy', 'field' => 'slug', 'terms' => array( 'your-term' ) ), ), 'post_type' => 'your-post-type' ); $loop = new WP_Query($args); if($loop->have_posts()) { $term = $wp_query->queried_object; while($loop->have_posts()) : $loop->the_post(); //Output what you want echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; endwhile; }
タイトル
- リストアイテム
- リストアイテム
- リストアイテム
To show a list of custom posts from a custom taxonomy
$args = array( 'tax_query' => array( array( 'taxonomy' => 'your-custom-taxonomy', 'field' => 'slug', 'terms' => array( 'your-term' ) ), ), 'post_type' => 'your-post-type' ); $loop = new WP_Query($args); if($loop->have_posts()) { $term = $wp_query->queried_object; while($loop->have_posts()) : $loop->the_post(); //Output what you want echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; endwhile; }
Title
- List item
- List item
- List item
特定のカスタム投稿タイプのすべての投稿を一覧表示し、それらに添付されているカスタム分類用語で並べ替える方法はありますか?
例;
タクソニーターム#1
投稿タイプ
投稿タイプ
投稿タイプ
分類用語#2
投稿タイプ
投稿タイプ
どんな助けでも大歓迎です.
ありがとうございます.