display category name wordpress

To display a category name in WordPress, follow these steps:

  1. Identify the Category ID:
  2. Log in to your WordPress dashboard.
  3. Go to "Posts" and then click on "Categories."
  4. Hover over the category whose name you want to display and note its ID from the URL in your browser's address bar. For example, if the URL shows something like .../wp-admin/term.php?taxonomy=category&tag_ID=5, the category ID is "5."

  5. Use PHP code in your theme files:

  6. Access your WordPress theme editor under "Appearance" > "Theme Editor."
  7. Open the theme file where you want to display the category name (e.g., single.php, archive.php, etc.).
  8. Insert the following PHP code at the desired location to display the category name: php <?php $category = get_the_category(); // Retrieve the category information if (!empty($category)) { echo esc_html($category[0]->name); // Display the name of the first category } ?>
  9. Replace get_the_category() with get_the_category($post_id) if you want to get categories for a specific post (replace $post_id with the post ID).

  10. Save the changes:

  11. After inserting the PHP code, click on the "Update File" button to save the changes to your theme file.

  12. View the category name:

  13. Visit the frontend of your WordPress site where the theme file is applied to see the category name being displayed as per the code placement.

Note: Ensure to backup your theme files before making any changes, and be cautious while editing theme files as any incorrect modification could potentially cause issues with your site's functionality.