Viewing 12 posts - 1 through 12 (of 12 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

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

You must be logged in to reply to this topic.