• Resolved AnuVi

    (@anuvi)


    I have two parent-categories: books and authors. I would like to add microdata according to parent-categories.

    Something like that:

    <a rel="category tag" title="" href="" itemprop="name">Book1</a>
    <a rel="category tag" title="" href="" itemprop="author">Author</a>

    How can I achive that?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Thread Starter AnuVi

    (@anuvi)

    It’s get_the_category_list and it’s inside the loop

    <?php
                    /* translators: used between list items, there is a space after the comma */
                    $categories_list = get_the_category_list( __( ' ', 'requiredfoundation' ) );
                    if ( $categories_list ):
    
                ?>
    
                    <?php printf( __( ' %2$s', 'requiredfoundation' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
                     ?>
    
                <?php endif; // End if categories ?>
    Moderator keesiemeijer

    (@keesiemeijer)

    Try replacing that with this:

    <?php
                    /* translators: used between list items, there is a space after the comma */
                    $categories_list = my_get_the_category_list( __( ' ', 'requiredfoundation' ) );
                    if ( $categories_list ):
    
                ?>
    
                    <?php printf( __( ' %2$s', 'requiredfoundation' ), 'entry-utility-prep entry-utility-prep-cat-links', $categories_list );
                     ?>
    
                <?php endif; // End if categories ?>

    And put this in your theme’s functions.php:

    function my_get_the_category_list( $separator = ' ') {
    
    	$output = '';
    
    	$categories = get_the_category();
    
    	if ( $categories ) {
    
    		foreach( $categories as $category ) {
    
    			$itemprop = '';
    
    			// use category ID of parents 'book' and 'author'
    			if( ( $category->parent == 3 ) || ( $category->parent == 4 ) ){
    				$itemprop = ($category->parent == 3) ? ' itemprop="book"' : ' itemprop="author"';
    			}
    
    			$output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s", 'requiredfoundation' ), $category->name ) ) . '" ' . $itemprop .'>'.$category->cat_name.'</a>'.$separator;
    
    		}
    
    		return trim( $output, $separator );
    	}
    }

    And change the category IDs to that of the book and the author category IDs in:

    // use category ID of parents 'book' and 'author'
    if( ( $category->parent == 3 ) || ( $category->parent == 4 ) ){

    Thread Starter AnuVi

    (@anuvi)

    Thank you, it worked!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you’ve got it resolved 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘microdata for categories’ is closed to new replies.