Afficher la liste des sous-catégories et les messages qu'elles contiennent, dans une seule catégorie principale
2 réponses
- votes
-
- 2011-09-15
La question a reçu une réponse sur un autre site ..merci! BTW,le code qui a accompli ce dontj'avaisbesoin était:
$categories = get_categories('child_of=31'); foreach ($categories as $category) { //Display the sub category information using $category values like $category->cat_name echo '<h2>'.$category->name.'</h2>'; echo '<ul>'; foreach (get_posts('cat='.$category->term_id) as $post) { setup_postdata( $post ); echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>'; } echo '</ul>'; }
Question was answered on another site.. thank you! BTW, the code that accomplished what I needed was:
$categories = get_categories('child_of=31'); foreach ($categories as $category) { //Display the sub category information using $category values like $category->cat_name echo '<h2>'.$category->name.'</h2>'; echo '<ul>'; foreach (get_posts('cat='.$category->term_id) as $post) { setup_postdata( $post ); echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>'; } echo '</ul>'; }
-
- 2011-10-30
Voici le codefinal utilisépour créer unepage depodcast àpartir de WP Posts. Au cas où quelqu'unpourraiten bénéficier.
<?php $categories = get_categories('child_of=31'); foreach ($categories as $category) { //Display the sub category information using $category values like $category->cat_name // display category image, if one exists - place image in /images/podcast_images/ dir $cat_img = ''; if(get_bloginfo("url") .'/wp-content/images/podcast_images/' . $category->slug . '.jpg' != ' ') {$cat_img = '<img class="podcast_category_image" src="'.get_bloginfo("url") .'/wp-content/images/podcast_images/' . $category->slug . '.jpg" />';} echo '<h2 class="podcast_h2">'.$cat_img.$category->name.'</h2>'; // start a list for the podcasts echo '<ul class="podcast_series">'; foreach (get_posts('orderby=post_date&category='.$category->term_id) as $post) { setup_postdata( $post ); // format date $my_date = mysql2date('F j\<\s\u\p\>S\<\/\s\u\p\>, Y', $post->post_date); // load the custom fields for this post, if they have content if(get_post_meta($post->ID, 'Speaker', true)){ $speaker_name = '<div class="speaker"><strong>Speaker: </strong>'. get_post_meta($post->ID, "Speaker", true).'</div>'; } else { $speaker_name = ''; } if(get_post_meta($post->ID, 'Scripture', true)){ $scripture = '<div class="scripture"><strong>Scripture: </strong>'. get_post_meta($post->ID, "Scripture", true).'</div>'; } else { $scripture = ''; } // echo out the results into a list item echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'. $speaker_name . $scripture.'<div class="podcast_date"> Recorded On: '. $my_date .'</div></li>'; } // close the list echo '</ul>'; } ?>
Ce codeparcourttous les articles de la catégorie 31 (dans cetexemple)et affiche les sous-catégorieset leurs articles. J'ai d'abord créé une catégoriepour PODCASTS (quiest la cat. 31)et des sous-catégories qu'elle contient,pour les séries depodcast.
Le résultatest une liste de sous-chats dansnotre chat depodcastprincipal ..et lesmessages (podcasts)pour chacun de ces sous-chats répertoriésen dessous:
Sous cat1 podcast 1 podcast 2 podcast 3
Sous cat2 podcast 1 podcast 2 podcast 3
Merci àtouspour l'aide!
Here is the final code used to create a podcast page out of WP Posts. In case anyone could benefit from it.
<?php $categories = get_categories('child_of=31'); foreach ($categories as $category) { //Display the sub category information using $category values like $category->cat_name // display category image, if one exists - place image in /images/podcast_images/ dir $cat_img = ''; if(get_bloginfo("url") .'/wp-content/images/podcast_images/' . $category->slug . '.jpg' != ' ') {$cat_img = '<img class="podcast_category_image" src="'.get_bloginfo("url") .'/wp-content/images/podcast_images/' . $category->slug . '.jpg" />';} echo '<h2 class="podcast_h2">'.$cat_img.$category->name.'</h2>'; // start a list for the podcasts echo '<ul class="podcast_series">'; foreach (get_posts('orderby=post_date&category='.$category->term_id) as $post) { setup_postdata( $post ); // format date $my_date = mysql2date('F j\<\s\u\p\>S\<\/\s\u\p\>, Y', $post->post_date); // load the custom fields for this post, if they have content if(get_post_meta($post->ID, 'Speaker', true)){ $speaker_name = '<div class="speaker"><strong>Speaker: </strong>'. get_post_meta($post->ID, "Speaker", true).'</div>'; } else { $speaker_name = ''; } if(get_post_meta($post->ID, 'Scripture', true)){ $scripture = '<div class="scripture"><strong>Scripture: </strong>'. get_post_meta($post->ID, "Scripture", true).'</div>'; } else { $scripture = ''; } // echo out the results into a list item echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'. $speaker_name . $scripture.'<div class="podcast_date"> Recorded On: '. $my_date .'</div></li>'; } // close the list echo '</ul>'; } ?>
This code will loop through all posts in Category 31 (in this example) and display the sub-categories and their posts. I first created a category for PODCASTS (which is cat. 31) and sub categories within it, for podcast series.
The result is a list of subcats within our main podcast cat.. and the posts (podcasts) for each of those subcats listed below it:
Sub cat1 podcast 1 podcast 2 podcast 3
Sub cat2 podcast 1 podcast 2 podcast 3
Thanks to all for the help!
J'aitrouvé destonnes de codeet depluginspourfaire diverses choses; àpartir demessages d'expositionpour des chats spécifiques,des sous-chats d'un chat,etc. MAIS,je nepeuxpaspour la vie demoitrouver,et jene connaispas assezbien l'API WPpouren faire ce dontj'aibesoin ..
Voici ce quej'essaie d'accomplir:
Afficher une UL detous les sous-chats dans Cat31,et lesmessagespour chacun de ces sous-chats:
SubCat1
SubCat2
SousCat3
C'est assez simple,maistoutes lesboucles quej'aiessayées échouent soit à laboucle subcat,soit à labouclepost (l'une ou l'autrefonctionne,je nepeuxpas lesfairefonctionnertoutes les deux ..)
Donc,àmoins queje netrouve unpluginpour lefaire (jepréfère coder ceci dans unfichiermodèle!) alorsje dois comprendre comment:
Bouclez les sous-chats dans Cat31touten bouclant les sous-diffusions,bouclez lespublicationspour chaque sous-chat
Toute aideesttrès appréciée!