admin管理员组文章数量:1022712
I have a code that returns all the tags for the article. But that is not what I want, I only want to return the author name as the tag.
<?php the_tags() ?></span> <br />
This will return all the tags that are assigned to the article. What would i have to do so that it only returns the author name?
Default code which returns all the TAGS in a post including the author's name.
<?php
$post_id = get_the_ID();
$queried_post = get_post($post_id);
$user_info = get_userdata($post->the_author);
$first = $user_info->last_name;
$last = $user_info->last_name;
wp_set_post_tags( $post_id, $first, true );
if ( has_tag('$first-$last') ) { $author_tag = get_term_by( 'slug', '$first-$last', 'post_tag' );
echo esc_html( $author_tag->name ); }
?>
I have a code that returns all the tags for the article. But that is not what I want, I only want to return the author name as the tag.
<?php the_tags() ?></span> <br />
This will return all the tags that are assigned to the article. What would i have to do so that it only returns the author name?
Default code which returns all the TAGS in a post including the author's name.
<?php
$post_id = get_the_ID();
$queried_post = get_post($post_id);
$user_info = get_userdata($post->the_author);
$first = $user_info->last_name;
$last = $user_info->last_name;
wp_set_post_tags( $post_id, $first, true );
if ( has_tag('$first-$last') ) { $author_tag = get_term_by( 'slug', '$first-$last', 'post_tag' );
echo esc_html( $author_tag->name ); }
?>
Share
Improve this question
edited May 8, 2019 at 16:53
rocketsin6
asked May 8, 2019 at 15:22
rocketsin6rocketsin6
11 bronze badge
18
|
Show 13 more comments
1 Answer
Reset to default 0Your code with changes:
<?php
$post_id = get_the_ID();
$queried_post = get_post($post_id);
$user_info = get_userdata($post->post_author); // <-- or: $user_info = get_the_author_meta('ID')
$first = $user_info->first_name; // what if it is empty or contains forbidden characters?
$last = $user_info->last_name; // as obove
wp_set_post_tags( $post_id, $first, true ); // <-- tag slug, used in the code below
// --- display tag ---
if ( has_tag("$first") )
{
$author_tag = get_term_by( 'slug', "$first", 'post_tag' );
if ( $author_tag instanceof WP_term )
{
$a_name = $author_tag->name;
$a_link = get_term_link( $author_tag->term_id );
// display tag
echo '<a href="' . esc_url( $a_link ) . '" rel="tag">' . $a_name . '</a> ';
}
}
?>
Personally, I would move the code adding the tag to action hook save_post_{post_type}
and execute when $update
parameter is FALSE (a new post was created).
add_action( 'save_post_post', 'se337414_add_author_tag', 20, 3 );
function se337414_add_author_tag( $post_id, $post, $update )
{
// create tag only when post is created
if ( $update == true )
return;
$user_info = get_userdata( $post->post_author );
$tag_parts = [];
if ( !empty($user_info->first_name) )
$tag_parts[] = $user_info->first_name;
if ( !empty($user_info->last_name) )
$tag_parts[] = $user_info->last_name;
$tag_slug = implode( '-', $tag_parts );
if ( empty($tag_slug) )
return;
wp_set_post_tags( $post_id, $tag_slug, true );
}
I have a code that returns all the tags for the article. But that is not what I want, I only want to return the author name as the tag.
<?php the_tags() ?></span> <br />
This will return all the tags that are assigned to the article. What would i have to do so that it only returns the author name?
Default code which returns all the TAGS in a post including the author's name.
<?php
$post_id = get_the_ID();
$queried_post = get_post($post_id);
$user_info = get_userdata($post->the_author);
$first = $user_info->last_name;
$last = $user_info->last_name;
wp_set_post_tags( $post_id, $first, true );
if ( has_tag('$first-$last') ) { $author_tag = get_term_by( 'slug', '$first-$last', 'post_tag' );
echo esc_html( $author_tag->name ); }
?>
I have a code that returns all the tags for the article. But that is not what I want, I only want to return the author name as the tag.
<?php the_tags() ?></span> <br />
This will return all the tags that are assigned to the article. What would i have to do so that it only returns the author name?
Default code which returns all the TAGS in a post including the author's name.
<?php
$post_id = get_the_ID();
$queried_post = get_post($post_id);
$user_info = get_userdata($post->the_author);
$first = $user_info->last_name;
$last = $user_info->last_name;
wp_set_post_tags( $post_id, $first, true );
if ( has_tag('$first-$last') ) { $author_tag = get_term_by( 'slug', '$first-$last', 'post_tag' );
echo esc_html( $author_tag->name ); }
?>
Share
Improve this question
edited May 8, 2019 at 16:53
rocketsin6
asked May 8, 2019 at 15:22
rocketsin6rocketsin6
11 bronze badge
18
-
Are you actually setting up the author as a separate tag? WP automatically stores an author for each post, which you can access with
the_author()
. – WebElaine Commented May 8, 2019 at 15:31 - You should use get_the_author() function. – nmr Commented May 8, 2019 at 15:31
- I have author's name as tag (separate tag). It is linking to /tag/AuthorName. where would i insert the_author() in my code? – rocketsin6 Commented May 8, 2019 at 15:33
- the_author would return their name but it would link to /author/AuthorName which is not what I want. – rocketsin6 Commented May 8, 2019 at 15:35
- Can you somehow distinguish the author's tag from the others, e.g. after the prefix? – nmr Commented May 8, 2019 at 15:43
1 Answer
Reset to default 0Your code with changes:
<?php
$post_id = get_the_ID();
$queried_post = get_post($post_id);
$user_info = get_userdata($post->post_author); // <-- or: $user_info = get_the_author_meta('ID')
$first = $user_info->first_name; // what if it is empty or contains forbidden characters?
$last = $user_info->last_name; // as obove
wp_set_post_tags( $post_id, $first, true ); // <-- tag slug, used in the code below
// --- display tag ---
if ( has_tag("$first") )
{
$author_tag = get_term_by( 'slug', "$first", 'post_tag' );
if ( $author_tag instanceof WP_term )
{
$a_name = $author_tag->name;
$a_link = get_term_link( $author_tag->term_id );
// display tag
echo '<a href="' . esc_url( $a_link ) . '" rel="tag">' . $a_name . '</a> ';
}
}
?>
Personally, I would move the code adding the tag to action hook save_post_{post_type}
and execute when $update
parameter is FALSE (a new post was created).
add_action( 'save_post_post', 'se337414_add_author_tag', 20, 3 );
function se337414_add_author_tag( $post_id, $post, $update )
{
// create tag only when post is created
if ( $update == true )
return;
$user_info = get_userdata( $post->post_author );
$tag_parts = [];
if ( !empty($user_info->first_name) )
$tag_parts[] = $user_info->first_name;
if ( !empty($user_info->last_name) )
$tag_parts[] = $user_info->last_name;
$tag_slug = implode( '-', $tag_parts );
if ( empty($tag_slug) )
return;
wp_set_post_tags( $post_id, $tag_slug, true );
}
本文标签: phpshow limited tags in an article
版权声明:本文标题:php - show limited tags in an article 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745509526a2153762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_author()
. – WebElaine Commented May 8, 2019 at 15:31