如何仅在帖子属于特定类别时才显示作者姓名?

时间:2017-03-15 作者:Davood

我在我的主题中有这个功能,我想要的是只显示所选类别的作者名称(比如类别id 90)。其余类别不应显示作者姓名。

我相信>in\\u category可以做到这一点。我还尝试了一些函数,但没有成功,因为我不擅长编码。

提前感谢您的帮助:)

下面是函数

if (! function_exists(\'mom_posts_meta\')) {
function mom_posts_meta ($class = \'\', $display = null) {
    $num_comments = get_comments_number(); // get_comments_number returns only a numeric value

if ( comments_open() ) {
    if ( $num_comments == 0 ) {
        $comments = __(\'No Comments\', \'theme\');
    } elseif ( $num_comments > 1 ) {
        $comments = $num_comments .\' \'. __(\' Comments\', \'theme\');
    } else {
        $comments = __(\'1 Comment\', \'theme\');
    }
    $write_comments = \'<a href="\' . get_comments_link() .\'">\'. $comments.\'</a>\';
} else {
    //$write_comments =  __(\'Comments off\', \'theme\');
    $write_comments = \'\';
}
$author_link = esc_url( get_author_posts_url( get_the_author_meta( \'ID\' ) ) );
if (class_exists(\'userpro_api\')) {
    global $userpro;
$author_link = $userpro->permalink(get_the_author_meta( \'ID\' ));
}

$categories = get_the_category();
$separator = \', \';
$cats = \'\';
if($categories){
    foreach($categories as $category) {
        $cats.= \'<a href="\'.get_category_link( $category->term_id ).\'" title="\' . esc_attr( sprintf( __( "View all posts in %s", \'theme\' ), $category->name ) ) . \'">\'.$category->cat_name.\'</a>\'.$separator;
    }
}
$tags = get_the_tags();
$post_tags = \'\';
if($tags){
    foreach($tags as $tag) {
        $post_tags.= \'<a href="\'.get_tag_link( $tag->term_id ).\'" title="\' . esc_attr( sprintf( __( "View all posts in %s", \'theme\' ), $tag->name ) ) . \'">\'.$tag->name.\'</a>\'.$separator;
    }
}
    $output = \'<div class="mom-post-meta \'.$class.\'">\';
    $author = mom_option(\'post_meta-author\') == 1 ? \'<span class="author vcard" itemprop="author" itemscope itemtype="https://schema.org/Person">\'.__(\'Posted By:\', \'theme\').\' <span class="fn" itemprop="name"><a href="\'.$author_link.\'">\'.get_the_author().\'</a></span></span>\': \'\';
    $date = mom_option(\'post_meta-date\') == 1 ? \'<span>\'.__(\'on:\', \'theme\').\' <time datetime="\'.get_the_time(\'c\').\'" class="updated">\'.get_mom_date_format().\'</time></span>\': \'\';
    //$date = mom_option(\'post_meta-date\') == 1 ? \'<span>\'.__(\'The last Update:\', \'theme\').\' <time datetime="\'.get_the_time(\'c\').\'" itemprop="datePublished" class="updated">\'. get_post_modified_time(mom_option(\'date_format\')).\'</time></span>\': \'\';
    $cat = mom_option(\'post_meta-cat\') == 1 ? \'<span>\'.__(\'In\', \'theme\').\': \'.trim($cats, $separator).\'</span>\': \'\';
    $tag = mom_option(\'post_meta-tag\') == 1 ? \'<span>\'.__(\'Tags\', \'theme\').\': \'.trim($post_tags, $separator).\'</span>\': \'\';
    $comments = mom_option(\'post_meta-comments\') == 1 ? \'<span>\'.$write_comments.\'</span>\': \'\';
    if ($display == \'date_comments\') {
        $output .= $date.$comments;
    } else {
        $output .= $author.$date.$cat.$tag.$comments;
    }
    if(function_exists(\'the_views\')) {
        $output .= \'<span>\'.__(\'Views:\', \'theme\').\' \'.the_views(false).\'</span>\';
    }
    $output .= get_mom_show_review_score();
    if (is_single()) {
        if (mom_option(\'post_meta-tools\') == true) {
        $output .= \'<div class="post-tools">\';
        $output .= \'<a href="javascript:window.print()" rel="nofollow" class="print"><i class="fa-icon-print"> </i>\'.__(\'Print\').\'</a>\';
        $output .= \'<a href="mailto:?subject=\'.get_the_title().\'&body=\'.get_the_title().\' \'.get_permalink().\'" rel="nofollow" class="email"><i class="fa-icon-envelope"> </i>\'.__(\'Email\', \'theme\').\'</a>\';
        $output .= \'</div>\';
        }
    }

    $output .= \'</div>\';
    echo $output;
}
}

1 个回复
最合适的回答,由SO网友:Industrial Themes 整理而成

是的,这是您需要编辑的行,您需要检查当前帖子是否在您指定的类别内。因此,您可以用以下代码替换该行,它应该可以做到:

if(in_category(90)) {
    $author = mom_option(\'post_meta-author\') == 1 ? \'<span class="author vcard" itemprop="author" itemscope itemtype="https://schema.org/Person">\'.__(\'Posted By:\', \'theme\').\' <span class="fn" itemprop="name"><a href="\'.$author_link.\'">\'.get_the_author().\'</a></span></span>\': \'\';
} else {
    $author = \'\';
}
如果这不起作用,则可能是您的类别ID错误,或者此时未正确设置post对象(即,您不在循环中)。

另请注意:您不必使用类别ID,如果希望代码更可读,可以使用slug或name。

相关推荐

使用wp_Dropdown_Categories($args)的输出

请温柔点,我对所有这些编码都是新手!我终于找到了如何获得前端下拉字段,允许我从EDD类别中进行选择(我从Codex中获得):<div class=\"gallery-row\"> <li id=\"categories\"> <h2><?php _e( \'Categories:\' ); ?></h2> <form id=\"category-select\" class=\"category-