0

I am trying to get a list of my Wordpress categories to display on a Page, but haven't cracked this yet.

So far, I have been able to create a custom category page (category-categoryname.php), to display a list of Page Titles associated with a single category. The code I have used for this includes the following Loop:

while ( have_posts() ) : the_post(); ?>
<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>

However, I am now trying to display a list of Category titles on a normal WP Page, using the plugin "Post Snippets" to insert the PHP.

Q1: How do I change The Loop to display all Category titles, instead of Pages associated with a single category?

Q2: Is there anything else I need to add, like some code to functions.php etc?

Q3: Would it be easier to create a custom page template, instead of pasting into a regular page?

Note, I am not trying to filter out any categories from the list, like sub-categories or categories without associated pages/posts etc. Although filtering out 'Uncategorised' would be nice.

Thank you for reading

3
  • How do you want to display the category titles? Just a simple list?
    – s_ha_dum
    Oct 28, 2015 at 16:04
  • Note: I wrote my question on the assumption that a different type of Loop would be entered into the Snippet, but please do enlighten me if this is not necessary. Also, in case it is relevant, I am using the 'Genesis framework', with a customised 'Sample' theme.
    – Toby
    Oct 28, 2015 at 16:05
  • Answer: Yes, just a simple list for the time being. I might develop it into something more fancy in the near future.
    – Toby
    Oct 28, 2015 at 16:06

2 Answers 2

0

I believe you want wp_list_categories (). With a short set of arguments you should be able to get the results you want, including excluding "uncategorized". Something like:

$args = array(
  'exclude' => 1,
);
wp_list_categories( $args ); 
5
  • Thanks, do you know how to structure the code in the link developer.wordpress.org/reference/functions/wp_list_categories into the correct use on a snippet or custom page? ... I have just tried this: <?php /* Template Name: Show all categories */ ?> <?php wp_list_categories ( string|array $args = '' ) 'show_option_all' (string) 'orderby' (string) default is 'ID' 'order' (string) default is 'ASC' 'show_count' (bool|int) default is 0 ?> <?php genesis(); ?> ... but it has just given me a PHP fail warning on the page - line 5, which starts with: <?php wp_list_categories
    – Toby
    Oct 28, 2015 at 17:11
  • I'm not in ring of a computer. Give me an hour or so
    – s_ha_dum
    Oct 28, 2015 at 17:14
  • No probs. Am v. grateful for your help
    – Toby
    Oct 28, 2015 at 17:21
  • @Toby: See the edit
    – s_ha_dum
    Oct 28, 2015 at 19:37
  • Many thanks. This works - the snippet displays the categories in a list. I will now try to find the correct Genesis WP commands to display the rest of the page properly.
    – Toby
    Oct 29, 2015 at 15:38
0

WordPress Loop only works with posts. Categories (as in terms of category taxonomy) aren't posts and don't have concept of archive in WP.

You can build a list easily via wp_list_categories() and other related functions. For performance reason a page with page template would probably work better for it (so you are not querying number of posts just to throw them away).

2
  • Thanks, I am using the plugin "Add Categories to Pages" to enable the assigning of Pages under Categories, just like Posts. I would like to be able to add a snippet of PHP in there. I have also tried creating a custom page template. However, it just displays the categories on the top left of the entire page. I would like to be able to place it where I want, but the 3rd PHP command at the bottom seems to generate the entire WP page, so I assume I'll need to get "into" it some how: <?php /* Template Name: All categories */ ?> <?php the_category(', '); ?> <?php genesis(); ?>
    – Toby
    Oct 28, 2015 at 17:00
  • Between third party plugin and Genesis there I have no guess what is going on in your template there, sorry. :)
    – Rarst
    Oct 28, 2015 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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