0

I have a taxonomy that will be the brand, and the children’s items the template. enter image description here

I have a code in single.php to show the details.

        <div class="box2">
        <div class="faixa">
            <div class="marca">MARCA  <div class="marca">
            <?php 


            $term_names = wp_get_post_terms($post->ID, 'marcamodelo', array('fields' => 'names', 'parent' => 0));

            if ( ! empty( $term_names ) ) {
            //  echo $term_names[0];
             var_dump($term_names);

}

            ?>

            </div></div>
            <div class="modelo">MODELO  <div class="marca">

            <?php 

            $term_names = wp_get_post_terms($post->ID, 'marcamodelo', array('fields' => 'names' ));

            if ( ! empty( $term_names ) ) {
            // echo $term_names[0];
              var_dump($term_names);

}

            ?>
            </div></div>  
        </div>

The result is this: https://imgur.com/a/VsD1xhL

But there are some that appear inverted ?! because ? enter image description here

I have described all the parts I have done below:

enter image description here

So far only the FIAT brand, and UNO model that inverts .. Maybe it’s being organized by alphabetical order or something?

2 Answers 2

0

yes it's because you sort them by name and in order ASCending. because 'orderby' => 'name', 'order' => 'ASC' is the default setting.

try: $term_names = wp_get_post_terms($post->ID, 'marcamodelo', array('fields' => 'names', 'orderby' => 'name', 'order' => 'DESC', ));

and it should be the other way around.

here you have documentation of which parameters are possible with orderby and order: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

edit: maybe you want it like this (not quite sure about the correct syntax in detail):

<div class="box2">
            <div class="faixa">
                <div class="marca">MARCA  <div class="marca">
                <?php 


                $parents = wp_get_post_terms($post->ID, 'marcamodelo', array('parent' => 0));

                if ( ! empty( $parents ) ) {
                //  echo $parents[0];
                 var_dump($parents);

    }

                ?>

                </div></div>
                <div class="modelo">MODELO  <div class="marca">

                <?php 

                $childs = wp_get_post_terms($post->ID, 'marcamodelo', array('fields' => 'names', 'parent' => $parents[0]->ID ));

                if ( ! empty( $childs ) ) {
                // echo $childs[0];
                  var_dump($childs);

    }

                ?>
                </div></div>  
            </div>
6
  • Then everything will go wrong. when my parent => 0 what is the brand, with another name? imgur.com/a/NhAwW0a Oct 23, 2018 at 13:59
  • could you explain more precisely what you want to achieve? in the first var_dump() you want have the parent and in the second var_dump() you want to see the parent with it's childs listed before? Oct 23, 2018 at 14:05
  • Yes, the parent item is the brand of the vehicle and the child item is the model of the vehicle. Oct 23, 2018 at 14:15
  • you could save your brand into a variable. in the second loop just output the childs by configuring wp_get_post_terms filter with the parent option. you will need the ID of your parent for that, like: $childs = wp_get_post_terms($post->ID, 'marcamodelo', array('fields' => 'names', 'parent' => $parentID)); Oct 23, 2018 at 14:33
  • i try all and nothing for model uno is inverse. Oct 23, 2018 at 17:53
0

I solved: $terms = get_the_terms( $post->ID, 'marcamodelo' ); if ( !empty( $terms ) ){ // get the first term // var_dump ($terms); echo $terms[0]->name; }

and

$terms = get_the_terms( $post->ID, 'marcamodelo' ); if ( !empty( $terms ) ){ // get the first term // var_dump ($terms); echo $terms[1]->name; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.