• Hi, I am using wp_list_categories to show a list of assigned custom taxonomies to my posts. the taxonomy is showed as a vertical lsit, but I want it horizontally and to look like this:
    Category1, Category2, Category3 (commas everywhere except the last one) and for a hierarchical list: Parent / Child1 / Child2 .
    Any tips?

    <?php
    global $post;
    $taxonomy = 'materials';
    
    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    
    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
    
        $term_ids = implode( ',' , $post_terms );
        $args = array(
                    'taxonomy'    => $taxonomy,
                    'include'     => $term_ids,
    				'title_li'    => '',
    				'style'       => 'none',
    				'hierarchical'       => 0,
    				'exclude_tree'       => '',
    				'echo'         => '0'
                    //Add more arguments if you need them with a different value from default
                );
    ?>
    
    Materials:
    
    <b>
      <?php
        $terms = wp_list_categories($args);
    	$terms = str_replace('<br />', '', $terms);
     // display post categories
      echo  $terms; ?>
    </b>
    <?php
    }
    ?>

    Thanks πŸ˜‰

Viewing 13 replies - 1 through 13 (of 13 total)
  • why not use get_the_categories() instead?

    I’m not sure about excluding the comma from the last one but to get a list that you can define your own markup for, you want get_terms.

    @perotin

    Hi, I’m having a little trouble to figure exactly where (in which part of page or post url) you want to display this and what you want to customize. I used a very similar code to filter the loop and it worked, after I’m a bit lost with your question. Can you give me a bit more details please?

    Personnally, I used 2 level custom taxonomies once, using custom kind of page templates (aka, taxonomy-materials.php). Because in the wp_loop, it will call specific taxonomy pages if nothing match in your query – and it simplifies a lot of things.

    Please see this image on imgur, it will help you create the files & code needed while working on taxonomies:
    http://imgur.com/lPFcZdp

    According to that image, you can like I did create custom “taxonomy pages” on the root folder of your theme, a bit like standard custom pages (but super custom really).

    Also, if you want, I can post a sample code I used (it’s close to yours, but with a bunch of addons) to make pages like taxonomy-brand.php, taxonomy-product.php so on (all cross-searchable thanks to.. taxonomies).

    It’s a quite complex issue, so feel free to share and ask,

    Thread Starter Perotin

    (@perotin)

    Thank you for your help! I found these solutions with wp_get_post_terms:

    Category1, Category2, Category3 display:

    <?php
    $terms = wp_get_post_terms( $post->ID, 'materials');
    
        $count = count( $terms );
        $i = 0;
        $term_list = '<p class="my_term-archive">';
        foreach ( $terms as $term ) {
            $i++;
        	$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
        	if ( $count != $i ) {
                $term_list .= ', ';
            }
            else {
                $term_list .= '</p>';
            }
        }
        echo $term_list;
    ?>

    and for Parent / Child1 / Child2 display:

    <?php
    $terms = wp_get_post_terms( $post->ID, 'types', array( 'orderby' => 'term_id' ) );
    
        $count = count( $terms );
        $i = 0;
        $term_list = '<p class="my_term-archive">';
        foreach ( $terms as $term ) {
            $i++;
        	$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
        	if ( $count != $i ) {
                $term_list .= ' / ';
            }
            else {
                $term_list .= '</p>';
            }
        }
        echo $term_list;
    
    ?>

    It was important to me that the terms are “clickable” and point to the archives page.

    @juggledad
    get_the_categories() will not work in taxonomy context, because with this code you must query an object, not an array

    @doublee_design
    get_terms is about the same as wp_get_post_terms, no?

    @perotin

    Hi,
    Thanks for your share, in return I put here what I did for a custom taxonomy page (this was coded to display only posts that match a taxonomy (brand) and a date range, like for limited-time deals, vouchers so on. Cross referenced to 3 taxonomies (brand, product & merchant).

    Code is far from perfect but was working rather well in terms of performance. Some terms are in french but I guess you catch it (yes = oui, no = non).

    For date range, I was using custom fields, hence php conversion to format strings to date and the opposite.

    It might help others if they want to build a full custom taxonomy page.

    <?php
    /*
    Used with filename, taxonomy-brand.php and accessed from:
    http://www.yoursite.com/brands/thebrand/
    */
    ?>
    
    <?php get_header(); ?>
    <div class="site-content">
    
      <h1 class="titre-accueil">Last deals for <?php echo $wp_query->queried_object->name; ?></h1>
    
      <ul class="liste">
      <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>    
    
          <li>
          <?php print apply_filters( 'taxonomy-images-queried-term-image', '' );?>
          <div class="colonne-droite">
          <h2 class="titre-vente"><?php the_title(); ?><span> sur <?php echo get_the_term_list( $post->ID, 'merchant' ) ?></span></h2>
          <?php the_excerpt(); ?>
    
          <?php
          $deb_date = get_post_meta( get_the_ID(), 'Begin_date', true );
          $debut_date = strtotime($deb_date);
          $exp_date = get_post_meta( get_the_ID(), 'End_date', true );
          $expiration_date = strtotime($exp_date);
          $today_date = date("d-m-Y");
          $today_date2 = strtotime($today_date);
          echo "<p>from $begin_date to $exp_date</p>";
          if( $today_date2 > $expiration_date )
          {
          $expire = "oui";
          } else {
          $expire = "non";
          }
          if( $expire == "oui" )
          {
          echo "<p class='bouton-wrapper'><span class='vente-privee-bouton termine'>Deal ended</span></p>";
          } else {
          echo "<p class='bouton-wrapper'><a class='vente-privee-bouton' href=" . get_post_meta( get_the_ID(), 'Lien', true ) . ">Check deal</a></p>";
          }
          ?>       
    
          <?php echo get_the_term_list( $post->ID, 'product', '<p>Universe : ', ', ', '</p>' ) ?>
          </div>
    
          </li>
        <?php endwhile; ?>
      <?php endif; ?>
      </ul>
    
    </div>
    
    	<?php if ( is_active_sidebar( 'taxo-brand-sidebar-1' ) ) : ?>
    		<div id="secondary" class="widget-area" role="complementary">
    
    <?php
    $terms = get_the_terms( $post->ID , 'brand' );
    if($terms) {
    	foreach( $terms as $term ) {
        echo "<p>";
    		echo $term->description.'</p><br />';
    	}
    }
    ?>
    
    			<?php dynamic_sidebar( 'taxo-brand-sidebar-1' ); ?>
    
    		</div><!-- #secondary -->
    	<?php endif; ?>
    <?php get_footer(); ?>

    Thanks for sharing that solution Perotin πŸ™‚

    @doublee_design
    get_terms is about the same as wp_get_post_terms, no?

    Yeah I think so πŸ™‚

    @doubleedesign

    I think it’s the new version of it, and wp_get_post_terms will be deprecated some day – not 100% sure though πŸ™‚

    wp_get_post_terms – Retrieve the terms for a post (needs post ID)
    get_terms – Retrieve the terms in a taxonomy or list of taxonomies. (doesn’t need post ID)

    @juggledad
    Thanks! It will simplify a lot my above code then

    Thread Starter Perotin

    (@perotin)

    and i forgot to add this thing in front of the code: global $post;.

    Thanks;)

    @perotin

    Be careful with global and code safety though, it’s not exactly recommended.

    @juggledad

    wp_get_post_terms – Retrieve the terms for a post (needs post ID)
    get_terms – Retrieve the terms in a taxonomy or list of taxonomies. (doesn’t need post ID)

    Thanks for clarifying πŸ™‚

    @digico Paris

    Be careful with global and code safety though, it’s not exactly recommended.

    How would you recommend avoiding the use of the global here?

    @doubleedesign

    A few ideas:

    I think to be perfect, you’d make custom PHP classes that can be called by private or public functions (to control data flow between global/public and private php data – and prevent data to be loaded at every single WP page).

    // Public
    public $variable;
    public function doSomething(){
        ...code...
    }
    
    // Private
    private $variable;
    private function doSomething(){
        ...code...
    }

    Another idea, use php sessions to have more or less the same result (php session with unique ID + cookie to pre-filter what data can be accessed if user is logged in or not). If I remember well, on most LAMP setups and thus WP, sessions are disabled by default, so you have to enable them at server scale to try this out (php.ini).

    Hope it helps,

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘how to style wp_list_categories’ is closed to new replies.