Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2014
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2. // securité
  3. defined('ABSPATH') or die('Restricted Area');
  4.  
  5. // Affichage du header du thème
  6. get_header();
  7.  
  8. // Quelques variables à modifier
  9. $taxonomy = 'category';
  10. $post_type = 'post';
  11.  
  12. // nom du transient
  13. $transient_name = 'content-archive-'.$post_type.'-'.$taxonomy;
  14.  
  15. // lecture du transient
  16. $my_content = get_transient( $transient_name );
  17.  
  18. // Si on a du contenu
  19. if ( false !== $my_content ) {
  20.  
  21.     echo $my_content;
  22.  
  23. } else { // sinon on va le générer
  24.  
  25.     ob_start(); // mise en mémoire tampon
  26.     ?>
  27.     <header class="page-header">
  28.     <h1 class="container">
  29.     <?php
  30.     // récupération de l'objet du type de post
  31.     $post_type_obj = get_post_type_object( $post_type );
  32.  
  33.     // affichage de son nom
  34.     echo apply_filters( 'post_type_archive_title', $post_type_obj->labels->name, $post_type );
  35.     ?>
  36.     </h1>
  37.     </header>
  38.     <div class="content">
  39.     <div class="container">
  40.     <?php
  41.     // récupération des termes de ma taxo
  42.     $term_args = array(
  43.         'orderby' => 'name',
  44.         'order' => 'ASC'
  45.     );
  46.     $terms = get_terms( $taxonomy, $term_args );
  47.  
  48.     // si j'ai bien des termes
  49.     if ( $terms ) {
  50.  
  51.         // On boucle sur ces termes
  52.         foreach( $terms as $term ) { ?>
  53.             <div class="row">
  54.             <h1 class="term-title">
  55.             <?php
  56.             // on affiche le nom du terme
  57.             echo $term->name;
  58.             ?>
  59.             </h1>
  60.             <?php
  61.             // on replace le pointeur du tableau au début
  62.             reset( $GLOBALS['wp_query']->posts );
  63.  
  64.             // on parcours les itérations des articles de la main query
  65.             foreach( $GLOBALS['wp_query']->posts as $_post ) {
  66.  
  67.                 // si cet article contient le terme, on traite ce contenu
  68.                 if ( has_term( $term, $taxonomy, $_post ) ) {
  69.                     ?>
  70.                     <div>
  71.                     <?php
  72.                     // si il a une image à la une, ...
  73.                     if ( has_post_thumbnail( $_post->ID ) ) {
  74.                         // ... on l'affiche ...
  75.                         echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
  76.                     }
  77.                     // ... puis son lien et titre.
  78.                     ?>
  79.                     <h2><a href="<?php echo get_the_permalink( $_post->ID ) ?>" rel="bookmark" title="<?php the_title_attribute( array( 'post' => $_post ) ); ?>"><?php echo get_the_title( $_post ); ?></a></h2>
  80.                     </div>
  81.                     <?php
  82.                 }
  83.             }
  84.             ?>
  85.             </div>
  86.         <?php
  87.         }
  88.     }
  89.     ?>
  90.     </div>
  91.     </div>
  92.     <?php
  93.     // récupération du tampon
  94.     $my_content = ob_get_contents();
  95.  
  96.     // on affiche et efface le tampon
  97.     ob_end_flush();
  98.  
  99.     // on mets ce contenu dans un transient non timé
  100.     set_transient( $transient_name, $my_content );
  101. }
  102. // Affichage du footer du thème
  103. get_footer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement