Going to "mysite.com/category/my_city/restaurants/" I can display all the posts that are under category "my_city" AND under category "restaurants".
Let's assume that restaurants category has several childs (thai, italian, etc): how can i make a list of all children subcategories of restaurants that are ALSO and ONLY under my_city category?
For example "mysite.com/category/my_city/thai/"
For the moment this is the code in my category.php file:
<?php
$this_cat = (get_query_var('cat')) ? get_query_var('cat') : 1;
$this_category = get_category($this_cat);
if ( $this_category->parent ) { $this_cat = $this_category->parent; }
wp_list_categories('title_li=&child_of=' . $this_cat . '');
?>
The results is a list like "thai, italian, etc" (that apparently is good), but the links are all pointing to something like "mysite.com/category/restaurants/thai/", showing ALL restaurant, not only those under my_city.
Obviously I'm not catching the other category and don't know how to do the right array.
Thanks a lot in advance for your help.
https://gist.github.com/siddhartanaranjo/6073854
in this code get the cat of the cat-page and display subcats and parent with a simple subcategory-loop
or this in your template of restaurant-page
https://gist.github.com/siddhartanaranjo/6128695
Related
I have a post type named 'Property'.
I want to show single post in two different way.
if anyone click on post then it will shows a simple layout with name of post and description.
Now i have also category for beds. now if anyone goes in category '2 Bed' then you can see all post with '2 Bed' categories('its done'). but now if anybody click on post then it have to show different single page.
my English is very bad so please excuse it.
You can set up individual templates for a single category by using the single_template hook.
Put this in your functions.php file:
function my_category_templates($single_template) {
global $post;
if ( in_category( 'property' )) {
$single_template = dirname( __FILE__ ) . '/single-property.php';
}
// Copy the above for your other categories
return $single_template;
}
add_filter( "single_template", "my_category_templates" );
You can then create individual single templates for each category, just add more conditions and point them to the template you create.
There is the concept of Category Templates as dictated by the
Template Hierarchy but because you are asking how to display a "single" post based on category, you will want to use the in_category() Conditional Tag in the Template file you use to display singe posts. The Loop article has an example of using in_category.
Or look at this concept:
http://www.nathanrice.net/blog/wordpress-single-post-templates/
Or this:
http://justintadlock.com/archives/2008/12/06/creating-single-post-templates-in-wordpress
Or this plugin:
http://wordpress.org/extend/plugins/custom-post-template/
Or this plugin:
http://guff.szub.net/2005/07/21/post-templates-by-category/
So according to the Wordpress template hierarchy there is only one single.php, that cannot be separated by category (like the archive page for example.)
https://developer.wordpress.org/themes/basics/template-hierarchy/
So in this case I suggest you read the current category id in your single.php file and then adjust the content to your needs. You can use get_the_category() do do this (reference: https://developer.wordpress.org/reference/functions/get_the_category/) which will return you an array with categories. In my simple example I just pick the first category to do something:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 1) echo 'do something here';
Thanks for you help. I am done with it myself.
I create single.php for simple format and and in category result i didn't use the_permalink and call id of post and made a url like http://localhost/demo/page?id=$id
I'm trying to use the same template (category-slug.php) to do the following:
Check if category has children - we show subcategories
otherwise - we display all posts that belong to a specific category
So what's the correct way to do so? How many templates I need for this?
You can do it all in the same template file like so:
<?php
// list child categories
$cat_id = get_query_var('cat');
$catlist = wp_list_categories('echo=0&orderby=id&title_li=&child_of=' . $cat_id);
if (get_categories('parent=' . $cat_id)) {
echo $catlist;
} else {
// normal loop
}
?>
I'm trying to develop two different templates; one layout for a category listing, and a separate layout for subcategories.
I believe it'd require a conditional statement detecting if the current category is a category or subcategory.
I'm familiar with:
<?php if (is_category('Category A')) : ?>
<p>This is the text to describe category A</p>
<?php elseif (is_category('Category B')) : ?>
<p>This is the text to describe category B</p>
<?php else : ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php endif; ?>
But I need this to be more dynamic and handle any category or subcategory without hard coding the cat ID number.
Any help would be appreciated, thank you in advance.
You can use get_category() (WordPress Codex) to get all information about a category. Using this, you could create a custom conditional function in your functions.php like this:
function is_subcategory ($catid) {
$currentcat = get_category($catid);
if ( $currentcat->parent ) {
return true;
} else {
return false;
}
}
Now you could use this function, to determine if a category has a parent category, and therefore is a subcategory.
Unfortunately I don't know the context of your posted code. You'll somehow need to get the category id. Do you use the loop the walk the categories or a custom sql-query?
If the answer is not helping, maybe you could add some surrounding code to your question.
My aim is pretty simple but couldn't find a solution anywhere : when someone clicks on one of the taxonomy terms I have created for a custom post it brings them as expected to taxonomy.php. On this taxonomy.php page I want to display all the taxonomy terms in a list and assign dynamically a class called ACTIVE to the chosen term by the user.
I know I could create pages like taxonomy-slug.php for each term but I prefer something more dynamic.
So when someone clicks on the THRILLER term from the MOVIES taxonomy I would like to have THRILLER listed with the CSS class = "active"
At the moment terms are displayed on the page but the current one never has the class assigned (first case in my code). Can someone tell me what's wrong with the code ?
Thx in advance
<?php
$termqueried = $wp_query->queried_object;
$taxo = $termqueried->name;
$terms = get_terms('MOVIES');
foreach ($terms as $term) {
if ($taxo=$terms->name) {
echo '<li class="active">'.$term->name.'</li>';
}
else { echo '<li>'.$term->name.'</li>'; }
}
?>
Are you simply not getting confused by your use of the similar words of 'terms' and 'term'. Like the following:
if ($taxo=$terms->name) {
has to be:
if ($taxo=$term->name) {
Hope this was it.
I tried getting help on the WordPress forums but no luck. Anyways, here is my question...
Lets say I am creating 10 parent categories and 2 sub categories to each parent. My WordPress post belongs to one sub category of a particular parent category
How do I get the parent category name ONLY? I don't want subcategories names? what WordPress code would do that?
And one more question...
Is it possible to query post by the parent of a sub category by using:
but instead of entering cat=1 or the name of the particular category, can I do something like:
So this way it would automatically insert and query post for the parent of any particular sub category that's clicked on?
To get the parent category name, use the get_cat_name() function, with the parent as the parameter -- like so:
$cat = get_the_category();
$parentCatName = get_cat_name($cat[0]->parent);
All these answers failed for me.
I eventually managed to display a post's topmost category name like this :
$categories = get_the_category();
$category= '';
foreach($categories as $childcat) {
$parentcat = $childcat->category_parent;
if($parentcat>0){
$category = get_cat_name($parentcat);
continue;
}
}
$category = (strlen($category)>0)? $category : $categories[0]->cat_name;
Found this answer, which gives you the first ancestor slug. It could easily be modified to give you the name.
Got it here: http://nick.boldison.com/wordpress/wordpress-get-top-level-parent-category/
<?php
// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];
$sdacReplace = array(" " => "-", "(" => "", ")" => "");
$topParent = strtolower(strtr($topParentName,$sdacReplace));
?>
In fact, to get the parent name:
// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];
Lots of answers and examples in the Wordpress docs:
get category parents
get the category
(And it looks like some code snips or other text didn't come through in your original question)