Highlight menu item depending on current page - php

I'm trying to highlight a menu item depending if it's on the current page. The code below is currently highlighting every menu item because it's within the foreach loop. How can I highlight the taxonomy term if it's on a certain page id and ONLY that term (not every one)?
<?php $args = array( 'taxonomy' => 'blog' );
$terms = get_terms('blog', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
echo $postid;
foreach ($terms as $term) {
$i++;
$absolute = get_bloginfo('url');
$postid = get_the_ID();
if($postid == "561") {
$term_list .= '<a class="menu-active" style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
} else {
$term_list .= '<a style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
} } ?>
<?php echo $term_list; } ?>

You will have to query the taxonomy terms belonging to the current page and compare their id-s in the foreach loop.
The get_the_ID() function returns the id of the current post, not the current taxonomy.
Here is a link to a wordpress related question which shows how you can query current taxonomy terms:
https://wordpress.stackexchange.com/questions/20431/how-to-get-taxonomy-term-of-the-current-page-and-populate-queries-in-the-templat

Related

Show the post tags with a description in Single.php (not all tags, only the tags of that post with description)

I want to show single post tags with description in single.php.
I searched about this, the closest solution is below. But this code lists all tags of the blog with a description.
$tags = get_tags( array( 'hide_empty' => false ) );
if ($tags) {
foreach ($tags as $tag) {
if ($tag->description) {
echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></dt><dd>' . $tag->description . '</dd>';
}
}
}
I need to list only the post tags with description. (It should exclude the post tags without a description.)
For example:
We have 4500+ tags in database.
200+ tags have description.
There are 7 tags on an example single post.
Only 4 of them are have a description.
Result:
I need to show only 4 tags in single.php.
You can try below code it will work in single.php
<?php
$tags = wp_get_post_tags(get_the_ID());
if ($tags) {
foreach ($tags as $tag) {
if ($tag->description) {
echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></dt><dd>' . $tag->description . '</dd>';
}
}
}
?>

Wordpress list category in custom post type

I have a query below which I want to list ONLY those categories assigned to the current post I am viewing.
At the moment, it lists ALL of the categories for my custom post type.
Is it possible to list just those for the individual post? The post type is called 'resource' and the category attached to this post type is called 'resource-category'.
<?php
$taxonomy = 'resource-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> ';
}
?>
You can use wp_get_post_terms:
<?php
$taxonomy = 'resource-category';
$tax_terms = wp_get_post_terms($post->ID, $taxonomy, array("fields" => "all"));
foreach ($tax_terms as $tax_term) {
echo '' . '<a href="' . esc_attr(get_term_link($tax_term->term_id, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> ';
}
?>

wordpress : how to add commas between meta terms on single custom post type

extendeing may last question about how to change the display of meta on single custom post type, with a great thanks to TimRDD for his helpful answer, now i have another question.
the working generating code
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}
}
}
}
?>
displays the terms in a single row but without commas between them such as :
(- Proceeding2015 - keywordsbusinessconsumerresearch).
I need your help please to put (:) after every set of terms and commas between terms to display them such as :
(- proceeding : 2015 - keywords : business, consumer, research).
You're code is ok, you just need to modify the output a bit. Try this code:
//get all taxonomies and terms for this post (uses post's post_type)
foreach ((array) get_object_taxonomies($post->post_type) as $taxonomy) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo ': (- ' . $taxonomy . ': ';// I modify the output a bit.
$res = '';
foreach ($object_terms as $term) {
$res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View all posts in %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
}
echo rtrim($res,' ,').')';// I remove the last trailing comma and space and add a ')'
}
}
Hope it works.
I've not tested this code, but I have linted it. Based on your description, this should do it.
<?php
//get all taxonomies and terms for this post (uses post's post_type)
I moved this out of the fornext.
$taxonomies = get_object_taxonomies($post->post_type);
foreach ( $taxonomies as $taxonomy ) {
I moved this into an if statement. If the assignment fails (nothing is returned) it should fail the if and skip all of this.
if ($object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'))) {
$holding = array();
foreach ($object_terms as $term) {
Instead of outputting it immediately, I am building an array.
$holding[] = '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
} // foreach ($object_terms as $term)
Here is where we do that output. I'm using the explode() function. This will output each element of the array and put a ', ' after all of them except for the last one.
echo '- '.$taxonomy . ': ' .explode(', ',$holding) . ' ';
} // if ($object_terms)
} // foreach ( $taxonomies as $taxonomy )
?>
I do hope I got it right.
Cheers!
=C=

wordpress : how to change the display of meta on single custom post type

I found code for displaying the meta terms of a custom post type.
but it shows the metas as a vertical list.
I want the code to show CPT meta in a row, instead.
any help please?
the code:
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) .
'" title="' . sprintf( __( "View all posts in %s" ), $term->name ) .
'" ' . '>' . $term->name.'</a><p> ';
}
}
}
?>
It's because of the HTML markup in your foreach loop, try removing the paragraph tags to something like:
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}

get_category_link() is returning nothing

I am using the get_categories() function to manually create myself a nav menu. I have a custom taxonomy I'm using called Category and I'm trying to return the link for it for my tags in the menu using the get_category_link() function.
foreach ($categories as $category) {
if ($category->parent == 0) { //Check to see it is a parent
$output .= '<li>';
$output .= '' . $category->name . ''; //display parent taxonomy category
}
}
But it always returns <a href="">. I can echo out the $category->cat_ID successfully so I know it is passing the ID into the function but I don't know why it's returning blank.
Am I missing something? Is it because these are custom taxonomies? They have slugs.
You need something like this for custom taxonomies:
$tax = 'cars';
$cats = get_terms( $tax, '' );
if ($cats) {
foreach($cats as $cat) {
$output .= "<li>";
$output .= '<a href="' . esc_attr(get_term_link($cat, $tax)) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a>';
$output .= "</li>";
}
}
Although you can easily add to the top of the script to get an array of all taxonomies to feed in if you wanted.

Categories