-1

How can I place categories onto a page using short code from the following function.

<?php echo get_the_category_list(); ?>
1

get_the_category_list works on The Loop only and needs a post_id as a parameter. As a page does not have category, it won't work by default in a page.

If you want to retrieve all the categories on a page, you should use one of the following:

wp_list_categories

get_categories( array( 'echo' => false ) );
  • Hi @ghoul, I've tried changing the return to both of the following that you provided. the top returned the list at the top of the page - not where I placed the code. where as the other returned the text "array" – jordan Feb 8 '18 at 16:00
  • I have edited the answer. Passing echo => false will return the values where you wanted. – ghoul Feb 9 '18 at 3:44
0

To display it on a page using a shortcode you can add this to your functions.php:

function display_category_list( $atts ) {
  return get_the_category_list();
}
add_shortcode( 'display-categories', 'display_category_list' );

and then on your page simply write [display-categories].

Source: https://codex.wordpress.org/Function_Reference/add_shortcode

I suggest looking into the wp_list_categories-function since it can display a more detailed list, but I'm not sure what your goal is, so take it as a tip: https://developer.wordpress.org/reference/functions/wp_list_categories/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.