最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

taxonomy terms, inverted

matteradmin10PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far