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 Answer
Reset to default 0I 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
版权声明:本文标题:custom taxonomy - Array terms, if term one, if term two, etc 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307875a1933462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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