Home > database >  How To Display Categories on front-page.php
How To Display Categories on front-page.php

Time:10-26

I was wondering if there was a way to loop through WordPress categories like there is for posts. I am trying to display these categories on the front-page.php file of my custom theme. Is there any way to do this? I have tried using functions like wp_list_categories() and different variations of it. However, it only returns the “Uncategorised” category and non of the others.

I was also wondering if there was a way (in this supposed loop or function to display all the different categories) to get the $term_id of the category I have uploaded custom images for each category and would like to grab those as well. Is that a possibility?

CodePudding user response:

I think your problem with displaying categories via wp_list_categories() is that by default this function does not display categories with no posts.

Try use this function with parameter "hide_empty=0"

<?php wp_list_categories('hide_empty=0');

Also u can use get_categories() function

<?php
$categories = get_categories( array(
    'hide_empty' => 0,
) );
 
foreach( $categories as $category ) {

print_r($category);

$category_link = get_category_link( $category->term_id ); 
?>

<a href="<?php echo $category_link; ?>"><?php echo $category->name; ?></a>

<?php
}
?>
  • Related