admin管理员组

文章数量:1130349

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

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:

But there are some that appear inverted ?! because ?

I have described all the parts I have done below:

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

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

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:

But there are some that appear inverted ?! because ?

I have described all the parts I have done below:

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

Share Improve this question edited Oct 23, 2018 at 14:00 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Oct 23, 2018 at 13:41 Glauco MarquesGlauco Marques 1
Add a comment  | 

2 Answers 2

Reset to default 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/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>

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; }

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

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:

But there are some that appear inverted ?! because ?

I have described all the parts I have done below:

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

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

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:

But there are some that appear inverted ?! because ?

I have described all the parts I have done below:

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

Share Improve this question edited Oct 23, 2018 at 14:00 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Oct 23, 2018 at 13:41 Glauco MarquesGlauco Marques 1
Add a comment  | 

2 Answers 2

Reset to default 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/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>

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; }

本文标签: taxonomy termsinverted