Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( !class_exists('WPSEO_Primary_Term') ) {
- $category_index = 0;
- foreach( $post->post_categories as $category ) {
- $category_index++;
- $separator = $category_index < count( $post->post_categories ) ? ', ' : '';
- echo '<a href="'. esc_url( $category['permalink'] ) .'" title="' . esc_attr( $category['label'] ) . '">' . esc_html( $category['label'] ) . '</a>' . $separator;
- }
- } else {
- // SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
- $category = $post->post_categories;
- $useCatLink = true;
- // If post has a category assigned.
- if ($category){
- $category_display = '';
- $category_link = '';
- // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
- $wpseo_primary_term = new WPSEO_Primary_Term( 'project_category', get_the_id() );
- $wpseo_primary_term = $wpseo_primary_term->get_primary_term();
- $term = get_term( $wpseo_primary_term );
- if (is_wp_error($term)) {
- // Default to first category (not Yoast) if an error is returned
- $category_display = $category[0]->name;
- $category_link = get_category_link( $category[0]->term_id );
- } else {
- // Yoast Primary category
- $category_display = $term->name;
- $category_link = get_term_link( $term->term_id );
- }
- // Display category
- if ( !empty($category_display) ){
- if ( $useCatLink == true && !empty($category_link) ){
- echo '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
- } else {
- echo '<span class="post-category">'.htmlspecialchars($category_display).'</span>';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment