Scorri le tassonomie personalizzate e visualizza i post
-
-
Non capisco completamente latua domanda.Quando chiedi "ciclo attraverso ognitassonomiapersonalizzata",intendi "ciclo attraversoi termini di ognitassonomiapersonalizzata?"Inoltre,quando chiedi di "produrre unnumero dipost" significa "produrre"inserirenuoveistanze di untipo dipostnel database ogenerare HTMLperiltipo dipost?I don't completely understand your question. When you ask " loop through each custom taxonomy" do you mean " loop through the terms of each custom taxonomy?" Also, when you ask to "produce a number of posts" does "produce" mean insert new instances of a post type into the database or generate HTML for the post type?
- 0
- 2010-08-14
- MikeSchinkel
-
4 risposta
- voti
-
- 2010-08-16
Hopensato difornire un'altra rispostapoiché quella sopra è unpo 'complicata,inoltre ho aggiunto un altro livello che ottienetutte letassonomieper unpost_type.
$post_type = 'post'; // Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) ); foreach( $taxonomies as $taxonomy ) : // Gets every "category" (term) in this taxonomy to get the respective posts $terms = get_terms( $taxonomy ); foreach( $terms as $term ) : $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" ); if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); //Do you general query loop here endwhile; endif; endforeach; endforeach;
Si consiglia di aggiungere ogniposttrovato a un array
$post__not_in
,in modo dapoterlopassare aWP_Query
perimpedire la ricezione dipost duplicati.I thought I would provide another answer as the above one is a little hacky, also I added another layer that gets all taxonomies for a post_type.
$post_type = 'post'; // Get all the taxonomies for this post type $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) ); foreach( $taxonomies as $taxonomy ) : // Gets every "category" (term) in this taxonomy to get the respective posts $terms = get_terms( $taxonomy ); foreach( $terms as $term ) : $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=2" ); if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); //Do you general query loop here endwhile; endif; endforeach; endforeach;
It would be recommended to add each post found to a
$post__not_in
array, so you could pass that to theWP_Query
to prevent duplicate posts coming through. -
- 2012-08-31
Stai cercando questo?
<?php query_posts(array('post_type' => 'post type name', 'Taxonomy slug' => $term->slug ) ); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
Come creare un loop ditassonomiapersonalizzato
Spero chepossa aiutare
Are you looking for this ?
<?php query_posts(array('post_type' => 'post type name', 'Taxonomy slug' => $term->slug ) ); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
How to create Custom Taxonomy loop
Hope it will help
-
- 2016-08-03
Copiae incolla questafunzioneneltuofunctions.php
if ( ! function_exists( 'display_all_products_from_all_categories' ) ) { function display_all_products_from_all_categories() { // Get all the categories for Custom Post Type Product $args = array( 'post_type' => 'product', 'orderby' => 'id', 'order' => 'ASC' ); $categories = get_categories( $args ); foreach ($categories as $category) { ?> <div class="<?php echo $category->slug; ?>"> <!-- Get the category title --> <h3 class="title"><?php echo $category->name; ?></h3> <!-- Get the category description --> <div class="description"> <p><?php echo category_description( get_category_by_slug($category->slug)->term_id ); ?></p> </div> <ul class="mhc-product-grid"> <?php // Get all the products of each specific category $product_args = array( 'post_type' => 'product', 'orderby' => 'id', 'order' => 'ASC', 'post_status' => 'publish', 'category_name' => $category->slug //passing the slug of the current category ); $product_list = new WP_Query ( $product_args ); ?> <?php while ( $product_list -> have_posts() ) : $product_list -> the_post(); ?> <li class="product <?php the_field( 'product_flavor' ); ?>"> <a href="<?php the_permalink(); ?>" class="product-link"> <!-- if the post has an image, show it --> <?php if( has_post_thumbnail() ) : ?> <?php the_post_thumbnail( 'full', array( 'class' => 'img', 'alt' => get_the_title() ) ); ?> <?php endif; ?> <!-- custom fields: product_flavor, product_description ... --> <h3 class="title <?php the_field( 'product_flavor' ); ?>"><?php the_title(); ?></h3> <p class="description"><?php the_field( 'product_description' ); ?></p> </a> </li> <?php endwhile; wp_reset_query(); ?> </ul> </div> <?php } } }
Quindi chiamalo da qualsiasipunto deltuomodello con:
display_all_products_from_all_categories();
Copy and paste this function in your functions.php
if ( ! function_exists( 'display_all_products_from_all_categories' ) ) { function display_all_products_from_all_categories() { // Get all the categories for Custom Post Type Product $args = array( 'post_type' => 'product', 'orderby' => 'id', 'order' => 'ASC' ); $categories = get_categories( $args ); foreach ($categories as $category) { ?> <div class="<?php echo $category->slug; ?>"> <!-- Get the category title --> <h3 class="title"><?php echo $category->name; ?></h3> <!-- Get the category description --> <div class="description"> <p><?php echo category_description( get_category_by_slug($category->slug)->term_id ); ?></p> </div> <ul class="mhc-product-grid"> <?php // Get all the products of each specific category $product_args = array( 'post_type' => 'product', 'orderby' => 'id', 'order' => 'ASC', 'post_status' => 'publish', 'category_name' => $category->slug //passing the slug of the current category ); $product_list = new WP_Query ( $product_args ); ?> <?php while ( $product_list -> have_posts() ) : $product_list -> the_post(); ?> <li class="product <?php the_field( 'product_flavor' ); ?>"> <a href="<?php the_permalink(); ?>" class="product-link"> <!-- if the post has an image, show it --> <?php if( has_post_thumbnail() ) : ?> <?php the_post_thumbnail( 'full', array( 'class' => 'img', 'alt' => get_the_title() ) ); ?> <?php endif; ?> <!-- custom fields: product_flavor, product_description ... --> <h3 class="title <?php the_field( 'product_flavor' ); ?>"><?php the_title(); ?></h3> <p class="description"><?php the_field( 'product_description' ); ?></p> </a> </li> <?php endwhile; wp_reset_query(); ?> </ul> </div> <?php } } }
Then call it from anywhere in your template with:
display_all_products_from_all_categories();
-
- 2010-08-14
Controlla questoesempio;crea un ciclopersonaleper latuatassonomia.Puoi anche usarloin un cicloforeachper usaretutte le categorie.Oppure ènecessario creare una query SQLpersonalizzata.
<?php $taxonomies = get_the_term_list($post->ID, 'YOUR_TAXONOMIE', '', '', ''); $taxonomies = explode('>', $taxonomies); $taxonomies = $taxonomies[1]; $myq = new WP_Query('your_taxonomie = '.$taxonomies); if ($myq->have_posts()) : while ($myq->have_posts()) : $myq->the_post(); // the loop ?> <?php the_title();?> <?php the_content();?> <?php endwhile; else:?> <?php endif;?>
Please check this example; create a own loop for your taxonomie. You can also use this in a foreach-loop to use all categories. Or you must create an own sql-query.
<?php $taxonomies = get_the_term_list($post->ID, 'YOUR_TAXONOMIE', '', '', ''); $taxonomies = explode('>', $taxonomies); $taxonomies = $taxonomies[1]; $myq = new WP_Query('your_taxonomie = '.$taxonomies); if ($myq->have_posts()) : while ($myq->have_posts()) : $myq->the_post(); // the loop ?> <?php the_title();?> <?php the_content();?> <?php endwhile; else:?> <?php endif;?>
Anche se sono riuscito afarlofunzionarepernormali categorie WP,non sono riuscito afarlofunzionarepertassonomiepersonalizzate.
Vorrei scorrere ognitassonomiapersonalizzata (categorienelmio caso)e produrre unnumero dipostper ciascuna.
Unesempio dell'outputpotrebbeessere:
Ovviamente si ripeterà attraverso qualsiasitassonomia disponibileperiltipo dipostpersonalizzato.