WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] microdata for categories (6 posts)

  1. AnuVi
    Member
    Posted 1 year ago #

    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?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

  3. AnuVi
    Member
    Posted 1 year ago #

    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 ?>
  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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 ) ){
  5. AnuVi
    Member
    Posted 1 year ago #

    Thank you, it worked!

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags