Site logo

Archived topic

Remove Link from Date AND Show Category Next to It

6 replies · Started by Connor R on June 20, 2020

Viewing posts 1–7 of 7

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!

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!

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/

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!

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',
    );
} );

Glad you were able to figure out the coding part - you rock as well :)

This archived topic is closed to new replies.