Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [get-the-category]

get_the_category ( int $id = false ) retrieves post categories. It can be used outside The Loop by passing a post id as the parameter.

11
votes
5answers
47k views

How To Get Parent Category Slug of Current Post

My theme has styling by category using the following code, which inserts the current category's slug as a CSS class. <div class="CategorySpecificStyle <?php $category = get_the_category(); ...
2
votes
1answer
601 views

How can I get archives for specific category without category_base in the url?

I'm using this code to get archives for a specific category: function extend_date_archives_add_rewrite_rules($wp_rewrite){ $rules = array(); $structures = array( $wp_rewrite->...
0
votes
1answer
17 views

Why get_category() function is not working not working when embedded in a page?

I'm using PHP everywhere plugin to insert PHP snippets in my page. Specifically, I'm trying to get posts count in a category. Here's my code: <?php $count=get_category(607)->count; echo $...
0
votes
0answers
14 views

How to set sidebar that is already in category for single page

Experts, I'm a little newbie in the programming area, so I'm having some problems with Wordpress. It's the following, I'm using a layout in which each category has its sidebar, using a plugin to ...
0
votes
2answers
821 views

Show single Child Category on Custom Post

Am hoping someone can help me pull out a single custom category name and show it on my custom post page. I've created a new custom post type 'Itinerary' as well as some new custom categories (terms): ...
0
votes
2answers
2k views

How to fetch the highest category in the hierachry for a specific post?

I understand Wordpress concept "primary category", but I do not understand how to fetch the name of the highest category in the hierarchy for a specific post? Having this code: $category_obj = ...
2
votes
1answer
30 views

How to display recent / random posts by its category

trying to display random posts from each category that my custom post belongs to (it is related to 2 categories). I am trying to get post categories by id. I have tried to put category ID manually but ...
2
votes
1answer
18 views

Marking old posts as no longer relevant

I am working for a company with several products that will merge into a single product. They want to keep their old blog posts from the last few years but have a notice at the top to warn users that ...
-2
votes
1answer
41 views

How to insert the first letter in uppercase [closed]

short question, I have a variable concatenated with a get_the_title and get_the_category, and I need the get_the_category to show the first letter in capital letters. <?php echo get_the_title().' ...
0
votes
1answer
13 views

Get only children category name

On a single post, I display the post's categories like this: $categories = get_the_category($id); My category hierarchy is: cat1 parent of cat2, parent of cat3, etc. I would like to display only ...
0
votes
1answer
470 views

How to get tags and categories?

I used the following code to get tags from the post and then simply echo them: $posttags = get_the_tags($post->ID); if ($posttags) { foreach($posttags as $tag) { echo $tag->name . ', ';} ...
1
vote
1answer
432 views

How to display related posts from parent category

I am using the following code to display the related posts but it is showing posts for that category only like brand showing brand posts only. What I am looking for is if I select any of the posts ...
-1
votes
1answer
17 views

Print the associated categories of the current post starting with parent (with option to remove href)

Not sure why this seemingly elementary function is so difficult to perform, but I've spent a lot of time scouring the web, incorporating the suggestions and not getting the intended output. On the ...
1
vote
1answer
585 views

get_template_part based upon post's category

I have a WordPress website setup with 5 main parent categories each with dozens of child categories For each WordPress post in single.php, I'd like to load some content based upon each child category ...
0
votes
1answer
513 views

Category ID returns as '0'?

I'm trying to populate the category id from the category name within the loop. <?php $cat_name = get_theme_mod( 'cat_1' ); ?> <?php $category_id = get_cat_ID($cat_name); ?> &...
0
votes
1answer
488 views

the_category if/else statement

I need to do the following: The sense if I have a class dportfolio - show a category dportfolio. Otherwise show usual category. I use: if ( class_exists( 'DPortfolio' ) ) { echo DPortfolio::...
0
votes
1answer
31 views

Print only parent categories of post in custom RSS feed

I'm trying to print only the parent category for each post and a permalink to that category within a custom Wordpress RSS feed. Using <?php the_category_rss(); ?> prints all categories for a ...
0
votes
0answers
8 views

How to display the name of categories when multiple filtered?

In WordPress, if I hit https://example.com/category/Pop I will get the category page named "Pop". The h1 title will also display "Pop". But what about when I use multiple category filter in the ...
1
vote
1answer
1k views

how to cahnge <?php the_category() ?> template?

this function produce this html <ul class="post-categories"> <li><a href="#" rel="category tag" class="text">test</a></li> <li><a href="#" rel="category ...
2
votes
1answer
39 views

How to specify which category of the post to use in case of multiple categories

In my theme, to display the featured post I have to add an extra category "featured". Now I have articles showing featured in the url instead of the main category. Any help please? Thanls
1
vote
1answer
60 views

Is there a cleaner way to get post count for a category in category.php?

Building the category.php I'm curious to know if there is a better way to get the count of posts without wp_query? In the past I've called: single_cat_title() to render the category ...
0
votes
1answer
27 views

Style post archive blog preview based on category

0 down vote favorite I have a page showing your typical series of small post teasers - images, excerpt etc which advertise properties for lease. I would like to have the ability to add a 'new' or '...
2
votes
4answers
49k views

How to get category id of current post?

I need to get the category id of the current post outside the loop. First I get the category based on the post id: global $wp_query; $postcat = get_the_category( $wp_query->post->ID ); Now how ...
1
vote
1answer
23 views

Why is my loop not dynamically grabbing the correct Category and displaying all categorized posts?

Objective: When a category is clicked I would like the category.php page to dynamically pull in all posts filed under that clicked category. Example: News -> (Action:Click) -> Category = News ...
1
vote
2answers
10k views

Get category name from custom loop and echo it once

I have a magazine front page that updates with every issue, and the front page shows the current issue posts which are stickied. So for example, this month all the posts are in a category titlted '...
0
votes
2answers
311 views

Output the text before category

I need to output the text before the_category. In result, it should be something like Posted on September 20, 2014 IN MyCategory Here I use the_time('F j, Y') and start from here I use ...
0
votes
0answers
16 views

Find and Replace Words in Post Content For Specific category

I have tried my best it is still giving me blank page. My goal is to Find and Replace words in post content article for one or two specific category, Not the entire site. function ...
2
votes
2answers
2k views

How to get only the last child of category slug?

DESCRIPTION I want to get the name of the slug of the last child of the post's category. My categories of my posts represents cities and I am assigning them 3-4 level categories as continent, country,...
-1
votes
1answer
86 views

How to display a linked category name with get_the_category

I a code snippet with this bit included... $output .= '<li><a href=" ' . get_the_permalink() .' ">' . get_the_title() .'</a> ' . get_the_category() .' '. get_the_time('d M Y') . '&...
0
votes
1answer
1k views

How to Highlight current category on category and single page template?

On Wp Forums, I found a code that shows categories with images and it works very well. But I couldn't figure out how to highlight current category on category.php and single.php Here is the code that ...
0
votes
1answer
257 views

Get category id of next / prev post inside the_post_navigation

I would like to get_the_category inside the_post_navigation. I created a custom field on Categories to assign a hex color to each category. I would like to use this color as the next / prev link ...
0
votes
1answer
30 views

I can not call the categories of custom post type

This is my code that I used in the functions.php add_action( 'init', 'register_cpt_video' ); function register_cpt_video() { $labels = array( 'name' => __( 'Videos'...
1
vote
1answer
1k views

Get current_parent_category in a loop or Cat ancestor- big problem

Im having some trouble since a week now . .. I'm having a bad time with that ! I explain my situation: I have setup a style for post category. (a background-color and a small image background). ...
0
votes
2answers
2k views

Problem with calling custom function in a foreach loop

I have a custom function in functions.php that excludes specific category from the category list: function incomplete_cat_list() { $first_time = 1; foreach((get_the_category()) as $...
0
votes
1answer
102 views

Why is it so hard to show the URL of the current category and how to do it?

If I'm on a singular page, it is quite easy to show the current post URL outside the loop: the_permalink(); If I am on a category page it is quite simple to show the category description outside the ...
2
votes
1answer
6k views

Getting key value of WP_Term object in wordpress

How I can get from php on wordpress to a value of WP_Term object. I am using the next code to get the object: $queried_object = get_the_category( get_queried_object_id()); But this return an Array ...
0
votes
1answer
36 views

Fix Warnings that shouldn't be necessary to fix

I was trying to fix some warnings and I noticed that I get constantly PHP Notice: Undefined variable: If I use : $categories = get_the_category(get_query_var('cat')); $cat_name = $categories[0]-&...
0
votes
1answer
137 views

Adding custom post type to count in category

I'm working on building a section for related posts in my theme's single.php file and I'm querying things based on the count returned from count (returned from get_the_category($post->ID);) The ...
0
votes
1answer
63 views

Calling Category name without the link

I have looked to see if there is a simple function that only returns the name of a category on a post. I am looking to output it as a subheading for a list of posts but do not want it as a link and ...
0
votes
1answer
703 views

how to get correct category of a post in case of multiple categories?

I have a custom post type called recipes.In this I have lots of recipes as posts.All these recipes are assigned to several categories. Problem : Now Consider a post which is assigned to multiple ...
1
vote
2answers
8k views

Add classname to the_category() function?

is it possible to use add_filter or something similar to add the slug of the category as classname to the the_category() function? Right now when using the_category() the output looks like this … &...
0
votes
1answer
195 views

Insert post's category name into thumbnail string

Hopefully a simple question. I've added several post thumbnail sizes to functions.php using add_image_size. They are each named exactly like a post category. I would like to find a way of making each ...
0
votes
1answer
270 views

How to get the current category with custom posts

If I go to: domain.example/category/skydancers-en-blikvangers/ I want to get all the posts in the current category for skydancers-en-blikvangers, I know I can hard code the category but this needs to ...
1
vote
0answers
23 views

Publish Categories for a particular Author on a given Post

Suppose I have this HTML → <div class="author-category"> <ul> <li><a href="">CATEGORY 1</a></li> <li><a href="">CATEGORY 2&...
1
vote
2answers
40 views

Specified number of categoy posts not showing correctly

I have a code, i used to call a particular category in my post page footer and i have specified the number of posts to display but its showing all the posts from that category. here are the codes &...
1
vote
2answers
492 views

Five posts from a category in footer

I am trying to show 5 posts from a category in my home footer. I have placed this code <?php query_posts( 'category_name=entertainment&posts_per_page=5' ); ?> right in my home.php file ...
0
votes
1answer
73 views

list categories based on updated posts [closed]

I mean to order the of categories should be based on the updated posts. The category which has most recent post should come up first and so on. Suppose, I have 50 categories and i add new post very ...
0
votes
1answer
1k views

Display data when category is empty

I have the following code which inserts data to the bottom of a product category. It works perfectly however it only displays when a product is actually in the category, id like it to display in ...
0
votes
1answer
1k views

Display most popular posts of category

Right now in my slider I'm showing most popular post by views. However, I want to shop most popular post by views only for a certain category? The category id I want to use is "10". How would I ...
-1
votes
1answer
34 views

Why does my Wordpress index has a category?

So my homepage is set to display the latest articles of my wordpress site. I use php to detect if the page has a special category in the header of my template and it works on every other pages at the ...