Archived topic
Display post categories above title
5 replies · Started by Luong on August 10, 2018
Hello,
How can I display the category meta on a single post above the title instead as it is below the content?
Thanks for your help!
Hi there,
Try this snippet:
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
);
}
}
}
Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know :)
Thank you a lot, it works!
No problem :)
Hi,
I used this code:
add_filter( 'generate_category_list_output','lh_remove_categories' );
function tu_remove_categories( $categories ) {
if ( is_single() ) {
return '';
}
if ( is_home() ) {
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
);
}
}
if ( is_home() ) {
$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
);
}
}
}
But my error_log_php into cpanel display shows me a lot of error:
PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'lh_remove_categories' not found or invalid function name in /home/****/public_html/wp-includes/class-wp-hook.php on line 286
What's wrong and how do I fix it?
Thank you very much
Can you open a new topic for your question?
Thanks!