Scorrere su misura di tipo post di tipo tassonomia personalizzato? (Per l'ordine post WordPress per categoria, o la visualizzazione di tipo messaggio personalizzato termine tassonomia)
-
27-09-2019 - |
Domanda
Voglio avere una pagina che mostra tutti i messaggi, separati per categoria. L'idea è di ottenere le categorie, e poi scorrere tutti i messaggi per ciascuna categoria. Il problema è complicato dal fatto che voglio per scorrere tutti i messaggi di un determinato tipo personalizzato, utilizzando una tassonomia personalizzato come le categorie. (Running Wordpress 3)
Nel mio functions.php, il mio tipo messaggio personalizzato viene registrato come "video" e la tassonomia personalizzato come "video_types".
Nel mio modello di pagina personalizzata che dovrebbe mostrare tutti i video organizzati per categoria, questo è il codice che non restituisce nessun post (e che stanno lì, ho controllato):
<?php
$categories = get_categories(array(
'taxonomy' => 'video_types'
));
foreach ($categories as $cat):
?>
<section id="<?php $cat->slug ?>" class="video-category">
<?php
query_posts(array(
'cat' => $cat->cat_ID,
'posts_per_page' => -1
));
?>
<h2><?php single_cat_title(); ?></h2>
<p class="description"><?php echo category_description($cat->cat_ID); ?></p>
<?php while (have_posts()) : the_post(); ?>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="video">
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>
</article>
<?php endwhile; ?>
</section>
<?php endforeach; ?>
Soluzione
Cavolo, una volta a capire che ogni elemento di una tassonomia personalizzato viene chiamato un termine (non immediatamente evidenti nella documentazione WordPress per il noob), è tutto molto più semplice da cercare. Questa soluzione è più facile da capire, senza tutta la roba query personalizzata.
<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
<section class="category-<?php echo $category ?>">
<h2><?php echo $category->name; // Print the cat title ?></h2>
<p class="description"><?php echo $category->description ?></p>
<div class="<?php echo $category->post_type ?>-list">
<?php
//select posts in this category (term), and of a specified content type (post type)
$posts = get_posts(array(
'post_type' => 'custom_post_type_name',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
// Now you can do things with the post and display it, like so
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<?php
// Getting custom field data example
echo get_post_meta($post->ID, 'field_key', true);
?>
<?php the_content() ?>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
E poi eventuali lacune nella comprensione possono essere riempiti con la ricerca delle funzioni sopra nel codice wordpress. Nel codice di cui sopra, per la mia domanda specifica, custom_post_type_name sarebbe il video, e taxonomy_name sarebbe video_type (o video_types, non ricordo).
Altri suggerimenti
Si potrebbe provare un approccio diverso. Provare a utilizzare get_posts per ottenere i tuoi messaggi ordinati per la tua tassonomia personalizzato, impostare una variabile che è inizialmente una stringa vuota (denominata $ current_cat o qualcosa del genere), e con ogni ciclo dei risultati, controllare la tassonomia e confrontarlo con $ current_cat - se diverso, stampare un colpo di testa per la nuova categoria e quindi la voce, se la stessa, stampare una voce come al solito.
Un problema evidente con il tuo codice (credo) è che non stai interrogando correttamente la vostra tassonomia personalizzato. Si deve usare semplicemente taxonomy_name => 'value'
all'interno la tua ricerca, una tassonomia personalizzata non sarà toccato da cat
in una query.
Fammi sapere se hai bisogno di ulteriori dettagli.
modifica: più particolare!
// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";
$categories = $wpdb->get_results($querystr, OBJECT);
foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description
$posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
foreach($posts as $post) : // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
[ ... ] // do things with your post (display it)
endforeach;
endforeach;
Questo dovrebbe farlo -. E questo può essere utile per l'utilizzo di get_posts