Getting taxanomy value from URL - drupal

I'm setting up a search page by views exposed filter. And one of the field are filter by taxonomy term.
For example, when I search with the taxonomy term filter field, the URL is like below.
domain.com/search?subjects=69
Now I wish to get the value of the taxonomy (it's showing tid instead of value)
<?php
$idenity = $_GET['subjects'];
print $idenity;
?>
Anyway to get the value of the taxonomy value but not taxonomy id?

You didn't specify which version you're using so I'm assuming Drupal 7 because it's the latest stable version.
You can load the term with taxonomy_term_load():
<?php
$tid = intval($_GET['subjects']);
$term = taxonomy_term_load($tid)
print $term->name;
?>
Personally, I find that the comments on the Drupal API site are usually very helpful in understanding how to use functions that sound like they are relevant to my problem.

Related

Adding custom display_type to WooCommerce categories (product_cat taxonomy)

Under a category, WooCommerce ships with a field called 'display_type'. It has 4 values:
Default
Products
Subcategories
Both
How do I add more options in there?
I found the function in the source code: add_category_fields and it doesn't look like that there is a hook, where additional fields can be added.
Had I been lazy, then I would probably just have added a new meta-field with my customly added options (using ACF).
Bonus question: For some reason, then I can't get the value of the display_type-field, unless using $wpdb. $display_type = get_term_meta( $term->ID, 'display_type' ); Doesn't seem to work. I elaborated on this question over here.

Get multiple categories names from permalink Wordpress

I am trying to get categories names for the ones that are available in permalink. e.g. I have the following permalink structure.
abc.com/product-category/category-1/category-2/
Where as category-1 and category-2 both are categories. When I try $wp_query->get_queried_object(), it just returns me info related to category-2. How can I get details of category-1 using any existing wordpress functions?
Thanks
As the previous categories are parent of the current use can get it using get_term https://developer.wordpress.org/reference/functions/get_term/
$child_category = $wp_query->get_queried_object();
$parent_category = get_term($child_category->parent, 'product-category');
You could get the parent of the category-2 using the get_term function.
$child_term = get_queried_object();
$parent_term = get_term($child_term->parent, 'product_cat'); // if your using Woocommerce then the taxonomy is called 'product_cat'
Both of these variables will be WP_Term objects.
https://developer.wordpress.org/reference/classes/wp_term/
From there you can access the name, id and any other information you need.
Revised
If you're wanting to create a custom category page (i.e. not a Woocommerce/WordPress page) then I would recommend adding a meta-box to the page editor.
https://developer.wordpress.org/reference/functions/add_meta_box/
https://www.advancedcustomfields.com/
Have a select box with your product categories.
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Then in your page template, use the term(s) ID as part of your WP_Query to pull products from both of those product categories.

ACF and Wordpress extract data from object

I have two Custom Post Types
Buildings
Apartments
They are connected thru Post Object. Apartment have field where i choose Building, and i can extract some data like ->
<?php
$ligging = get_field('project_for_apartment');
?>
<?php the_field('location_facility', $ligging->ID) ?>
With that code i extract location from Project page to apartment. That works without any error. But when i try to extract data from repeater field on project page i dont get any results. How can i extract data from fields in repeater (on another page)
Thank you a lot !
The repeater field is usually used in a loop, as its name implies. Have you looked at their doco?
http://www.advancedcustomfields.com/resources/repeater/
The return value of a repeater field (when not used in a loop) is an array, so you could do something like this:
$repeater = get_field('field_name');
foreach($repeater as $field){
echo $field['sub_field_name'];
}

getting posts from multiple categories in blog category template

I have 5 blog sections each representing a category. However, I would like to have a category called "all" that if checked would include the post in all 5 section without having to click every category.
I guess the only way to do this is to add something into the blog category template file to tell it to include the usual category posts as well as the "all" category.
now I think i've located where it queries the post:
if ( $wp_query->have_posts()) : while (have_posts()) : the_post();
but there is nothing identifying the current category id, except for a few things above for specifying the right title for the theme.
What would be the best way of achieving what I have explained?
You can interact with the wp_query object... the same way that you interact with query_posts()... it accepts the same arguments.
For either you can use Category Parameters as follows (from the wordpress codex: query_posts function reference)
Category Parameters
Show posts associated with certain categories.
cat (int) - use category id.
category_name (string) - use category
slug (NOT name).
category__and (array) - use category
id.
category__in (array) - use category
id.
category__not_in (array) - use
category id.
When it references $wp_query, the variables of the query have already been defined, you can set up your own query with any parameteres you want.

Wordpress Archive by Year "query_posts()" Problem

I created an Archive list (also this is my archive.php codes) on my blog and i want to show posts by years.
But when i use query_posts() function for excluding some categories and limit posts then it's showing all posts not by year.
For example this page is showing all posts not only posts in 2009 years.
So if i summarize this issue; i want to show archive list by year (when i enter /2009/ permalink)+exclude some categories and limit posts.
Thank you.
maybe when you use global variable, $query_string, it will help.
so you must use it like this
global $query_string;
query_posts($query_string . '&cat=-13,-4,-14,-171&posts_per_page=5&paged='. $paged);
as codex mention, here : query post,
Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().

Resources