• Hi there,

    I have a function which retrieves all the terms of all the taxonomies associated to a post. But I have a basic issue with creating PHP function.

    I expect to call my function (see the code below) by passing as parameters all the variable I have defined IN the function.

    function get_tax_terms_for_the_product() {
        // Get post by post ID.
        if ( ! $post = get_post() ) {
            return '';
        }
    
        // Get post type by post.
        $post_type = $post->post_type;
    
        // Get the singular_name of the cpt
        $post_type_obj = get_post_type_object( $post_type );
    
        // Get post type taxonomies.
        $taxonomies = get_object_taxonomies( $post_type, 'objects' );
    
        foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
    
        // Get the terms related to post.
        $terms = get_the_terms( $post->ID, $taxonomy_slug );
    
        if ( ! empty( $terms ) ) {
    
                // We switch the HTML output in order to give them custom CSS classes
                switch ($taxonomy_slug) {
    
                    case "tax-plates-formes":
                        $info_plates_formes[] .= '<li class="info-plateformes">'.$term->name.'</li>';
                    break;
    
                    case "tax-categories":
                        $info_categories[] .= '<li class="info-categories">'.$term->name.'</li>';
                    break;
                }
            }
        }
    }

    I expect to call my function like this to echo the VALUE of the $variable :

    // Echo $info_categorie value (STRING)
    function get_tax_terms_for_the_product($info_categories)
    
    or
    
    // Echo $info_plates_formes value (STRING)
    function get_tax_terms_for_the_product($info_plates_formes)

    Can someone help me for this? I’m blocked for 2 days for a simple basic PHP ??

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Your problem is that your function is both generic and specific.
    You have written the function to handle all taxonomies, but want to call it to handle just one. But you didn’t make a parameter for the function.

    I would change it like this:

    function get_tax_terms_for_the_product($taxonomy_slug) {
        // Get post by post ID.
        if ( ! $post = get_post() ) {
            return '';
        }
    
        // Get the terms related to post.
        $terms = get_the_terms( $post->ID, $taxonomy_slug );
        if ( is_wp_error( $terms ) || empty( $terms ) ) {
          return '';
        }
        $names = array();
        foreach ( $terms as $term ) {
    
          $names[] = '<li class="'.$taxonomy_slug.'">'.$term->name.'</li>';
        }
    }

    And call it like this:

    $names = get_tax_terms_for_the_product('tax-categories');
    if ($names) {
      echo '<h4 class="taxonomy-label">Categories</h4><ul class="taxonomy-term-list">' . join( ' ', $names ) . '</ul>';
    }
    $names = get_tax_terms_for_the_product('tax-plates-formes');
    if ($names) {
      echo '<h4 class="taxonomy-label">Plates Formes</h4><ul class="taxonomy-term-list">' . join( ' ', $names ) . '</ul>';
    }
    Thread Starter moxymore

    (@moxymore)

    Thank you for your reply. This was the good way to proceed you have right.

    But by thinking a bit more, creating a function like this wasn’t necessary since I expected to make only one database call for all the taxonomies and terms. A function like this will make calls to the DB each time it is fired.

    I have instead created a custom php file, and I have require_once() it in my function.php, and include() it on specific single-xxx.php page. By doing this, I can grab the taxonomies_object in one DB call, and simply use the variables directly. This has another advantage : there is no need to make variable as $GLOBALS.

    Thank you anyway for your spent time, greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to pass multiple variables as parameters in PHP?’ is closed to new replies.