Question

well, this code fetches the category list and display it in the theme in which the post is assigned. I want to add nofollow tag to this out list. I surfed the net and couldn't find a solution. The only solution I found was to modify the wordpress core files. But I don't want to modify the core files.

<footer class="entry-meta">
        <?php
            /* translators: used between list items, there is a space after the comma */
            $category_list = get_the_category_list( __( ', ', 'basically' ) );



            $meta_text = __( 'Category: %1$s', 'basically' );

            printf(
                $meta_text,
                $category_list,
                get_permalink(),
                the_title_attribute( 'echo=0' )
            );
        ?>

Any other way?

Was it helpful?

Solution

<?php

foreach( (get_the_category() ) as $category ) {
    $category_link[] = '<a href="' . get_category_link( $category->cat_ID ) . '"'
                     . ' title="' . $category->cat_name . '" rel="nofollow">'
                     . $category->cat_name . '</a>';
}

printf( __( 'Category: %1$s', 'basically' ), implode( ', ', $category_link ) );

?>

It will work inside the_loop(), to use it outside the loop you have to provide the post id on get_the_category() so it should be get_the_category( $post->ID ).

OTHER TIPS

Try this too:

<footer class="entry-meta">
        <?php
            /* translators: used between list items, there is a space after the comma */
            $cat_list = get_the_category_list( __( ', ', 'basically' ) );
            $category_list = str_replace('rel="category tag"','rel="category tag nofollow"',$cat_list);    

            $meta_text = __( 'Category: %1$s', 'basically' );

            printf(
                $meta_text,
                $category_list,
                get_permalink(),
                the_title_attribute( 'echo=0' )
            );
        ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top