Hi @a4jpcom. This is the best I can come up with:
global $wp_query;
foreach ( $wp_query->tax_query->queries as $query ) {
$category_slug = $query['terms'][0];
$category = get_category_by_path( $category_slug );
echo esc_html( $category->name );
}
Thread Starter
a4jp
(@a4jpcom)
@noisysocks that is beautiful code. Thank you so much. I couldn’t find anything like this and had been trying lots of things. I just put in a small space between between the categories to make it easier to read.
<h1><?php global $wp_query;
foreach ( $wp_query->tax_query->queries as $query ) {
$category_slug = $query['terms'][0];
$category = get_category_by_path( $category_slug );
echo " ". esc_html( $category->name );
} ?> (<?php echo ($wp_query->found_posts); ?>)</h1>
If I want to show a plus sign if there are more than two, is there an easy way to do that?
echo " + ". esc_html( $category->name ); puts a plus in front of everything.
Try this:
<h1>
<?php
global $wp_query;
if ( count( $wp_query->tax_query->queries ) > 2 ) {
echo "+";
}
foreach ( array_slice( $wp_query->tax_query->queries, 0, 2 ) as $query ) {
$category_slug = $query['terms'][0];
$category = get_category_by_path( $category_slug );
echo " ". esc_html( $category->name );
}
?>
(<?php echo ($wp_query->found_posts); ?>)
</h1>
Thread Starter
a4jp
(@a4jpcom)
I changed the code a bit again and just need to remove the last + mark when 2 categories show up as the code only added the plus mark before or after both categories.
I switched > 2 to >= 2 so they show up if there are 2 or more categories.
I don’t know if this was the best solution but if I can remove the last plus mark it works. I think I could have just echoed the plus marks in all situations then removed them. Any solution is okay though.
<h1>
<?php
global $wp_query;
foreach ( array_slice( $wp_query->tax_query->queries, 0, 2 ) as $query ) {
$category_slug = $query['terms'][0];
$category = get_category_by_path( $category_slug );
echo esc_html( $category->name )." ";
/*echo count( $wp_query->tax_query->queries); //counts number of catogries*/
if ( count( $wp_query->tax_query->queries ) >= 2) {
echo " + ";
};
/* something to trim off the last + mark ltrim($str, '+');*/
}
echo " (". $wp_query->found_posts . ") ";
?>
</h1>
Thread Starter
a4jp
(@a4jpcom)
I changed the code a bit again and just need to remove the last + mark when 2 or more categories show up.
The array_slice limited the function so I removed it.
The code would also just add a plus mark before or after both categories.
I switched > 2 to >= 2 so they show up if there are 2 or more categories.
I don’t know if this was the best solution but if I can remove the last plus mark it works. I think I could have just echoed the plus marks in all situations then removed them. Any solution is okay though.
<h1>
<?php
global $wp_query;
foreach ( $wp_query->tax_query->queries as $query ) {
$category_slug = $query['terms'][0];
$category = get_category_by_path( $category_slug );
echo esc_html( $category->name )." ";
/*echo count( $wp_query->tax_query->queries); //counts number of catogries*/
if ( count( $wp_query->tax_query->queries ) >= 2) {
echo " + ";
};
/* something to trim off the last + mark ltrim($str, '+');*/
}
echo " (". $wp_query->found_posts . ") ";
?>
</h1>
Maybe I should also have the categories become links if there are 2 or more categories. It is so much better now than the original code I was using.
You can see it working here: Test Category Page
If it works, it works! š
If your question has been answered, we would love if you would mark this topic as resolved in the sidebar. This helps our volunteers find the topics that still need attention and more people will get helped, possibly like you did.
Probably not worth changing if it works but for future reference, using implode() works a treat to avoid the dangling + problem (or whatever joining characters you use)
https://www.php.net/manual/en/function.implode.php
You do need to start with an array of only the labels you want output. An array of term objects would be too much data. If you can’t get such an array out of the query, you can still construct the needed array using a foreach loop as you’ve already done.
Thread Starter
a4jp
(@a4jpcom)
@bcworkz and @noisysocks it almost works. Just now the code adds a plus mark after every category, if there are 2 or more categories.
Super sorry about the title: How do I echo one or more than one category from a URL on the category.php page?
Maybe I should have written: How do I echo one or more than one category, from a URL on the category.php page, including the plus mark from the URL when multiple categories are shown?
I just thought as I wrote how do I echo 1 or more categories from the URL that it would include the plus mark.
If I use implode() will that cause problems with links later on? I will add links by myself to categories when multiples show up one day.
I set a class for the plus marks and now just need to find out how to do display none on the class ".plus" . $wp_query->tax_query->queries
I think
Here is the updated code with incremented classes and IDs if that helps:
<?php
global $wp_query;
$counter = 0;
$howmany = count( $wp_query->tax_query->queries );//number of categories
foreach ( $wp_query->tax_query->queries as $query ) {
$counter++;
$category_slug = $query['terms'][0];
$category = get_category_by_path( $category_slug );
echo esc_html( $category->name )." ";
if ( count( $wp_query->tax_query->queries ) >= 2) {
echo " <span id='plus".$counter."' class='plus".$counter."' style='display:inline'>+</span> ";
};
/*echo '<script>
document.getElementById("plus"'.$howmany.').classList.remove("plus"'.$howmany.');
</script>';*/
if ( $howmany >= 2) {
echo '<script>const box = document.getElementById("plus'.$howmany.'");
.plus'.$howmany.'.style.setProperty("display", "none");
</script>';
};
}
echo " (". $wp_query->found_posts . ") ";
?>
I’m sorry, I’m not grasping what you’re after. Pluses in an URL are used to pass multiple elements in a single query string value. Once the URL is parsed they are discarded and no longer available in subsequent code. The terms in the query are what you have to work with, but you can do anything you like with them.
Regarding the use of implode(), it simply concatenates an array of string elements together using a passed “glue” arg between each string element. Assuming each array element is a simple string, implode() doesn’t otherwise care what they are, they can be any string values you want. If there’s any problem later on, it’d be due to what strings you used, not the use of implode() itself.