0

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!

  • Where you run this function single_post_content? and you didn't set the $author you can use the get_the_author_meta('ID'); to get the current post author ID or the global $post->post_author – Shibi Dec 13 '17 at 17:20
  • @Shibi this is single.php file on the root of the child theme – Scott Agirs Dec 13 '17 at 17:26
  • Yeah sorry didn't notice it's for genesis framework I'm not familiar with it. – Shibi Dec 13 '17 at 18:14
  • where's $author defined? – inarilo Dec 13 '17 at 20:08
  • @inarilo I have updated the code with the definition, still the all the same it outputs localhost/author - without the actual author – Scott Agirs Dec 14 '17 at 2:41
0

get_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>';
0

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>';
}
  • you should accept your answer – inarilo Dec 14 '17 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.