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.
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>
Get category names with permalink in single post without loop
the_category(',', '', get_the_ID())
If you want only the first one:
get_the_category(get_the_ID())[0]
Related
What is category permalink in wordpress?
get_post_meta($post->ID, '_category_permalink', true);
What is the output for this?
permalink means seo friendly url of category, for get permalink you can use below code
$category_link = get_category_link( $category_id );
The given code will output nothing because get_post_meta is useful for the meta_values associated with the post.
So to get category link, there is one function in wordpress which is get_category_link but it has required parameter category_id. So you eventually you also need category ID also to get category link.
To get category ID,you can use get_the_category and you can do something like ,
$cat_name = get_the_category($post->ID);
So it will give you an array of object(If you want to see that array object, simply do var_dump($cat_name)) and you can get category_ID from that object.
So now you will have category link which you can get by get_category_link($cat_name[0]->cat_ID).
Final code to get category link :
$cat_name = get_the_category($post->ID);
echo get_category_link($cat_name[0]->cat_ID);
NOTE : If you want link in loop, you can simply use the_category
My current setup includes having the category of each post. But whenever I mouse over the category, it shows the default message of Show all posts in [category].
I have looked around and have seen some pretty similar questions that require multiple lines of php code. This surely cannot be that complicated. Is there a way I can perhaps tweak the following to get it to do what I want?
<?php the_category(' '); ?>
This is the code that returns the above. I am looking through the codex and found category_description() and get_the_category(). I am kind of new to arrays and am having problems figuring them out. I was kind of hoping that something like this would work:
<?php the_category('title=category_description()'); ?>
but it is not. I know that is a pretty hilarious way of solving this to most of you but I am completely lost. I just want the description of the category to be the title attribute. Is there a simple way that I can do this?
wordpress codex
using get_the_category(); you can save all of the categories into a variable.
Ex:
$categories = get_the_categories();
There is however an optional parameter that you can pass; the category ID.
Ex:
$categories = get_the_categories(THE_CATEGORY_ID);
Whether you present a category ID or not you must run $categories through a loop.
Ex:
foreach($categories as $category){
}
Only then, will you be able to customize your title attribute without modifying the wordpress code.
Ex:
foreach($categories as $category){
echo ''.$category->cat_name.'';
}
So I need to list some categories, and I can list them with wp_list_categories but i also need the description for that category, i need to do something like this:
Category name
The description here
And right now I'm stuck i don't know how to do this with wordpress
This page shows how to add the Category Description: http://codex.wordpress.org/Function_Reference/category_description
It should have everything you need there, but for quick reference, the simplest way is:
<?php echo category_description(); ?>
(within the Wordpress loop)
EDIT
Just realized since you're combining this with wp_list_categories(), you'll have to create a loop that goes through the categoriy lists and displays the title and the description.
Try this Wordpress support page: http://wordpress.org/support/topic/have-wp_list_categories-output-the-category-descpription
Solved:
<?php
foreach(get_categories() as $category):
echo $category->cat_name;
echo category_description($category->cat_ID);
endforeach;
?>
I'm using custom fields on my template page and I want create a hyperlink to multiple category IDs.
This snippet gets my custom post field and returns category IDs 23 and 19, or whatever series I put in the custom field, ie: 23,9,17:
<?php $featuredpost_cat = get_field('featured_category_id'); ?>
I want to do something like this (I don't know the proper calls and syntax):
<a href="<?php wp link to $featuredpost_cat ?>"My Link Title </a>
I hope this is clear enough.
To category link by category id:
<?php
$featuredpost_cat = get_field('featured_category_id');
$category_link = get_category_link( $featuredpost_cat );
?>
My Link Title
You can get more details about this in the wordpress function reference site.
http://codex.wordpress.org/Function_Reference/get_category_link
Try this:
My Link Title
Im just giving an general solution how you can link to the id hope this helps.
So I am trying to add a "-none" to a class for a post if it is in a specific category in Wordpress. So like lets say if I am viewing a post that has a category id of 7, i want a certain class titled "example" change to "example-none".
Here's my code:
<div class="example<?= is_category('events') ?'-none':'' ?><?= in_category('7') ?'-none':'' ?>">
The weird thing with the code is, it works in a page when I am viewing all the posts in a specific category. But when I go to an interior post that is in a specific category, the code does not work.
I am using the in_category('7') tag to achieve this on a wordpress sidebar.
Any idea on what I am doing wrong?
I would remove the quotes around the id of the category:
in_category(7)
This should be a number, not a string.
Thanks. i got it working using this code:
<div class="example<? wp_reset_query(); ?><?= in_category(7) ?'-none':'' ?>">