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(); ...
4
votes
1answer
184 views

How to add classes to images based on their categories?

I need to be able to: 1.) assign categories to images and 2.) add classes to all images that correspond to their assigned categories. I was able to add categories to images with the following: ...
4
votes
2answers
2k views

Display recent posts from the same category as current post in sidebar

I'm using a modified version of Twenty Twelve and I'm trying to display recent posts from the same category as the current post (except the current post) in the sidebar. I started this way but ...
3
votes
1answer
295 views

Change role after the user has read all the posts in a category

I'm creating a platform with "X" categories and "X" roles with the same name to each category. I would like to know how to change the user's role after they have read all the posts in a category. ...
2
votes
4answers
50k 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 ...
2
votes
3answers
5k views

Exclude one category from get_the_term_list

I want to exclude one of the categories from my posts, which will always be the same, named "documentaires" and with the ID 20. Here is the code : <?php echo get_the_term_list( get_the_ID(), '...
2
votes
1answer
11k views

Test if post has a category

I need to display some HTML only if the post being displayed has a category - not a specific category, just any category at all. I tried if (!is_empty(get_the_category($post->ID))), but for some ...
2
votes
1answer
8k views

Get the number of posts in a named category, outside the loop

I've managed to do this from a single.php file, inside the loop, but now I need to do it from another page. I need to find out how many live posts are in category named every story (slug is ...
2
votes
1answer
2k views

Add body class of category parent

I am not a coder, but I usually get by with Wordpress by doing my research and find my solution. I can't find what I need to do this time so I have attempted to crib together some code - what I am ...
2
votes
1answer
417 views

Get a variable from object in array [closed]

I'm trying to echo get_the_category($id)[0]->slug and this works on localhost but not on the server. The error is Parse error: syntax error, unexpected '['.... Is there a workaround?
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
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 ...
2
votes
1answer
40 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
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
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,...
2
votes
1answer
192 views

Get the category is not working on url

Although this is my first post but i have been coming here for answers so I know the rule and this isn't a duplicate because i have tried whatever i could but couldn't seem to get my codes to work. ...
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->...
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 … &...
1
vote
2answers
1k views

Why is get_the_category() saying that I have two categories?

My post has only one category. The following code: $categories = get_the_category(); gives me this result: Array ( [0] => stdClass Object ( [term_id] => 22 ...
1
vote
1answer
143 views

Get posts with at least one category in common with current post?

In single.php : I want to get all posts with at least one category in common with the current post. I tried reading through the wordpress codex documentation but I found it rather confusing (I'm ...
1
vote
2answers
1k views

How to add a rel attribute to images that contains their categories?

I've added categories to images, so that I can use them to filter images within a portfolio page. Now I'm thinking I need to add the rel attribute to each image that contains its assigned categories. ...
1
vote
3answers
3k views

Add Class While on Current Post; wp_list_categories

The code below produces a sub-menu of child categories of the currently active parent category. This code also produces the child categories of the top parent category while viewing child categories ...
1
vote
1answer
555 views

Is it possible to change any of the HTML/URL returned from the_category()

I've found this is on of my theme files: <?php the_category( ' ' ) ?> Ideally I'd like to wrap the whole thing in a function and replace some of the URL's being output, but I don't seem to be ...
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 '...
1
vote
1answer
12k views

Display Categories or Subcategories with name & description in category.php

I have the following issue for my customer. Can some suggest a possible solution to this issue? This website has Categories & Subcategories Cat1 -Subcat1 -Subcat2 -Subcat3 Cat2 -Subcat1 -...
1
vote
1answer
308 views

Insert <ul> Into Sub-child Menu

This code displays a list of child categories of the parent category while a user is on a child category or single post. The styling around this list is displaying even when a list isn't being ...
1
vote
4answers
2k views

Display the deepest child category from Category X (in loop)

I have a number of top-level categories including "News" and "Sport". They each have multiple levels of child categories as shown below. News Australia NSW Sydney Sports Cycling Road Cycling ...
1
vote
1answer
63 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 ...
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
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
494 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 ...
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 ...
1
vote
1answer
1k views

JSON API plugin, Get posts by category return no post

Im trying to get the posts by category. using this URL: /api/core/get_category_posts/?slug=cat2 But responding this: {"status":"ok","count":0,"pages":0,"category":{"id":3,"slug":"cat2","title":"...
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). ...
1
vote
2answers
989 views

Apply different Class for each element in a foreach()

sorry for the silly question, but I am really lost. I want to apply the following CSS to the even elements of my foreach. CSS .blogArticles a { float: left; max-width: 250px; width: 25%; ...
1
vote
1answer
341 views

Display post X of Y in category

I am trying to display a post number (X) on each single.php, based on what number that post is in it's category, followed by the total number of posts (Y) in the category. So if there is 10 posts in ...
1
vote
1answer
671 views

get_the_category_list open in parent window

I'm opening my posts in an iframe, and I'm using get_the_category_list to list post's categories. The problem is that when a user clicks on the category link, it opens in the iframe. How can I set the ...
1
vote
1answer
2k views

Using wp_list_categories like the the_category (showing just current categories of a post)

fromi'm using wp_list_categories to get an unordered list with headlines (on single.php inside the post-loop): <ul><?php wp_list_categories('title_li=0');?></ul> the thing is: i ...
1
vote
1answer
751 views

Get only one of the current categories

On post pages Im using the below code to display links to other posts from the current category. My problem is when the current post is in more than 1 category it will display posts from the first ...
1
vote
1answer
589 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 ...
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
1answer
435 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
vote
1answer
781 views

How to get custom posts sub category link

I'm trying to echo custom posts sub category link as a view more option.For the last week, i am trying to do that, but i just can't find the right way. I've tried, category links, terms, with args, id....
1
vote
0answers
329 views

List only child categories within posts

I am using the_category() to display categories in my posts that are also filed in child-categories. Unfortunately this also displays the parent category which i would love to hide. Is there a way to ...
0
votes
3answers
144 views

Need help “sanitizing” a custom function that pulls category slug into body class

Not sure if "sanitizing" is really the correct phrase to use for this but it was the best one I could think of...anyways, here is the problem: My function works great like so... // add category id ...
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 ...
0
votes
1answer
118 views

Related content based on category name

I'd like to modify the code below so it grabs the data based on the category the post is in and 'only' that category, not children categories, not other categories, only the category of the current ...
0
votes
2answers
5k views

Display post category in foreach loop

I am trying to display post category in a foreach loop. I have all the elements displaying properly except for the category. Here is my function: function my_get_display_author_posts() { global $...
0
votes
2answers
4k views

get_the_category listing in hierarchial order

Hello at the moment I'm wondering how I would be able to make this work to post in Hierarchical order within the post loop. <?php foreach ((get_the_category()) as $childcat) { ...
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]-&...