Viewing 28 posts - 1 through 28 (of 28 total)
  • Author
    Posts
  • #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!

    #1152521

    Hi,
    Just following up on this. Can you help?
    Thanks!

    #1154058

    Hi,

    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,
    Basilis

    #1154064

    Hi 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!
    Gabe

    #1155473

    Hi,

    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,
    Ismael

    #1155643

    Hi 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!

    #1155841

    Hi,

    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,
    Ismael

    #1156062

    Ismael,
    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

    #1156068

    Ismael,
    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!
    Gabe

    #1156131

    Hi,

    Add this to quick css:

    span.portfolio_cat:not(:last-child):after {
      content: ", ";
    }

    Best regards,
    Jordan Shannon

    #1156435

    Thanks! You guys rock! Thanks for ALL your help.

    #1156550

    Hi,

    No problem at all my Friend. Did you need additional help with this topic or shall we close?

    Best regards,
    Jordan Shannon

    #1165364
    This reply has been marked as private.
    #1166094

    Hi,
    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,
    Mike

    #1166545
    This reply has been marked as private.
    #1166782

    Hi,
    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,
    Mike

    #1166899
    This reply has been marked as private.
    #1166921

    Hi,
    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,
    Mike

    #1167031
    This reply has been marked as private.
    #1167242

    Hi,
    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,
    Mike

    #1167327

    Mike,
    Here’s the screenshot to help you.
    Thanks,
    Gabe

    #1167725

    Hi,
    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,
    Mike

    #1167856
    This reply has been marked as private.
    #1168108

    Hi,
    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,
    Mike

    #1168356
    This reply has been marked as private.
    #1168360

    Hi,
    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.
    2019-12-20-175042
    2019-12-20-175130
    Sorry for that, please give this a try.

    Best regards,
    Mike

    #1168509
    This reply has been marked as private.
    #1168518

    Hi connect4consulting,

    Glad you got it working for you! :)

    If you need further assistance please let us know.

    Best regards,
    Victoria

Viewing 28 posts - 1 through 28 (of 28 total)

You must be logged in to reply to this topic.