如何将帖子元字段作为元元素添加到文章的<head>中

时间:2016-08-04 作者:Polina Salimova

我需要在文章中添加以下类型的元信息

    <meta name="citation_publication_date" content="1996/05/17">
    <meta name="citation_journal_title" content="Journal of Biological Chemistry">
    <meta name="citation_volume" content="271">
    <meta name="citation_issue" content="20">
    <meta name="citation_firstpage" content="11761">
    <meta name="citation_lastpage" content="11766">
    <meta name="citation_pdf_url"content="http://www.example.com/content/271/20/11761.full.pdf">

https://scholar.google.com/intl/en/scholar/inclusion.html#indexing

1 个回复
最合适的回答,由SO网友:Andy Macaulay-Brook 整理而成

在后期编辑屏幕中,您可以将数据添加为自定义字段,然后将这些自定义字段拉入您的后期模板,如下所示:

Adding custom fields to a post

如果您想为这些字段提供更好的管理界面,这是可能的,而且在web和本网站上有很多指南。搜索admin metabox.

要将数据拉入主题,请将此未测试的代码添加到functions.php 或者,如果你无法编辑主题,请将其添加到你自己的插件中。

function wpse_235005_post_meta(){

    if ( !is_single() ) {
        return; 
        /* bail out if not showing a single post of the right type
           You may want to adjust this conditional to suit your site,
           maybe checking for a custom post type
        */
    }

    global $post; // outside the loop make sure we can see this post

    $fields = array(
        "citation_publication_date",
        "citation_journal_title",
        "citation_volume",
        "citation_issue",
        "citation_firstpage",
        "citation_lastpage",
        "citation_pdf_url"
    )

    foreach( $fields as $field ) {

        if( $value = get_post_meta($post->ID, $field, true) ) {

            // in PHP empty strings are falsy

            echo \'<meta name="\';
            echo $field;
            echo \'" content="\';
            echo $value;
            echo \'">\';
        }
    }
}

add_action(\'wp_head\', \'wpse_235005_post_meta\');
/* adjust the priority to move your meta elements up or down 
   within your document\'s head
*/

相关推荐

Google搜索控制台Sitemap 404错误

我对谷歌搜索控制台中的站点地图有问题。在索引站点地图中,我看到所有posttype都有一个指向该posttype站点地图的链接。https://www.example.com/wp-sitemap-posts-post-1.xmlhttps://www.example.com/wp-sitemap-posts-page-1.xmlhttps://www.example.com/wp-sitemap-posts-product-1.xmlhttps://www.example.com/wp-sitemap-