[php] Get Wordpress Category from Single Post

I'm finishing up a WP theme, and I'm on the single.php template. I'm having some issues because I need to access the parent category that a post is in in order to display certain images and XML content.

Here is an example of what I'm talking about. The following is the end url of a single post:

/andrew/leaf-art-2/

/andrew/ is the category, and leaf-art-2 is the single post. When I am on the single post, I am having trouble getting single_cat_title(); to return the category that the current post is in. I am using single_cat_title(); instead of the_category(); because it displays the string value of the category which I then use to place a picture of the artist (whose category this is) on their posts. I don't have any use for the url, I just need the string with the category name.

Any good ways of doing this? I have been searching the Wordpress Codex and lots of forums, and haven't found any answers yet.


The following was my original post.

I have set up a category called "artists" which when I run single_cat_title("", false); I can get the string value of the category and then use it to search for the appropriate artist image using XML.

This works fine on the category.php template page.

The problem is that when I'm actually inside of a single post that has the "artists" category, single_cat_title(); doesn't output any information to the page, thereby keeping me from accessing the XML data.

I need to, while in the "artists" > "sample" post, be able to get from WP the category.

P.S. the above category is one of many that is using this setup, which is why I can't hardcode it.

This question is related to php wordpress themes wordpress-theming

The answer is


How about get_the_category?

You can then do

$category = get_the_category();
$firstCategory = $category[0]->cat_name;

For the lazy and the learning, to put it into your theme, Rfvgyhn's full code

<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name; echo $firstCategory;?>

<div class="post_category">
        <?php $category = get_the_category();
             $allcategory = get_the_category(); 
        foreach ($allcategory as $category) {
        ?>
           <a class="btn"><?php echo $category->cat_name;; ?></a>
        <?php 
        }
        ?>
 </div>

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to wordpress

#1273 – Unknown collation: ‘utf8mb4_unicode_520_ci’ How to get WooCommerce order details Wordpress plugin install: Could not create directory WooCommerce: Finding the products in database How to get post slug from post in WordPress? How to get featured image of a product in woocommerce Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\wordpress\wp-includes\class-http.php on line 1610 Use .htaccess to redirect HTTP to HTTPs Load More Posts Ajax Button in WordPress How to decode encrypted wordpress admin password?

Examples related to themes

Change the Theme in Jupyter Notebook? How to change the Spyder editor background to dark? How to edit default dark theme for Visual Studio Code? Why do Sublime Text 3 Themes not affect the sidebar? How do you set the title color for the new Toolbar? How to set editor theme in IntelliJ Idea How to change background color in the Notepad++ text editor? How to change app default theme to a different app theme? Apply a theme to an activity in Android? MySQL Workbench Dark Theme

Examples related to wordpress-theming

The requested URL /about was not found on this server Get current category ID of the active page Redirect after Login on WordPress WordPress path url in js script file How can I get the current page name in WordPress? Get Wordpress Category from Single Post