web-dev-qa-db-fra.com

Grouper le type de message personnalisé dans une page à l'aide de son tag taxonomie

J'espère que je peux écrire une question claire depuis que je suis confus en essayant de résoudre ce problème ...

J'ai un type de message personnalisé ... ce type de message personnalisé a taxomony (catégorie comme)
et je veux retourner une structure comme celle-ci dans une page qui montre tous les messages de
ce type de message ..

--------------------------------------------- STURCTURE SOUHAITÉE

nom de la catégorie de taxomonie (Exemple: new york)
- post one (associé à la catégorie new york)
- poste deux (associé à la catégorie new york)
- poste trois (associée à la catégorie new york)

nom de la catégorie de taxomonie (exemple: Washington DC)
- poste un (associé à Washington DC catégorie)
- poste deux (associé à Washington DC catégorie)
- poste trois (catégorie associée à Washington DC)
- poste quatre (catégorie associée à Washington DC)
- poste cinq (associé à Washington DC catégorie)

nom de la catégorie de taxomonie (Exemple: SomeCity)
- post one (associé à la catégorie SomeCity)
- poste deux (associé à la catégorie SomeCity)

C'est le code que j'ai maintenant:

        <div class="pageContent portfolioPage">

            <!--=STR== PAGE TITLE ===-->
            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>   
                <h1 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h1>
                <?php the_content(); ?>
            <?php endwhile; endif; ?>
            <!--=END== PAGE TITLE ===-->

            <!--=STR== OUR CLIENTS ===-->                   
            <?php 
            /*
                query_posts(array( 
                    'post_type' => 'vacationrental',
                    'showposts' => 100
                ) );  
            */
            $loop = new WP_Query( array( 
            'post_type' => 'vacationrental',
            'post_per_page' => 100,
            'orderby' => 'date',
            'order' => 'ASC'
            ));             

            ?>


            <?php while ($loop->have_posts()) : $loop->the_post(); ?>
                <div class="vacationRentalBox">
                    <div class="vacationRentalBox-inner">

                        <?php
                        $image_id = get_post_thumbnail_id();  
                        $image_url = wp_get_attachment_image_src($image_id,'large');  
                        $image_url = $image_url[0];  
                        // $googleMapsUrl = get_post_meta( $post->ID, 'vacationrental_map', true ); // GOOGLE MAPS URL
                        ?>              

                        <div class="vacationRentalBar">
                            <div class="vacationRentalName"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> 

                            <div class="thumbsingle">
                            <a title="<?php the_title(); ?>" href="<?php echo $image_url; ?>" class="lightbox"><?php the_post_thumbnail('testimonialPage'); ?></a>
                            </div>      

                            <div class="vacationRentalDescription"><?php dynamic_excerpt(315); ?></div> 

                            <div class="vacationRentalExtraInfo">
                                <div class="vacationRentalReadMore"><a href="<?php the_permalink(); ?>" rel="nofollow"><?php _e('Read More &rarr;' ,'sagive'); ?></a></div>                             
                            </div>
                            <br style="clear: both;" />
                        </div>

                    </div>
                </div>

            <?php endwhile;?>
            <!--=END== OUR CLIENTS ===-->                           

            <br style="clear: both;" />
        </div>

.
J'ai réussi à obtenir la liste des catégories de taxomonies en utilisant ceci:

$terms = get_terms("locations");
$count = count($terms);
if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
    echo "<li>" . $term->name . "</li>";

    }
    echo "</ul>";
}

Mais je ne sais pas comment intégrer cela dans la boucle/structure Souhaiterait vraiment votre aide puisque je suis coincé ...

2
Sagive SEO

Si je suis bien votre question, vous pourriez utiliser une boucle de requête imbriquée, qui parcourt vos termes de taxonomie, puis effectue une boucle WP_Query pour chacun.

Il existe une approche plus complexe qui utilise un code SQL personnalisé et un filtre, mais voici l'exemple que je choisirais:

$terms = get_terms("locations");
$count = count($terms);
if ( $count > 0 ){
    foreach ( $terms as $term ) {
        echo '<h2>' . $term->name . '</h2>';
        echo '<ul>';
        $loop = new WP_Query( array( 
            'post_type' => 'vacationrental',
            'post_per_page' => 100,
            'orderby' => 'date',
            'order' => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'locations',
                    'field' => 'id',
                    'terms' => $term->term_id
                )
            )
        ));
        // the loop
        while ($loop->have_posts()) : $loop->the_post();
            // do loop content
            echo '<li>' . get_the_title() . '</li>';
        endwhile;
        // reset $post so that the rest of the template is in the original context
        wp_reset_postdata();
        echo '</ul>';
    }
}
4
sanchothefat

ok, c'est vieux et tu as une solution, mais j'essayais de faire la même chose. Voici comment j'ai fini par le faire avec SQL voodoo patché ensemble depuis plusieurs endroits, mais surtout:

En utilisant wp_query est-il possible de commander par taxonomie? et

comment regrouper les publications de type publication personnalisée par terme de taxonomie personnalisée

d'abord la requête vaudou:

function wpa_38075( $clauses, $wp_query ) {
    global $wpdb;

    if( !is_post_type_archive( 'vacationrental' )) return $clauses;


        //join term_relationships to posts, and term_relationships to term_taxonomy and term_taxonomy to terms
        $clauses['join'] .= "LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)" ;

        //look for posts with and without a subject term
        $clauses['where'] .= " AND (taxonomy = 'locations' OR taxonomy IS NULL)";

        $clauses['groupby'] = "object_id";

        //remove limits
        $clauses['limits'] = "";

        //group posts by term name
        $clauses['orderby']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";

    return $clauses;
}


add_filter( 'posts_clauses', 'wpa_38075', 10, 2 );

et ensuite dans votre modèle:

<?php 

$prev = ''; //not set yet

if (have_posts()) :

    echo "<ul>";

while ( have_posts() ) : the_post();  


    $subhead =  array_shift(wp_get_post_terms(get_the_ID(), 'subject', array("fields" => "names")));
    $subhead = $subhead ? $subhead : __('Uncategorized','yourtextdomain');

    if($subhead != $prev) { ?>
        <h3 class="subhead"><?php echo $subhead;?></h3>
    <?php } ?>

    <li><a href="<?php echo get_permalink();?>" title="<?php echo __('Permalink to', 'peterwade') . ' ' . get_the_title();?>"><?php the_title();?></a></li>

    <?php 

    $prev = $subhead; //cache the previous tax term

endwhile; 
echo "</ul>";
endif;
?>
1
helgatheviking