问题描述:

I'm posting this question as I'm having a little trouble adding the author info to my post hero on my website.

I'm using the Genesis framework with Wordpress, so what I did is removing the post info from the post and adding it back in the post hero. That all works, except that the author name is not being shown anymore as it's not yet fetched in the post loop.

 // Remove entry title

remove_action( 'genesis_entry_header', 'genesis_do_post_title' );

// Remove post info

remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );

// Add page title

add_action( 'hero-info', 'genesis_do_post_title' );

// Add page info

add_action( 'hero-info', 'genesis_post_info', 12 );

To be able to add the post author info back in the post hero, I've looked up stackoverflow and found a link where the OP was able to fix it by creating a shortcode for it and running it in the hero-info

function author_shortcode() {

global $post;

$author_id=$post->post_author;

the_author_meta( 'display_name', $author_id );

}

add_shortcode('author', 'author_shortcode');

This shortcode [author] is then added into

add_filter( 'genesis_post_info', 'custom_post_info' );

function custom_post_info( $post_info ) {

if ( is_archive() || is_home() ) {

$post_info = __( 'Article by [author] [post_author_posts_link] on [post_date] - [post_comments zero="Leave a Comment" one="1 Comment" more="% Comments" hide_if_off="disabled"]', 'tcguy' );

return $post_info;

}

}

This is the result now: http://imgur.com/a/6lX5J

It is shown in the wrong place for some reason. Anybody knows how this can be?

The site can be found here: http://websforlocals.com/business/

Hope I gave enough info, and that someone with the same problem can be helped out.

网友答案:

It is issue in your ShortCode registering php code.

When adding short-code we should not ECHO anything as this way it will not be echoed at the place we want to but at the top of the post content.

So always return the output in short code function, and then echo the short-code function.

Now WordPress have a convention for functions which echo the result and which returns the result, i.e. the_author_meta vs get_the_author_meta (first one which you are using will display/echo the result, however get_ functions will return the values).

We need to use get_the_author_meta instead of the_author_meta in your shortcode registering block and it will solve your displaying location issue.

function author_shortcode() {
global $post;
$author_id=$post->post_author;
return get_the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');
相关阅读:
Top