[Resolved] Category above title for blog page and single posts

Home Forums Support Category above title for blog page and single posts

Viewing 15 posts - 1 through 15 (of 23 total)
  • Author
    Posts
  • #1296297
    Matthew

    I saw this topic to add blog category above title, for the blog page only.

    https://generatepress.com/forums/topic/blog-category-above-title/

    It works but how can it also,

    1) It doesn’t work for my CPT custom_post_type_pictures. The category still below the excerpt (blog page)
    2) Do same for the single post pages i.e. categories above the title (regular & CPT).

    add_filter( 'generate_category_list_output','tu_remove_categories' );
    function tu_remove_categories( $categories ) {
    	if ( is_home() || is_archive() ) {
    		return '';
    	}
    	
    	return $categories;
    }
    
    add_action( 'generate_before_content','tu_cats_above_title' );
    function tu_cats_above_title() {
    	if ( is_home() || is_archive() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
    }

    I tried

    if ( is_home() || is_archive() || is_single() ) {

    Which only made the categories disappear.

    Is it better to add a separate filter or function for the single post pages so I could treat that snippet on it’s own i.e. modify or deactivate it without affecting the blog page? Or would the above snippet be modified

    Thanks,

    #1296960
    Tom
    Lead Developer
    Lead Developer

    Hey Matthew,

    So are basically trying to take what we did here and move them to the top of the post?: https://generatepress.com/forums/topic/entry-meta-not-displaying-for-a-cpt/#post-1284466

    Let me know 🙂

    #1297599
    Matthew

    Hi Tom,

    Yes that’s right, if that last snippet you linked to is active my blog page shows the categories as normal after the excerpt:

    https://drive.google.com/open?id=16KAeaI9trL3L19dARZW5PEUGvPs5acWK

    But as you can see the tags (blue) are also there on a CPT post but shouldn’t because in settings Layout > Blog > Archives ‘Display post tags’ are set to off. On the regular non-CPT post that follows i.e. “Test post 1” which also does have tags assigned they are not displaying on the archive page (which is correct). So my CPT needs to follow the regular post behavior in that regard.

    With the snippet I placed earlier activated as well, on the the blog page categories are placed above the title but my CPT misses out:

    https://drive.google.com/open?id=1wFDwBPJgSPab0cOu8JuM7QKBhyu5pu7b

    And for the single posts of both regular and CPT, the category above the title there too, would be nice (tags will stay at the bottom of the post).

    Can the default folder and tag font icons be removed as well? Or I could have a go at that afterwards if it’s not too hard.

    thanks,

    #1298328
    Tom
    Lead Developer
    Lead Developer

    Best to leave the snippet you included in your original post here off the site – it’ll just confuse things for now.

    So without that function (and with my functions from the other post), does everything work as it should? The only thing that needs to be done is to move the categories above the title?

    #1298780
    Matthew

    Agreed, we’ll exclude that newer function and stick with the other winner you coded.

    Yes everything works – except for the problem I mentioned in the last post which is tags are showing on the blog page where they shouldn’t be according to my customizer settings. Pls see the first screenshot I added. Tags on single posts only.

    So just to move categories above the title for both the blog page and single posts then, including for my cpt.

    Thanks,

    #1299609
    Tom
    Lead Developer
    Lead Developer

    Ah, having it above the header complicates things a bit, so we might need to go with the function you mentioned in this topic.

    For example:

    add_filter( 'generate_category_list_output','tu_remove_categories' );
    function tu_remove_categories( $categories ) {
    	if ( is_home() || is_archive() || is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
    		return '';
    	}
    	
    	return $categories;
    }
    
    add_action( 'generate_before_content','tu_cats_above_title' );
    function tu_cats_above_title() {
    	if ( is_home() || is_archive() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
    
            if ( is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
                $genres_list = get_the_term_list( get_the_ID(), 'genres', '', ', ' );
    
                if ( $genres_list ) {
                    printf(
                        '<span class="tags-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
                        esc_html_x( 'Genres', 'Used before tag names.', 'generatepress' ),
                        $genres_list,
                        apply_filters( 'generate_inside_post_meta_item_output', '', 'genres' )
                    );
                }
    
                $words_list = get_the_term_list( get_the_ID(), 'words', '', ', ' );
    
                if ( $words_list ) {
                    printf(
                        '<span class="tags-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
                        esc_html_x( 'Words', 'Used before tag names.', 'generatepress' ),
                        $words_list,
                        apply_filters( 'generate_inside_post_meta_item_output', '', 'words' )
                    );
                }
            }
    }

    Is that closer to the desired result?

    #1299877
    Matthew

    It’s getting it there, I deleted the following from your function (as I don’t want tags at the top) so now I am using just:

    add_filter( 'generate_category_list_output','tu_remove_categories' );
    function tu_remove_categories( $categories ) {
    	if ( is_home() || is_archive() || is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
    		return '';
    	}
    	
    	return $categories;
    }
    
    add_action( 'generate_before_content','tu_cats_above_title' );
    function tu_cats_above_title() {
    	if ( is_home() || is_archive() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
    
            if ( is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
                $genres_list = get_the_term_list( get_the_ID(), 'genres', '', ', ' );
    
                if ( $genres_list ) {
                    printf(
                        '<span class="tags-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
                        esc_html_x( 'Genres', 'Used before tag names.', 'generatepress' ),
                        $genres_list,
                        apply_filters( 'generate_inside_post_meta_item_output', '', 'genres' )
                    );
                }
            }
    }

    Here is what is happening.

    ON SINGLE POSTS:

    CPT: categories are at the top now

    CPT: although categories are at the top, they are still at the bottom. The ones at the bottom use the cat-links with my CSS, and the ones at the top use the default link style. I want to style this so can we use cat-links for the top ones?

    Regular posts: categories are still at the bottom

    ON BLOG PAGE:

    Regular posts: categories are at the top now

    CPT: categories are still at the bottom

    CPT: tags are still showing (I have them set to no display in customizer)

    #1300827
    Tom
    Lead Developer
    Lead Developer

    Hmm, any chance you can link me to these pages so I can take a closer look?

    #1301383
    Matthew

    Sure, I sent you the login through the account issue form.

    Also ignore my query about the cat-links style, as the code just had a minor error that used tag-links etc. But the duplicate categories are still at the bottom.

    thanks,

    #1302437
    Tom
    Lead Developer
    Lead Developer

    Can you make sure post categories are turned off for archives and single posts in the Customizer? They look like they’re enabled right now.

    Let me know 🙂

    #1303188
    Matthew

    I’ve turned them off, not sure why as I want categories on(?)

    #1303825
    Tom
    Lead Developer
    Lead Developer

    Looks like they’re off now.

    I think your CPT Post meta code snippet is why it’s still showing at the bottom of the post. If you disable that snippet, it should go away.

    #1304302
    Matthew

    Disabling CPT Post meta worked for removing the duplicate categories from the bottom of the custom post type but it also removed tags so I modified that snippet so it references tags only and re-activated it.

    To summary what I have now is,

    Snippet CPT Post meta
    Displays the tags on archives and single posts of my CPT
    This has control in the customizer for archives i.e. check “Display post tags” to show and hide tags, but not for single posts of CPT (the tags are always shown regardless)

    add_action( 'generate_post_meta_items', function( $item ) {
        if ( 'words' === $item ) {
            $words_list = get_the_term_list( get_the_ID(), 'words', '', ', ' );
    
            if ( $words_list ) {
                printf(
                    '<span class="tags-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
                    esc_html_x( 'Words', 'Used before tag names.', 'generatepress' ),
                    $words_list,
                    apply_filters( 'generate_inside_post_meta_item_output', '', 'words' )
                );
            }
        }
    } );
    
    add_filter( 'generate_footer_entry_meta_items', function( $items ) {
        if ( 'pictures' === get_post_type() ) {
            return array(
                'genres',
                'words',
                'comments-link',
            );
        }
    
        return $items;
    } );
    
    add_filter( 'generate_footer_meta_post_types', function( $types ) {
        $types[] = 'pictures';
    
        return $types;
    } );

    Snippet Categories above title (this is an unusual combination)
    Displays the categories above the title on single posts of my CPT
    Displays the categories above the title on archives of regular posts

    add_filter( 'generate_category_list_output','tu_remove_categories' );
    function tu_remove_categories( $categories ) {
    	if ( is_home() || is_archive() || is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
    		return '';
    	}
    	
    	return $categories;
    }
    
    add_action( 'generate_before_content','tu_cats_above_title' );
    function tu_cats_above_title() {
    	if ( is_home() || is_archive() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
    
            if ( is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
                $genres_list = get_the_term_list( get_the_ID(), 'genres', '', ', ' );
    
                if ( $genres_list ) {
                    printf(
                        '<span class="cat-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
                        esc_html_x( 'Genres', 'Used before cat names.', 'generatepress' ),
                        $genres_list,
                        apply_filters( 'generate_inside_post_meta_item_output', '', 'genres' )
                    );
                }
            }
    }

    Snippet Categories above title (2)
    Displays the categories above the title on single posts of regular (non CPT) posts

    add_filter( 'generate_category_list_output','lh_remove_categories' );
    function lh_remove_categories( $categories ) {
    	if ( is_single() ) {
    		return '';
    	}
    	
    	return $categories;
    }
    
    add_action( 'generate_before_entry_title','lh_single_cats_above_title' );
    function lh_single_cats_above_title() {
    	if ( is_single() ) {
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) );
    		if ( $categories_list ) {
    			printf( '<span class="entry-meta cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'generatepress' ),
    				$categories_list
    			);
    		}
    	}
    }

    What’s still not working is the categories above the title on archives of my CPT.

    Thanks,

    #1305304
    Tom
    Lead Developer
    Lead Developer

    This part of the code should be added Genres above the title on your CPT archives:

    if ( is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
        $genres_list = get_the_term_list( get_the_ID(), 'genres', '', ', ' );
    
        if ( $genres_list ) {
            printf(
                '<span class="cat-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
                esc_html_x( 'Genres', 'Used before cat names.', 'generatepress' ),
                $genres_list,
                apply_filters( 'generate_inside_post_meta_item_output', '', 'genres' )
            );
        }
    }

    So it’s checking to see if we’re on the pictures CPT. If we are, it will display a list of genres if there are any.

    Is that the correct taxonomy?

    #1305771
    Matthew

    Right, so you’re saying that this code which is in the snippet Categories above title:

    if ( is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {
    	$genres_list = get_the_term_list( get_the_ID(), 'genres', '', ', ' );
    
    	if ( $genres_list ) {
    		printf(
    			'<span class="cat-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span>',
    			esc_html_x( 'Genres', 'Used before cat names.', 'generatepress' ),
    			$genres_list,
    			apply_filters( 'generate_inside_post_meta_item_output', '', 'genres' )
    		);
    	}
    }

    Should be working?

    The custom taxonomy I have registered is custom_taxonomy_genres i.e. the category of my CPT.

    I looked over the code closely, cleared browser and Litespeed cache again but it’s still not showing categories above title on the archive for the CPT.

    This part should be doing it?

    if ( is_post_type_archive( 'pictures' ) || is_singular( 'pictures' ) ) {

    Which is its version of what is used for the regular posts in that snippet, i.e

    if ( is_home() || is_archive() ) {

    and which is working for the regular posts.

    Can’t see why it wouldn’t be working if you reckon it should. It is placing the categories above title for the single posts of the CPT, just not the archive.

Viewing 15 posts - 1 through 15 (of 23 total)
  • You must be logged in to reply to this topic.