Meta function issue

时间:2014-04-19 作者:Jamie

从旧的WordPress股票主题中提取线索,我创建了一个元函数来显示每个帖子的信息。

function my_entry_meta() {
// Translators: used between list items, there is a space after the comma.
$categories_list = get_the_category_list( __( \', \', \'ritualHealing\' ) );

// Translators: used between list items, there is a space after the comma.
$tag_list = get_the_tag_list( \'\', __( \', \', \'ritualHealing\' ) );

$date = sprintf( \'<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>\',
    esc_url( get_permalink() ),
    esc_attr( get_the_time() ),
    esc_attr( get_the_date( \'c\' ) ),
    esc_html( get_the_date() )
);

$author = sprintf( \'<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>\',
    esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ),
    esc_attr( sprintf( __( \'View all posts by %s\', \'ritualHealing\' ), get_the_author() ) ),
    get_the_author()
);

//Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author\'s name.
?>
 <div class="blog_entry"> 
     <span class="author">Posted by --<a href="#" class="normaltip" title="Posted By">  <?php the_author(); ?></a></span>
     <span class="categories">-- Category --<?php __(\'%1$s\') ?></span> 
     <span class="date">-- <?php __(\'%3$s\') ?> --</span>
     <span class="comments"><a href="single_blog.html" class="normaltip" title="Comments"><?php comments_number( \'no responses\', \'one response\', \'% responses\' ); ?></a></span> 
     <span class="tags"><?php __(\'%2$s\') ?></span>
 </div><?php

printf(
    $utility_text,
    $categories_list,
    $tag_list,
    $date,
    $author
);
 }
 endif;
由于某些原因,类别和日期不会显示。作者姓名和评论编号显示得很好。我错过什么了吗?

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

好吧,这不起作用是出于上帝的原因。

<div class="blog_entry"> 
    <span class="author">Posted by --<a href="#" class="normaltip" title="Posted By">  <?php the_author(); ?></a></span>
    <span class="categories">-- Category --<?php __(\'%1$s\') ?></span> 
    <span class="date">-- <?php __(\'%3$s\') ?> --</span>
    <span class="comments"><a href="single_blog.html" class="normaltip" title="Comments"><?php comments_number( \'no responses\', \'one response\', \'% responses\' ); ?></a></span> 
    <span class="tags"><?php __(\'%2$s\') ?></span>
</div>
这部分代码根本没有任何意义。您试图通过打印格式字符串而不是值来输出类别和日期(<?php __(\'%1$s\') ?> - 这里的价值在哪里?更准确地说,你甚至没有在那里打印任何东西-__ 函数返回其值,不打印任何内容)。

下面您可以找到正确的代码

function my_entry_meta() {
    // Translators: used between list items, there is a space after the comma.
    $categories_list = get_the_category_list( __( \', \', \'ritualHealing\' ) );

    // Translators: used between list items, there is a space after the comma.
    $tag_list = get_the_tag_list( \'\', __( \', \', \'ritualHealing\' ) );

    $date = sprintf( \'<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>\',
        esc_url( get_permalink() ),
        esc_attr( get_the_time() ),
        esc_attr( get_the_date( \'c\' ) ),
        esc_html( get_the_date() )
    );

    /* You don\'t use $author variable anywhere in your ouput (you use the_author tag instead), so it\'s redundant and should be deleted */
    $author = sprintf( \'<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>\',
        esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) ),
        esc_attr( sprintf( __( \'View all posts by %s\', \'ritualHealing\' ), get_the_author() ) ),
        get_the_author()
    );

    // This comment is wrong - I\'m guessing you copied this part of code from a theme that was using sptrinf/printf function to ouput this HTML?
    //Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author\'s name.
    ?>
    <div class="blog_entry"> 
        <span class="author">Posted by --<a href="#" class="normaltip" title="Posted By">  <?php the_author(); ?></a></span>
        <span class="categories">-- Category --<?php echo $categories_list; ?></span> 
        <span class="date">-- <?php echo $date; ?> --</span>
        <span class="comments"><a href="single_blog.html" class="normaltip" title="Comments"><?php comments_number( \'no responses\', \'one response\', \'% responses\' ); ?></a></span> 
        <span class="tags"><?php echo $tag_list; ?></span>
    </div><?php

    /* I\'m pretty sure you don\'t need the printf call from next line */
    printf(
        $utility_text,
        $categories_list,
        $tag_list,
        $date,
        $author
    );
}
endif;

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在