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.
Related
been struggling finding a solution to my problem for weeks.
Case :
I have a custom post type named : design. This CPT have a custom field (made with ACF plugin) called thematique. I created the same custom field (thematique) for design's categories.
Expected behaviour:
I want that whenever if we make a get_posts() or WP_Query if a design's thematique field is empty, it should inherit its categorie's thematique.
I've investigated into the pre_get_posts hook but I'm not quite sure how to handle it.
Anybody has an idea ?
Thanks in advance, I really appreciate your help !
You can just do this the easy way and inside your WP Query where you have the formatting for each returned item add this:
<?php $thematique = get_field('thematique'); //Gets current posts value of fiels
<?php if (empty($thematique)){ //Checks if the field is empty, if so do the following
$postCat = get_the_category(); //Get current posts category ID
$catID = 'category_' . $postCat; //Merge category ID and needed string for ACF
$thematique = get_field('thematique', $catID); //Updated the value of $thematique with categories value
}?>
Although not tested this should indeed work as it's how ACF says to get the value from categories. Find out more here.
#Ali_k
I'm not so sure about how to go about it though. I would need something like :
// Designs Thematique priority mechanic
function design_thematique_priority($query){
if($query->query['post_type'] == "design"){
foreach($query->posts as $post){
if($post->thematique == ""){
$post->thematique = $post->category->thematique;
}
}
}
}
add_filter( 'pre_get_posts', 'design_thematique_priority' );
But I don't think there is any loop I can use to loop through posts in pre_get_posts right ?
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
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
How to count number of visitors for post under specific category ? Can I make such plugin who can do the whole magic ? I don't want plugin users to modify theme files or add code snippets in other theme files ...
something along the lines of adding to the post meta could do what you're wanting.
<?php
add_action('the_content', 'myplugincb');
function myplugincb() {
global $wp_query;
if (count($wp_query->posts) == 1) { //just do this for individual posts/pages
$pid = $wp_query->posts[0]->ID;
$key = 'myplugin_post_visit_counter';
update_post_meta($pid, $key, get_post_meta($pid, $key)+1);
}
}
function myplugin_show_viewed($post_id) {
return get_post_meta($post_id, 'myplugin_post_visit_counter');
}
You'd have to change that quite a few different ways depending on your desired result. You probably want to use something like Google Analytics if you're wanting to see specifics on pages visited and where the user came from etc.
I am trying to get the terms of a specific taxonomy to display on a page. The code is located in a page template and when I use 'get_the_terms' the result that shows is 'array'. Can someone explain this?
It's corrent. As reported in the codex page for get_the_terms() return an array of terms.
You can then use the array later in your code.
Do
<?php
$terms = get_the_terms();
print_r($terms)
?>
to discover the structure of the terms.
This example prints out the name of the terms that are retrieved by the function.
foreach ( $terms as $term ) {
echo $term->name
}
Slightly off-topic, but just yesterday I answered a question related to this over on wordpress.stackexchange.com. It uncovered some interesting variations on the theme of "getting taxonomies/terms associated with a post".