[Resolved] Remove Link from Date AND Show Category Next to It

Home Forums Support Remove Link from Date AND Show Category Next to It

  • This topic has 6 replies, 2 voices, and was last updated 3 months ago by Leo.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1335855
    Connor R

    Hi there!

    I am trying to remove the link from the date and then move the category label next to it. I have written a code snippet as follows:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    	printf( '<span class="posted-on">%s</span>',
    		$time_string
    	);
    }, 10, 2 );
    add_filter( 'generate_post_date_output', function( $date ) {
        if ( is_singular() ) {
            return $date;
        }
    
        echo $date;
    
        $categories_list = get_the_category_list( ', ' );
    
        if ( $categories_list ) {
            echo ' | <span class="categories">' . $categories_list . '</span>';
        }
    } );

    However, this causes the date and category to show up twice on my blog page (i.e. “June 20, 2020 | Investing June 20, 2020 | Investing).

    Any help fixing this would be greatly appreciated! Thank you!

    #1335874
    Connor R

    I seem to have resolved this (by accident) so it correctly displays on my blog category page. However, I’d like to do the same on my post page. Currently, my post page only shows the date (and the category is still at the bottom).

    Thanks!

    #1336451
    Leo
    Staff
    Customer Support

    Hi there,

    You can only use the one filter once. So remove this snippet:

    add_filter( 'generate_post_date_output', function( $date ) {
        if ( is_singular() ) {
            return $date;
        }
    
        echo $date;
    
        $categories_list = get_the_category_list( ', ' );
    
        if ( $categories_list ) {
            echo ' | <span class="categories">' . $categories_list . '</span>';
        }
    } );

    Then use this these to rearrange instead:
    https://docs.generatepress.com/article/generate_header_entry_meta_items/
    https://docs.generatepress.com/article/generate_footer_entry_meta_items/

    #1336561
    Connor R

    Hi Leo,

    I removed the code snipped you suggested and then added the PHP you mentioned:

    add_filter( ‘generate_header_entry_meta_items’, function() {
    return array(
    ‘categories’,
    ‘date’,
    ‘author’,
    );
    } );

    However, this creates two issues:

    1. It duplicates the category at the top and bottom of the post/blog page.
    2. How do I style this (like I had in my old PHP) so that it appears as “Date | Category?”

    Thanks!

    #1336685
    Leo
    Staff
    Customer Support

    1. You’d need this filter to remove the duplicate in the footer:
    https://docs.generatepress.com/article/generate_footer_entry_meta_items/

    2. Try this filter:
    https://docs.generatepress.com/article/generate_inside_post_meta_item_output/

    Let me know if you need help with the code 🙂

    #1336697
    Connor R

    Leo,

    You rock! This worked:

    add_filter( 'generate_footer_entry_meta_items', function() {
        return array(
        );
    } );
    add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
    	if ( 'categories' === $item ) {
            return ' | ';
        }
        return $output;
    }, 50, 2 );
    add_filter( 'generate_header_entry_meta_items', function() {
        return array(
            'date',
    		'categories',
        );
    } );
    #1336702
    Leo
    Staff
    Customer Support

    Glad you were able to figure out the coding part – you rock as well 🙂

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