Category permalink in wordpress - php

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

Related

How to detect the slug of the Category page i'm on? (WooCommerce)

I keep finding tutorials on listing all the slugs on the internet but can't seem a function or line of code that can echo/return me the slug of the category i'm on so I can save it on a variable.
Does anyone know what it is?
I need to be able to use it in archive-product.php
Thanks in advance
The category slug should be available in global variable $wp_query.
Try this in yourtheme/woocommerce/archive-product.php:
global $wp_query;
$cat_slug = $wp_query->query_vars['product_cat'];
echo $cat_slug;
The other method with get_the_category() may work for normal posts in archive.php but I had no success with it in archive-product.php.
$slugs = array();
foreach( (get_the_category()) as $category ) {
array_push( $slugs, $category->slug );
}
var_dump( $slugs );
(untested)
http://codex.wordpress.org/Function_Reference/get_the_category
Edit:
Tested and works correctly. Try adding to archive.php (temporarily) and make sure your url is something like: mysite.com/category/uncategorized/ . I suspect you may not be calling the correct template file or perhaps not actually browsing any categories.

Adding a title attribute to category without affecting core

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.'';
}

Call Wordpress Archive page with multiple category IDs

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.

wp_list_categories add class

So what I need to do, is when I list wp_list_categories I need to check an ID with each category ID listed on wp_list_categories and if that statement is true add a class.Something like this:
if($foo = $category_id) echo '<li class="active">Category name</li>'; Is this possible?
//Short brief
I need something like this because I list the categories in the sidebar and also I display the first post from that category, so I need to mark that category as active when I do this and I do all of this in a page template.
I've solved it by passign an argument current_category

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.
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]

Categories