• Hi

    I’m outputting a series of parent pages and the child page content on a custom post type archive template. I’m trying to add the custom taxonomy terms slug as a css class to the article tag but struggling to get the $term->slug to work. Feel like I’m missing something really simple. I know the terms are being set correctly because it returns a value if I do var_dump($terms);

    Here’s the part I’m struggling with, I think I’m doing something stupid within the article tag:

    foreach ( $child_pages as $child_page ) {
           $terms = get_the_terms( $child_page->ID, 'media' );
           var_dump($terms);
          echo '<article class="post col ' . $child_page->term->slug .'">';

    Thanks, any help much appreciated.

Viewing 8 replies - 1 through 8 (of 8 total)
  • What is the output of var_dump( $child_page )? Also, what is the output of var_dump( $terms )?

    Thread Starter leanda

    (@leanda)

    Thanks for replying, it’s driving me insane. Feel like I’m missing something really simple.

    The output for var_dump( $child_page ) is:

    object(WP_Post)#341 (24) {
    ["ID"]=> int(196)
    ["post_author"]=> string(1) "1"
    ["post_date"]=> string(19) "2014-05-24 14:51:20"
    ["post_date_gmt"]=> string(19) "2014-05-24 14:51:20"
    ["post_content"]=> string(0) ""
    ["post_title"]=> string(17) "Fordlandia: domus"
    ["post_excerpt"]=> string(0) ""
    ["post_status"]=> string(7) "publish"
    ["comment_status"]=> string(6) "closed"
    ["ping_status"]=> string(6) "closed"
    ["post_password"]=> string(0) ""
    ["post_name"]=> string(16) "fordlandia-domus"
    ["to_ping"]=> string(0) ""
    ["pinged"]=> string(0) ""
    ["post_modified"]=> string(19) "2014-06-25 11:20:22"
    ["post_modified_gmt"]=> string(19) "2014-06-25 11:20:22"
    ["post_content_filtered"]=> string(0) ""
    ["post_parent"]=> int(194)
    ["guid"]=> string(67) "http://www.iofpi.co.uk/civicworks.net/?post_type=archive&p=196"
    ["menu_order"]=> int(1)
    ["post_type"]=> string(7) "archive"
    ["post_mime_type"]=> string(0) ""
    ["comment_count"]=> string(1) "0"
    ["filter"]=> string(3) "raw"
    }

    And the output for var_dump( $terms ) is:

    array(1) {
    [5]=> object(stdClass)#82 (11) {
    ["term_id"]=> int(5)
    ["name"]=> string(11) "Publication"
    ["slug"]=> string(11) "publication"
    ["term_group"]=> int(0)
    ["term_taxonomy_id"]=> int(10)
    ["taxonomy"]=> string(5) "media"
    ["description"]=> string(0) ""
    ["parent"]=> int(0)
    ["count"]=> int(1)
    ["object_id"]=> int(196)
    ["filter"]=> string(3) "raw"
    }

    The full code I’m using to get the parent and sub pages is:

    <?php
    
        $parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive', 'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
        foreach ( $parent_pages as $parent_page ) {
    
        echo '<h1 class="page-heading" id="';
        echo $parent_page->post_name;
        echo '">';
    
        echo $parent_page->post_title;
        echo '</h1>';
         echo '<div class="wrapper grid4">';
    
        $all_pages = get_pages(array( 'post_type'=> 'archive',  'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
        $child_pages = get_page_children($parent_page->ID, $all_pages );
        foreach ( $child_pages as $child_page ) {
           $terms = get_the_terms( $child_page->ID, 'media' );
          echo '<article class="post col ' . $terms->slug .'">';
    
         echo '<a class="fancybox" data-fancybox-type="iframe" href="http://www.iofpi.co.uk/civicworks.net/wp-content/plugins/pdfjs-viewer-shortcode/web/viewer.php?file=http://www.iofpi.co.uk/civicworks.net/wp-content/uploads/2014/05/Citizen_Manchester.pdf" title="' . the_title_attribute('echo=0') . '" >';
    
          echo get_the_post_thumbnail( $child_page->ID, 'medium');
    
          echo '</a>';
    
            echo '<h1>';
          echo $child_page->post_title;
          echo '</h1>';
    
          echo '</article>';
        }
             echo '</div>';
          }
          ?>

    try the code below:

    echo '<article class="post col ' . $term[0]->slug .'">';

    Turns out get_the_terms returns an array with all the terms, the way you did above will display the first term in the returned array.

    Thread Starter leanda

    (@leanda)

    Hi Leo

    Thanks for replying. I tried your code and I’m afraid it still doesn’t return the term.

    Any other ideas?

    Thanks.

    Ow, a thousand apologies!

    Was missing an S in the name of the object, tests this new code please.

    echo '<article class="post col ' . $terms[0]->slug .'">';

    Thread Starter leanda

    (@leanda)

    Thanks again, sadly still doesn’t return the term πŸ™

    You need to iterate over $terms with another foreach() loop:

    foreach ( $terms as $term ) {
        echo '<article class="post col ' . $term->slug .'">';
    }
    Thread Starter leanda

    (@leanda)

    Brillant, thanks! I actually used the following because I was getting an invalid argument:

    foreach ((array) $terms as $term) {
        echo '<article class="post col ' . $term->slug .'">';
      }

    Many thanks for all your help very much appreciated.

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

The topic ‘Add WordPress custom taxonomy terms as css class’ is closed to new replies.