• Resolved csjWP

    (@csjwp)


    Can you use the post_class(); to echo out only a specific taxonomy slug, instead of the long default list?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @csjwp,

    if you want to use the post_class you can apply a filter to it: https://developer.wordpress.org/reference/hooks/post_class/

    and remove the classes you don’t need.

    Here you can read more about filters: https://developer.wordpress.org/reference/functions/add_filter/

    You might also want to take a look at the get_the_terms function it could be a starting point to write your own post_class function. https://developer.wordpress.org/reference/functions/get_the_terms/

    • This reply was modified 8 years, 6 months ago by implenton.
    Thread Starter csjWP

    (@csjwp)

    Thanks for the help and clarification implenton

    I will try to read through it and see if I can find a simple solution I understand. Because what I what to do seems trivial, but when I start looking for documentation and trying to solve it. It gets too complicated for me very fast.

    I have a custom post type with a custom taxonomy assigned to it. And I need to somehow loop through each post and store the taxonomy(s) slug that is assigned to each post in a variable that I can echo out inside my opening div tag efter the class.

    I have been trying to string together a WP_Query loop that uses the get_terms(); But I break apart. I had managed to create the button for the filter navigation. But this last part is tripping me up.

    This is as far as I have come:
    From my archive-projects.php

    
    <!-- Add Iostope filter button list -->
    <div class="button-group filter-button-group">
    	<button data-filter="*">Alle</button>
    	<?php
    		$terms = get_terms("typologi");
    		$count = count($terms);
    		if ($count > 0) {
    			foreach ($terms as $term) {
    				echo "<button data-filter='.".$term->slug."'>" . $term->name . "</button>\n";
    			}
    		}
    	?>
    </div><!-- .button-group -->
    

    But when I turn my attention to the content-projects-index.php things get out of hand and no matter what I’ve tried.

    
    $args = array(
    	'post_type' => 'projects'
    );
    
    /* The WP_Query */
    $iso = new WP_Query( $args );
    
    if ( $iso->have_posts() ) {
    	// The Loop
    	while ( $iso->have_posts() ) {
    		$iso->the_post();
    		$terms = get_the_terms( $iso->ID, 'typologi' );
    			foreach ($terms as $term) {
    				echo '<div class="element-item . $term->slug">';
    			}
    				the_post_thumbnail();
    				the_title( sprintf( '<h3 class="item-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h3>' ); ?>
    				<h5><?php echo get_the_term_list( $post->ID, 'typologi', 'Typologi: ', '', '' ); ?></h5>
    	}
    			<?php echo '</div>'?><!-- .element-item -->
    
    }
    

    I should say that this is the HTML markup I’m aiming for:
    Isotope Selectors

    <div class="grid">
      <div class="element-item transition metal">...</div>
      <div class="element-item post-transition metal">...</div>
      <div class="element-item alkali metal">...</div>
      <div class="element-item transition metal">...</div>
      <div class="element-item lanthanoid metal inner-transition">...</div>
      <div class="element-item halogen nonmetal">...</div>
      <div class="element-item alkaline-earth metal">...</div>
      ...
    </div>
    
    • This reply was modified 8 years, 6 months ago by csjWP.
    • This reply was modified 8 years, 6 months ago by csjWP. Reason: added Isotope markup and link
    • This reply was modified 8 years, 6 months ago by csjWP.

    Hello @csjwp,

    try replacing this part from your code

    $terms = get_the_terms( $iso->ID, 'typologi' );
    foreach ($terms as $term) {
        echo <div class="element-item . $term->slug";
    }

    with this:

    // get all the typologi terms from the post
    $terms = get_the_terms( $post->ID, 'typologi' );
    
    // since get_the_terms returns WP_Term and you need only the slugs
    $terms_only_slugs = array_map( function( $term ) {
        return $term->slug;
    }, $terms );
    
    // when multiple terms set make it a string
    $element_item_classes = implode( ' ', $terms_only_slugs );
    
    // pass the string as an attribute
    echo '<div class="element-item ' . $element_item_classes . '">';
    • This reply was modified 8 years, 6 months ago by implenton.
    • This reply was modified 8 years, 6 months ago by implenton.
    • This reply was modified 8 years, 6 months ago by implenton. Reason: code modified based on @csjwp new request
    Thread Starter csjWP

    (@csjwp)

    WOW!!!

    Thanks a bunch. I can’t possible grasp how I could have missed $iso->ID for $post->ID.

    But that array_map and implode was new to me, I need to go study some more PHP. I’m not where I need to be.

    Thanks, again. You are a lifesaver.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘post_class(); usage’ is closed to new replies.