-
AuthorPosts
-
October 29, 2019 at 6:05 pm #1152110
Hi,
I found the following function to display portfolio categories on a portfolio item:
function show_portfolio_category_func() {
global $post;
$the_terms = get_the_terms( $post->ID , ‘portfolio_entries’);
$output = ”;
foreach($the_terms as $term) {
$termlink = get_term_link($term->term_id, $term->taxonomy);
$output .= ‘<span class=”portfolio_cat”>‘.$term->name.’ </span>’;
}
return $output;
}
add_shortcode(‘portfolio_category’, ‘show_portfolio_category_func’);How do I modify this to ONLY show categories within the Parent Category “Habitats”?
If you look in private content and scroll to the bottom, you can see the portfolio categories listed – I like this but want to remove the categories that are not part of the Habitats parent category.
Thanks for your help as always!
October 30, 2019 at 9:48 pm #1152521Hi,
Just following up on this. Can you help?
Thanks!November 5, 2019 at 7:27 pm #1154058Hi,
Do you want to only so the category ” Habitats ” or do you want it when ever there is a parent category to show this?
Because if u want the first, give us the category id of Habitas.Best regards,
BasilisNovember 5, 2019 at 7:37 pm #1154064Hi Basilis,
Thanks!I’d like to know both answers.
In this scenario, I want to only show the parent category for “habitats” but I have another scenario where I only want to show the parent category “features”. In both situations, it’s a parent category, however.
I want to be able to use a shortcode and display the types of habitats of each portfolio item and then another shortcode to display all of the features of each portfolio item – in two different spots on the page.
The category ID for habitats is 218.
The category ID for features is 248.Thanks so much!
GabeNovember 11, 2019 at 4:48 am #1155473Hi,
Thank you for the info.
In the loop, you can add a condition, which checks for the term or category’s parent id. Look for this line:
$output .= ‘<span class=”portfolio_cat”>‘.$term->name.’ </span>’;
Replace it with:
if($term->parent === 218) { $output .= '<span class="portfolio_cat">'.$term->name.'</span>'; }
If you want to be able to define a different parent category, you can modify the shortcode and add a new parameter to it.
// https://developer.wordpress.org/reference/functions/add_shortcode/#user-contributed-notes
You can then replace the hard coded category ID with the parameter or attribute.
if($term->parent === 218) {
Replace with:
if($term->parent === $atts['id']) {
Best regards,
IsmaelNovember 11, 2019 at 4:01 pm #1155643Hi Ismael,
Since there have been several different support replies, let me tell you exactly what I need. I am using the portfolio items to describe locations for birding in Maryland. Each portfolio item has many different portfolio categories (Features – like beginners, biking, birding by car, boat launch, hiking trails, etc.; Habitats – forests & woodlands, human habitats, conifers, etc.; Regions – counties)Each portfolio item can then be in anywhere from 1 to 20 different categories.
What I’m trying to do is have a section on each portfolio item page (each birding site description page) that says “Habitats” (portfolio category ID 218) and shows which habitats (the portfolio categories that fall under the portfolio category ID 218) are represented.
I also want each portfolio item page to have a section that says “Features” (portfolio category ID 248) and shows which Features (the portfolio categories that fall under the portfolio category ID 248) are represented.
Will your solution do this for me? If so, how do do it for both Features and Habitats and what is the shortcode that I need to put on the birding site description pages?
Thanks so much for your help! I know this is a complex support request. Appreciate the help immensely!
November 12, 2019 at 6:52 am #1155841Hi,
Yes, that is exactly what the modified shortcode should do. It will only display child categories from the specified parent portfolio category.
Please add this modified snippet in the functions.php file:
function show_portfolio_category_func($atts) { $atts = shortcode_atts( array( 'id' => '', ), $atts, 'portfolio_category' ); global $post; $the_terms = get_the_terms( $post->ID , 'portfolio_entries'); $output = ''; foreach($the_terms as $term) { if($term->parent == $atts['id']) { $termlink = get_term_link($term->term_id, $term->taxonomy); $output .= '<span class="portfolio_cat"><a href='.$termlink.'>'.$term->name.' </a></span>'; } } return $output; } add_shortcode('portfolio_category', 'show_portfolio_category_func');
You can then use the following shortcode to display child categories of the “Habitat” category.
[portfolio_category id="218"]
Simply use the ID of the parent category as the value of the “id” parameter.
Best regards,
IsmaelNovember 12, 2019 at 5:53 pm #1156062Ismael,
This is ALMOST perfect. If you look at this page, https://birdersguidemddc.org/site/adkins-arboretum/, you’ll see that it’s now displaying the 4 sub-categories of portfolio_category id=”218″, but I also need it to display the sub-categories of the sub-categories. In this example, “Forests & Woodlands” is a sub-category of “Habitats”, but there are 10 or more types of “Forests & Woodlands”.Do I just add to the shortcode?
Thanks for ALL your help! You guys rock!
Gabe
November 12, 2019 at 6:18 pm #1156068Ismael,
I think I figured out a solution by using the following shortcode:
[portfolio_category id=”219″] [portfolio_category id=”237″] [portfolio_category id=”224″] [portfolio_category id=”229″]
How do I add commas between the habitats that are displayed: https://birdersguidemddc.org/site/adkins-arboretum/
Thanks!
GabeNovember 12, 2019 at 8:36 pm #1156131Hi,
Add this to quick css:
span.portfolio_cat:not(:last-child):after { content: ", "; }
Best regards,
Jordan ShannonNovember 13, 2019 at 4:32 pm #1156435Thanks! You guys rock! Thanks for ALL your help.
November 13, 2019 at 10:42 pm #1156550Hi,
No problem at all my Friend. Did you need additional help with this topic or shall we close?
Best regards,
Jordan ShannonDecember 12, 2019 at 10:45 pm #1165364This reply has been marked as private.December 15, 2019 at 4:10 pm #1166094Hi,
Sorry for the late reply, in your output above there is a space after the term and before the</a >
$output .= '<span class="portfolio_cat"><a href='.$termlink.'>'.$term->name.' </a></span>';
please try moving the space to the other side, like this:
$output .= '<span class="portfolio_cat"><a href='.$termlink.'> '.$term->name.'</a></span>';
Then clear your browser cache and any cache plugin, and check.
Best regards,
MikeDecember 16, 2019 at 7:46 pm #1166545This reply has been marked as private.December 17, 2019 at 11:32 am #1166782Hi,
Ok, that is good, now use this as your css, it will add the space after the comma:span.portfolio_cat:not(:last-child):after { content: ",\00a0" }
Best regards,
MikeDecember 17, 2019 at 4:36 pm #1166899This reply has been marked as private.December 17, 2019 at 5:25 pm #1166921Hi,
Glad to hear, the sidebar in the search results page is the default demo that shows when there is no widgets in the sidebar, so try adding a widget.
The search results page uses the default sidebar or the “Displayed Everywhere” widget area. It’s in the Appearance > Widgets panel. Make sure that the Sidebar Settings > Sidebar on Pages option is turned on.Best regards,
MikeDecember 17, 2019 at 10:27 pm #1167031This reply has been marked as private.December 18, 2019 at 12:24 pm #1167242Hi,
Sorry, I’m not sure what you mean, please see the screenshot in Private Content area of what I’m seeing in the search results, which is a title, date, comments, category breadcrumbs, & excerpt. I’m not sure which items on your site are portfolios.Best regards,
MikeDecember 18, 2019 at 3:42 pm #1167327Mike,
Here’s the screenshot to help you.
Thanks,
GabeDecember 19, 2019 at 1:38 pm #1167725Hi,
Thank you, in this search result the categories are showing and the content div is empty, this looks to be related to the function at the start of the thread. Does the search results show correctly when the function is removed?Best regards,
MikeDecember 19, 2019 at 6:03 pm #1167856This reply has been marked as private.December 20, 2019 at 1:02 pm #1168108Hi,
Yes, I understand, if it is the above function causing the error then perhaps we can rewrite it, but please test without the function so we can isolate the error.Best regards,
MikeDecember 21, 2019 at 12:10 am #1168356This reply has been marked as private.December 21, 2019 at 12:53 am #1168360Hi,
Thanks for testing, and sorry, I don’t know what I was thinking 🤦♂️
Anyways the portfolio item is built with the Advanced Layout Builder so you have to add the excerpt manually in the excerpt field at the bottom of the page for it to show in the search results.
Sorry for that, please give this a try.Best regards,
MikeDecember 21, 2019 at 8:33 pm #1168509This reply has been marked as private.December 21, 2019 at 9:54 pm #1168518Hi connect4consulting,
Glad you got it working for you! :)
If you need further assistance please let us know.
Best regards,
Victoria -
AuthorPosts
- You must be logged in to reply to this topic.