Exclude certain taxonomy from function - php

I need a help with a WordPress code.
On this page https://developer.wordpress.org/reference/functions/get_the_terms/ I got a code that returns my taxonomies and their terms.
But I'd like a certain taxonomy called "Ad Type" to be excluded from this list.
I managed to exclude some terms from "Ad Type" using this code...
// An array of IDs to ignore / exclude
$ excluded_ids = array (1, 2, 3, 4);
foreach ($ terms as $ term) {
// Only proceed if the term_id is NOT in the $ excluded_ids array
if (! in_array ($ term-> term_id, $ excluded_ids)) {
$ out. = '<li> <a href="' .get_term_link($term-> slug, $ taxonomy).' '>'. $ term-> name. '</a> </ li>';
}
}
But what I want is that the "Ad Type" taxonomy does not appear at all.
Because with the code above the Title of "Ad Type" is still appearing on the terms list retrieved.
What i want is to totally exlcude this taxonomy from being retrieved on that part of my wordpress template.
Here is the code:
function wpdocs_custom_taxonomies_terms_links() {
// Get post by post ID.
$post = get_post( $post->ID );
// Get post type by post.
$post_type = $post->post_type;
// Get post type taxonomies.
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
}
return implode( '', $out );
}
And here is how i retrieve it on my template page:
<?php echo wpdocs_custom_taxonomies_terms_links(); ?>
Is there a way to do this?
can anybody help me?
Thanks

I don't know if I am comparing it right but you can check if current taxonomy is not Ad Type only then get terms
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy_slug !== 'ad_type') :
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
endif;
}

You can exclude the desired term(s) by adding if ($term->term_id == 30) continue; as shown below, where 30 and 31 are the IDs of excluded terms.
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
//Excluded terms, change the IDs as you wish
if ($term->term_id == 30) continue;
if ($term->term_id == 31) continue;
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
}
return implode( '', $out );
}

Related

Print / Return all "results" of a function by using a shortcode in WordPress

I am trying to print all terms associated with a specific WordPress post. I have found good starting points (my approach is based on this article: https://theeventscalendar.com/knowledgebase/k/add-a-list-of-category-links-below-the-search-bar/), however, I fail at getting the result I want, which would be a single line of terms with links, seperated by commas.
e.g.:
Term 1, Term 1
What I currently have is:
add_shortcode( 'tribe_links_ineko', 'tribe_get_links_ineko' );
function tribe_get_links_ineko () {
$terms = get_terms( [
'taxonomy' => Tribe__Events__Main::TAXONOMY
] );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return;
}
echo '<div><p>';
foreach ( $terms as $single_term ) {
$url = esc_url( get_term_link( $single_term ) );
$name = esc_html( get_term_field( 'name', $single_term ) );
return "<a href='$url'>$name</a>";
}
echo'</p><div>';
};
However, this only returns one of the terms. If I use echo instead of return, it returns all of the terms, but they get printed to the top of the page (using shortcode to place the output). As far as I understand it this is expected behavior for return, however, I cannot find an explanaition as to why echo is printed in the wrong place and on how to fix this.
Maybe someone could point me in the right direction, as I have no idea of php :(
Build the string by concatenating into a single variable before returning, as opposed to returning straight away:
add_shortcode( 'tribe_links_ineko', 'tribe_get_links_ineko' );
function tribe_get_links_ineko () {
$terms = get_terms( [
'taxonomy' => Tribe__Events__Main::TAXONOMY
] );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return;
}
$out = '<div><p>';
foreach ( $terms as $single_term ) {
$url = esc_url( get_term_link( $single_term ) );
$name = esc_html( get_term_field( 'name', $single_term ) );
$out = $out . "<a href='$url'>$name</a>";
}
$out = $out . '</p><div>';
return $out;
};

Object doesnt have the same properties inside a function PHP

I've never worked with PHP before. I have the code below, which works fine.
$taxonomy = 'person';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$separator = '';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
echo $terms;
}
I tried to put this in a function so I can pass it an argument and call it.
function get_custom_tax($taxonomy) {
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$separator = '';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
echo $terms;
}
}
$taxonomy = 'person';
get_custom_tax($taxonomy);
But for some reason this doesn't work... Stack tells me im trying to get the property ID of a non-object (I guess for $post->ID)
Whats going on here?
Looks like it was a score issue.
Changing my function to pass $post outside the function did the trick
function get_custom_tax($taxonomy, $post) {
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$separator = '';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
echo $terms;
}
}
$taxonomy = 'person';
get_custom_tax($taxonomy, $post);
Your function is missing post object.. you can declare as global or pass as a params
function get_custom_tax($taxonomy) {
global $post;
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$separator = '';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
echo $terms;
}
}
$taxonomy = 'person';
get_custom_tax($taxonomy);

Get product category and tag terms as meta keyword in WooCommerce

I have been using below code for using Tags & categories as META Keywords for my wordpress posts.
function wcs_add_meta_keywords() {
global $post;
if ( is_single() ) {
$cats = get_the_category( $post->ID );
$tags = get_the_tags( $post->ID );
$keywords = '';
foreach ( $cats as $cat ) {
$keywords .= $cat->cat_name . ", ";
}
foreach ( $tags as $tag ) {
$keywords .= $tag->name . ", ";
}
echo '<meta name="keywords" content="' . $keywords . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );
and below code for using Product description as META description.
function wcs_add_meta_description_tag() {
global $post;
if ( is_single() ) {
$meta = strip_tags( $post->post_content );
$meta = strip_shortcodes( $post->post_content );
$meta = str_replace( array("\n", "\r", "\t"), ' ', $meta );
$meta = mb_substr( $meta, 0, 125, 'utf8' );
echo '<meta name="description" content="' . $meta . '" />' . "\n";
}}add_action( 'wp_head', 'wcs_add_meta_description_tag' , 2 );
But now i want to achieve the same for my products in woocommerce. I have learnt and came to know that woocommerce use taxonomies instead so i tried using get_terms() and product_tag, product_cat in place of get_the_category and get_the_tag. But it does not work.
Can anyone help with the correct usage of the variables for the both codes.
thanks in advance
For WordPress and WooCommerce term taxonomies on single post (or custom post), you can better use wp_get_post_terms(), which allows the "fields" argument to target term "names", so the code will be more compact and efficient:
For both WooCommerce and Wordpress you will use:
add_action( 'wp_head', 'wcs_add_meta_keywords' , 2 );
function wcs_add_meta_keywords() {
// For WordPress single posts with categories and tags
if ( is_single() && ! is_product() ) {
$cats = (array) wp_get_post_terms( get_the_id(), 'category', array('fields' => 'names') );
$tags = (array) wp_get_post_terms( get_the_id(), 'post_tag', array('fields' => 'names') );
}
// For WooCommerce single product (product categories and product tags)
elseif ( is_product() ) {
$cats = (array) wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'names') );
$tags = (array) wp_get_post_terms( get_the_id(), 'product_tag', array('fields' => 'names') );
}
if ( ! empty( $cats ) || ! empty( $tags ) ){
echo '<meta name="keywords" content="' . implode( ', ', array_merge( $cats, $tags ) ) . '" />' . "\n";
}
}
For WooCommerce only use:
add_action( 'wp_head', 'wcs_add_meta_keywords', 2);
function wcs_add_meta_keywords() {
if ( is_product() ) {
$product_cats = (array) wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'names') );
$product_tags = (array) wp_get_post_terms( get_the_id(), 'product_tag', array('fields' => 'names') );
}
if ( ! empty( $product_cats ) || ! empty( $product_tags ) ){
echo '<meta name="keywords" content="' . implode( ', ', array_merge( $product_cats, $product_tags ) ) . '" />' . "\n";
}
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.

Fetching Taxonomy Slug

I am using the following code to get taxonomy slug:
<?php
$terms = get_the_terms( $post->ID, 'locations' );
if ( !empty( $terms ) ){
$term = array_shift( $terms );
}
?>
I am then using the following code to output the slug:
<?php echo $term->slug; ?>
My question is, how can I use this to output two different taxonomies in the same location? For example:
<?php
$terms = get_the_terms( $post->ID, 'locations', 'status' );
if ( !empty( $terms ) ){
$term = array_shift( $terms );
}
?>
I thought I could perhaps add terms 'location' , 'status' but it doesn't work.
If you want to display two or more taxonomy then i think you should loop the $terms variable.
<?php
$terms = get_the_terms( $post->ID, 'locations' );
if ( !empty( $terms ) ){
foreach ($terms as $term):
echo $term->slug;
endforeach;
}
?>
Hope its helps you.
Thank You
According to the official documentation for get_the_terms, only one taxonomy can be supplied. If you wanted to output the slugs of all the terms within two different taxonomies, you can do as Mohammad suggested, but twice.
i.e.
<?php
// output all slugs for the locations taxonomy
$locations_terms = get_the_terms( $post->ID, 'locations' );
if ( ! empty( $locations_terms ) ) {
foreach ( $locations_terms as $term ) {
echo $term->slug;
}
}
// output all slugs for the status taxonomy
$status_terms = get_the_terms( $post->ID, 'status' );
if ( ! empty( $status_terms ) ) {
foreach ( $status_terms as $term ) {
echo $term->slug;
}
}
?>
However, if you only care to get the slug of an individual term in each of the taxonomies, you may find get_term_by more useful.
i.e.
<?php
$loc_field = 'name';
$loc_field_value = 'special location';
$loc_taxonomy = 'locations';
$locations_term = get_term_by( $loc_field, $loc_field_value, $loc_taxonomy );
echo $locations_term->slug;
$stat_field = 'name';
$stat_field_value = 'special status';
$stat_taxonomy = 'status';
$status_term = get_term_by( $stat_field, $stat_field_value, $stat_taxonomy );
echo $status_term->slug;
?>

Exclude specific categories from category list

Below is my code for the category list displayed on this page
http://www.surefiresearch.com/blog/
I want to remove the categories 'page-seo' and 'page-seo-1'. I am trying 'exclude=cat_id' which has worked for preventing the actual posts from displaying on this page, but it doesn't work for the list of categories.
Can anybody see what I am doing wrong?
Cheers.
$taxonomy = 'category';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids . '&exclude=66,67');
$terms = rtrim( trim( str_replace( '
', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
This is what I was looking for, the exclude command has worked
wp_list_categories('orderby=name&show_count=1&exclude=66,67,68,69,70,71&title_li=');

Categories