1

I have this function in my theme, what i want is to display author name only for the selected category (say category id 90). No author name should be displayed for the rest of the categories.

I believe > in_category can do this. I also tried some function but no luck as I'm not good at coding.

Thanks in Advance for your help :)

Here is the function

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

}

  • I think I have to change/modify this line to get the work done. $author = mom_option('post_meta-author') == 1 ? '<span class="author vcard" itemprop="author" itemscope itemtype="schema.org/Person">'.__('Posted By:', 'theme').' <span class="fn" itemprop="name"><a href="'.$author_link.'">'.get_the_author().'</a></span></span>': ''; – Davood Mar 15 '17 at 12:55
2

Yes, that is the line you need to edit, and you need to check and see if the current post is within your specified category. So you could replace that line with this code and it should do the trick:

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

If that doesn't work, either you have the wrong category ID, or the post object isn't getting set correctly at this point (i.e. you aren't in the loop).

Also note: you don't have to use the category ID, you can use the slug or name if you want it to be more readable code.

  • 1
    Yes Sir, It works! :) – Davood Mar 15 '17 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.