【问题标题】:Adding author info outside the loop in Genesis在创世纪的循环之外添加作者信息
【发布时间】:2016-11-06 15:52:25
【问题描述】:

我发布这个问题是因为在我的网站上将作者信息添加到我的帖子英雄时遇到了一点问题。

我在 Wordpress 中使用 Genesis 框架,所以我所做的是从帖子中删除帖子信息并将其添加回帖子英雄中。一切正常,除了作者姓名不再显示,因为它尚未在 post 循环中获取。

    // 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 );

为了能够在帖子英雄中添加帖子作者信息,我查找了 stackoverflow 并找到了一个链接,OP 可以通过为它创建一个短代码并在 hero-info 中运行它来修复它

function author_shortcode() {
    global $post;
    $author_id=$post->post_author;
    the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');

然后将此短代码 [作者] 添加到

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;
    }
}

这是现在的结果:http://imgur.com/a/6lX5J 由于某种原因,它显示在错误的位置。有人知道这是怎么回事吗?

该网站可以在这里找到:http://websforlocals.com/business/

希望我提供了足够的信息,并且有同样问题的人可以得到帮助。

【问题讨论】:

  • 这是您的简码注册代码中的问题,这就是为什么您的作者姓名显示在帖子的开头而不是您拥有简码的地方。阅读下面的答案以获取背景信息。

标签: wordpress author genesis info


【解决方案1】:

这是您的 ShortCode 注册 php 代码的问题。

在添加短代码时,我们不应该 ECHO 任何内容,因为这样它不会在我们想要的位置而是在帖子内容的顶部回显。

所以总是在短代码函数中返回输出,然后回显短代码函数。

现在 WordPress 对回显结果和返回结果的函数有一个约定,即 the_author_metaget_the_author_meta(您使用的第一个函数将显示/回显结果,但是 get_ 函数将返回值) .

我们需要在您的短代码注册块中使用get_the_author_meta 而不是the_author_meta,它将解决您的显示位置问题。

function author_shortcode() {
global $post;
$author_id=$post->post_author;
return get_the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    相关资源
    最近更新 更多