All in single.php file in Genesis child theme.
I have an echo
of divs
and in between I in one of the anchors
I am trying to insert author's post url via a variable
, like so:
function my_function() {
$author = get_the_author_meta( $post->post_author );
$author_link = get_author_posts_url($author);
$author_avatar = get_avatar_url(get_the_author_meta( $post->post_author ));
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
//..
echo '<div>
// ..
<div class="">
By <a href="'.$author_link.'">Author</a>
</div>'
</div>
}
genesis();
I have tried:
the_author_posts_url();
,
get_the_author_meta('user_url');
,
get_author_posts_url();
, get_author_posts_url( get_the_author_meta( 'ID' ) );
every time the link is either empty or it generates
http://localhost/author
without outputting the specific author.
full code of single.php is here:
function custom_entry_content() { $author = get_the_author_meta( $post->post_author );
$author_link = get_author_posts_url($author);
$author_avatar = get_avatar_url(get_the_author_meta( $post->post_author ));
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<div style="margin-bottom: 50px;position: relative; text-align:center; width:100%;background-image: url('.$featured_image[0].');
height: 502px; background-size: cover;
background-repeat: no-repeat;">
<div class="post-title-box">
<h1 class="the-title">
'. get_the_title(). '</h1>
</div>
<div class="post-auth-info">
<div class="vertical-middle" style="display:inline-block">
<div class="">
By <a href="'.$author_link.'">Author</a>
</div>
<time class="mk-publish-date" datetime="2017-11-01">
<a href="#">Published ' . time_elapsed_string(get_the_date()). '</a>
</time>
</div>
<div style="display:inline-block">
<a href="'.$author_link.'">
<div>
<img alt="" src="'.$author_avatar.'" class="img-circle avatar avatar-55 " height="55" width="55" style="height:55px;width:55px">
</div>
</a>
</div>
</div>
</div>';
}
// Removes Published by and time data from before the post content area
remove_action( 'genesis_entry_header', 'genesis_post_info', 12);
add_filter( 'genesis_attr_site-inner', 'remove_top_padding');
function remove_top_padding( $attributes ) {
$attributes['class'] = 'container box nopadding';
return $attributes;
}
// Adds left padding to content
add_filter( 'genesis_attr_content', 'padding_left');
function padding_left( $attributes ) {
if ( 'full-width-content' === genesis_site_layout() )
$attributes['class'] = '';
else
$attributes['class'] = 'col-md-8 single-post-entry';
return $attributes;
}
genesis();
Many thanks in advance!
All in single.php file in Genesis child theme.
I have an echo
of divs
and in between I in one of the anchors
I am trying to insert author's post url via a variable
, like so:
function my_function() {
$author = get_the_author_meta( $post->post_author );
$author_link = get_author_posts_url($author);
$author_avatar = get_avatar_url(get_the_author_meta( $post->post_author ));
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
//..
echo '<div>
// ..
<div class="">
By <a href="'.$author_link.'">Author</a>
</div>'
</div>
}
genesis();
I have tried:
the_author_posts_url();
,
get_the_author_meta('user_url');
,
get_author_posts_url();
, get_author_posts_url( get_the_author_meta( 'ID' ) );
every time the link is either empty or it generates
http://localhost/author
without outputting the specific author.
full code of single.php is here:
function custom_entry_content() { $author = get_the_author_meta( $post->post_author );
$author_link = get_author_posts_url($author);
$author_avatar = get_avatar_url(get_the_author_meta( $post->post_author ));
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<div style="margin-bottom: 50px;position: relative; text-align:center; width:100%;background-image: url('.$featured_image[0].');
height: 502px; background-size: cover;
background-repeat: no-repeat;">
<div class="post-title-box">
<h1 class="the-title">
'. get_the_title(). '</h1>
</div>
<div class="post-auth-info">
<div class="vertical-middle" style="display:inline-block">
<div class="">
By <a href="'.$author_link.'">Author</a>
</div>
<time class="mk-publish-date" datetime="2017-11-01">
<a href="#">Published ' . time_elapsed_string(get_the_date()). '</a>
</time>
</div>
<div style="display:inline-block">
<a href="'.$author_link.'">
<div>
<img alt="" src="'.$author_avatar.'" class="img-circle avatar avatar-55 " height="55" width="55" style="height:55px;width:55px">
</div>
</a>
</div>
</div>
</div>';
}
// Removes Published by and time data from before the post content area
remove_action( 'genesis_entry_header', 'genesis_post_info', 12);
add_filter( 'genesis_attr_site-inner', 'remove_top_padding');
function remove_top_padding( $attributes ) {
$attributes['class'] = 'container box nopadding';
return $attributes;
}
// Adds left padding to content
add_filter( 'genesis_attr_content', 'padding_left');
function padding_left( $attributes ) {
if ( 'full-width-content' === genesis_site_layout() )
$attributes['class'] = '';
else
$attributes['class'] = 'col-md-8 single-post-entry';
return $attributes;
}
genesis();
Many thanks in advance!
Share Improve this question edited Dec 14, 2017 at 2:44 Scott Agirs asked Dec 13, 2017 at 17:00 Scott AgirsScott Agirs 1131 silver badge6 bronze badges 5 |2 Answers
Reset to default 0get_the_author_meta()
get_author_posts_url()
get_avatar_url()
You are using all three of these functions incorrectly. Please always check the documentation first. get_the_author_meta()
does not require any arguments, but you can supply the field you wish to be returned. get_author_posts_url()
requires the user_id. get_author_posts_url()
and this one is a bit more flexible and will take a user_id or various Objects.
The code sample below should work...
$author_id = get_the_author_meta( 'ID' );
$author_link = get_author_posts_url( $author_id );
$author_avatar = get_avatar_url( $author_id );
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<div>
<div class="">
By <a href="' . esc_url( $author_link ) . '">Author</a>
</div>
</div>';
Thank you to everyone, who helped, it appears that the main problem was that I did not have global $post
declared inside the function.
Also after tweaking the author_id declaration everything started working, here is the final code:
function single_post_entry() {
global $post;
$author_id = $post->post_author;
$author_link = get_author_posts_url( $author_id );
$author_avatar = get_avatar_url( $author_id );
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// ..
echo '<div>
By <a href="' . esc_url( $author_link ) . '">Author</a>
</div>';
}
single_post_content
? and you didn't set the$author
you can use theget_the_author_meta('ID');
to get the current post author ID or the global$post->post_author
– Shibi Commented Dec 13, 2017 at 17:20$author
defined? – inarilo Commented Dec 13, 2017 at 20:08