从Instant Fields插件的自定义字段更新Wordpress Post字幕/作者元数据

时间:2016-09-05 19:52:28

标签: php wordpress facebook

我们目前正在我们的网站上运行Facebook即时文章。我们遇到的问题是它抓取默认作者而没有副标题。我在CMB2中都定义了它们:

$cmb->add_field( array(
        'name' => __( 'Author:', 'cmb2' ),
        'id' => $prefix . 'author',
        'type' => 'text_medium',
    ) );
$cmb->add_field( array(
        'name' => __( 'Lede:', 'cmb2' ),
        'id' => $prefix . 'lede',
        'type' => 'textarea',
    ) );

我可以通过以下方式调用它:

$lede = get_post_meta( get_the_ID(), '_cmb_lede', true ); 
echo esc_html( $lede );

$author = get_post_meta( get_the_ID(), '_cmb_author', true ); 
echo 'By ' . esc_html( $author ); 

不幸的是,这会导致IA Wordpress插件不一致,后者调用字幕:

if ( ! is_null( $this->_subtitle ) ) {
            return $this->_subtitle;
        }

和作者:

$authors = array();

$wp_user = get_userdata( $this->_post->post_author );

if ( is_a( $wp_user, 'WP_User' ) ) {
    $author = new stdClass;
    $author->ID            = $wp_user->ID;
    $author->display_name  = $wp_user->data->display_name;
    $author->first_name    = $wp_user->first_name;
    $author->last_name     = $wp_user->last_name;
    $author->user_login    = $wp_user->data->user_login;
    $author->user_nicename = $wp_user->data->user_nicename;
    $author->user_email    = $wp_user->data->user_email;
    $author->user_url      = $wp_user->data->user_url;
    $author->bio           = $wp_user->description;

    $authors[] = $author;
}

/**
 * Filter the post author(s).
 *
 * @since 0.1
 *
 * @param array  $authors  The current post author(s).
 * @param int    $post_id  The instant article post.
 */
$authors = apply_filters( 'instant_articles_authors', $authors, $this->_post->ID );

return $authors;

更新

我取得了一些进展。这是在我的functions.php我很接近但不完全在那里:

use Facebook\InstantArticles\Elements\Author;

add_action( 'instant_articles_after_transform_post', function ($ia_post) {
    $instant_article = $ia_post->instant_article;
    $post_id = $ia_post->get_the_id();
    $author = get_post_meta( $post_id, '_cmb_author', true );
    $instant_article->withHeader(addAuthor( Author::create()->withName($author) ));
} );

在functions.php中覆盖这些自定义字段的正确方法是什么,以便它可以正确地将其拉入Facebook Instant Articles插件?

0 个答案:

没有答案