Existuje způsob, jak mohu vypsat všechny příspěvky ve specifickém typu vlastního příspěvku a uspořádat je podle vlastního názvu taxonomie připojeného k nim?
Například;
Pojem Taxonmy # 1
Typ příspěvku
Typ příspěvku
Typ příspěvku
Termín taxonomie # 2
Typ příspěvku
Typ příspěvku
Jakákoli pomoc by byla nejvíce oceněna.
Dík.
Zkuste to
$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;
}
}
Dostaneme všechny podmínky taxonomie, smyčky přes ně, a oheň z názvu odkaz na každé místo, které patří do tohoto termínu. Pokud potřebujete změnit pořadí taxonomií, můžete tak učinit snadno pomocí pluginu. Reorder Taxonomie , věřím. Ale věnujte pozornost tomu, že tento plugin přidá (!) Další sloupec do tabulky o aktivaci a neodstraní jej při deaktivaci !
Není to nijak zvlášť elegantní řešení, ale můžete vytvořit více dotazů pokaždé pro konkrétní podmínky. Doufejme, že někdo může přijít s hezčí způsob, jak automaticky tahání termíny změnit výstup/třídění. Ale tohle by vám pomohlo.
<?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;
}
Výborně! Řešení GhostOne bylo to, co jsem hledal. V mé situaci byl typ vlastního příspěvku „minining_accidents“ a vlastní taxonomie spojená s tímto typem byla „nehoda-typy“, které měly pod sebou několik výrazů. Mým nápadem bylo vytvořit vlastní widget, který bude zobrazovat seznam příspěvků pod podmínkami v této vlastní taxonomii. V mém pokusu jsem dostal to, co jsem chtěl. Zbytek byl smrk. Zde je můj kód:
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;
}
Ano! Vždy existuje možnost dalšího zdokonalení kódu.