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
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
I have a page named "Shop". On that page I display a custom menu, which contains links to categories like "Books", "Shoes" etc. Whenever I click one of those links/categories it takes me to a relevant category page, for example /category/books/.
I do not want it to redirect me to a category page, I want it to display posts on the same page ("Shop") that the menu item was clicked on. The only problem I have encountered trying to accomplish this, is the fact that I do not know how to change the custom menu behavior. I don't want it to redirect me, but instead send a GET value to the same page ("Shop"). Then the shop page would take that GET value and display relevant posts.
I know how to display posts from a category etc. I just don't know how to change the behavior of the custom menu.
Could anybody help me? I would be grateful.
Original Answer:
Use wp_get_nav_menu_items:
$menu_slug = 'YOUR_MENU_SLUG';
$menu_id = get_nav_menu_locations()[$menu_slug];
$menu_items = wp_get_nav_menu_items($menu_id);
foreach($menu_items as $item){
if($item->object == 'category'){
print('<p>Title: ' . $item->title . '<br>ID: ' . $item->object_id . '</p>');
}
}
wp_get_nav_menu_items returns an array of menu items. You get several information on the item, like type (post, category, ...), id, title and so on.
See http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items for a full list of properties.
Hope this helps :)
Update:
Assuming your permalink setting is set to default, the following code will print a modified menu that uses GET.
$menu = wp_nav_menu(array(
'theme_location'=>'YOUR_MENU_LOCATION',
'echo'=>0,
));
$new_url = $_SERVER['SCRIPT_NAME'] . '?shop_page=$1';
$menu = preg_replace('/href="[^"]*cat=(.+)"/', 'href="'.$new_url.'"', $menu);
print($menu);
The regex may not be the best as it ignores other GET values and I'm not experienced on them. If your permalink setting is different and every time you change it, you will have to edit the regex.
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.