admin管理员组

文章数量:1122832

I'm working on a function for bibliographic citations.

To make it more dynamic the author of the book is a custom taxonomy (autoria).

If the book only has one author, the citation begins with the last name + first name

Eintein Albert (1950)...

but if the book has two or more authors, the second author starts with the first name + last name. To change the order of the words I use explode

Einstein Albert & Paul Auster (1980)...

My code is the following, it works only partially.

 $tagx = get_the_terms($post->ID , 'autoria');
        $number = count($tagx);
       
         if ( $number == 1) {
            foreach ($tagx as $tag) {
                echo '<strong>'; echo esc_html( $tag->name ); echo '</strong>';
            }
        }

        if ( $number < 1) {
        foreach ($tagx as $tag) {
        $tag_array[] = $tag->name;

            if ($tag_array[0]) {

                echo '<strong>'; echo esc_html( $tag->name ); echo '</strong>';
            }

            if ($tag_array[1]) {

                echo '& <strong>';
                echo explode(' ', $tag->name)[3] . ' ';
                echo explode(' ', $tag->name)[2] . ' ';
                echo explode(' ', $tag->name)[1] . ' ';
                echo explode(' ', $tag->name)[0] . ' ';
                echo '</strong> ';
                
                }
            if ($tag_array[2]) {

                echo ', <strong>';
                echo explode(' ', $tag->name)[3] . ' ';
                echo explode(' ', $tag->name)[2] . ' ';
                echo explode(' ', $tag->name)[1] . ' ';
                echo explode(' ', $tag->name)[0] . ' ';
                echo '</strong> ';
                
                } 
            }
        }

The result is this. It is incorrect because the second author repeats it twice.

Einstein Albert, Auster Paul Paul Auster

What am I doing wrong in $tag_array[0]? why display two authors instead of just one?

print_r($tag_array);
Array ( [0] => Einstein Albert ) Array ( [0] => Einstein Albert [1] => Auster Paul )

I'm working on a function for bibliographic citations.

To make it more dynamic the author of the book is a custom taxonomy (autoria).

If the book only has one author, the citation begins with the last name + first name

Eintein Albert (1950)...

but if the book has two or more authors, the second author starts with the first name + last name. To change the order of the words I use explode

Einstein Albert & Paul Auster (1980)...

My code is the following, it works only partially.

 $tagx = get_the_terms($post->ID , 'autoria');
        $number = count($tagx);
       
         if ( $number == 1) {
            foreach ($tagx as $tag) {
                echo '<strong>'; echo esc_html( $tag->name ); echo '</strong>';
            }
        }

        if ( $number < 1) {
        foreach ($tagx as $tag) {
        $tag_array[] = $tag->name;

            if ($tag_array[0]) {

                echo '<strong>'; echo esc_html( $tag->name ); echo '</strong>';
            }

            if ($tag_array[1]) {

                echo '& <strong>';
                echo explode(' ', $tag->name)[3] . ' ';
                echo explode(' ', $tag->name)[2] . ' ';
                echo explode(' ', $tag->name)[1] . ' ';
                echo explode(' ', $tag->name)[0] . ' ';
                echo '</strong> ';
                
                }
            if ($tag_array[2]) {

                echo ', <strong>';
                echo explode(' ', $tag->name)[3] . ' ';
                echo explode(' ', $tag->name)[2] . ' ';
                echo explode(' ', $tag->name)[1] . ' ';
                echo explode(' ', $tag->name)[0] . ' ';
                echo '</strong> ';
                
                } 
            }
        }

The result is this. It is incorrect because the second author repeats it twice.

Einstein Albert, Auster Paul Paul Auster

What am I doing wrong in $tag_array[0]? why display two authors instead of just one?

print_r($tag_array);
Array ( [0] => Einstein Albert ) Array ( [0] => Einstein Albert [1] => Auster Paul )
Share Improve this question edited May 7, 2024 at 17:23 Oox asked May 7, 2024 at 16:58 OoxOox 535 bronze badges 1
  • 1 How are you getting $tag_array? How are you printing the second author? From what I can see from your code, you don't have a conditional branch for when there is more than one tag. You only have conditional branches for exactly one branch, $number == 1 or zero tags, $number < 1 – Wongjn Commented May 7, 2024 at 18:12
Add a comment  | 

1 Answer 1

Reset to default 0

I solved it like this. Although if there are more than 3 co-authors problems could arise

$tagx = get_the_terms($post->ID , 'autoria');
            $number = count($tagx);
           
             if ( $number == 1) {
             $first_author = $tagx[0];
                    echo '<strong>'; echo esc_html( $first_author->name ); echo '</strong>';
                
            }

            if ($number == 2) {
            $first_author = $tagx[0];
            $second_author = $tagx[1];    
                
            echo '<strong>'; echo esc_html( $first_author->name ); echo '</strong>,'; 
            echo ' <strong>';
            echo explode(' ', $second_author->name)[3] . ' ';
            echo explode(' ', $second_author->name)[2] . ' ';
            echo explode(' ', $second_author->name)[1] . ' ';
            echo explode(' ', $second_author->name)[0] . ' ';
            echo '</strong> ';
                
            }


            if ( $number > 2) {
            $first_author = $tagx[0];

            echo '<strong>'; echo esc_html( $first_author->name ); echo '</strong>, ';
            

            foreach ($tagx as $tag) {
            $tag_array[] = $tag->name;

                if ($tag_array[1]) {

                    echo ', <strong>';
                    echo explode(' ', $tag->name)[3] . ' ';
                    echo explode(' ', $tag->name)[2] . ' ';
                    echo explode(' ', $tag->name)[1] . ' ';
                    echo explode(' ', $tag->name)[0] . ' ';
                    echo '</strong> ';
                    
                    }
                if ($tag_array[2]) {

                    echo 'y <strong>';
                    echo explode(' ', $tag->name)[3] . ' ';
                    echo explode(' ', $tag->name)[2] . ' ';
                    echo explode(' ', $tag->name)[1] . ' ';
                    echo explode(' ', $tag->name)[0] . ' ';
                    echo '</strong> ';
                    
                    } 
                }
            }

本文标签: custom taxonomyArray termsif term oneif term twoetc