Support » Fixing WordPress » If not category, then add Author Information

  • Resolved markross1967

    (@markross1967)


    Hi,
    My blogsite is http://www.original-republican.com, and for some time, I have been trying to add some code to my Twenty Eleven, content-single.php file that would display my custom author info, if the category is not “Political Artwork,” or whichever other categories I chose to exclude.

    This basic example is working:

    <?php if (! in_category('Political Artwork') )
    the_author_posts_link(); ?>

    However, I would like to do something a little more custom. I’ve also tried this example, but can’t seem to make it work:

    <?php if (! in_category('Political Artwork') )
    <p>Written by: <?php the_author_posts_link(); ?></p>

    Would somebody be able to tell me how I might be able to make that method work, so that I can expound on it?

    Thank you,
    Mark

Viewing 15 replies - 1 through 15 (of 21 total)
  • The exact code you have mentioned above, works fine on my test site. I added the code immediately after the printf line which outputs the tags, categories and permalink.

    I also noticed that Twenty Eleven also displays an author bio (if the blog has multiple authors and the author details are updated in the profile). You could change that check with your check (on category)

    <?php if ( get_the_author_meta( 'description' ) && ( ! function_exists( 'is_multi_author' ) || is_multi_author() ) ) : // If a user has filled out their description and this is a multi-author blog, show a bio on their entries ?>

    can be replaced with
    <?php if (! in_category('Political Artwork') ) :?>

    Hi, thank you for the response!

    No matter where I place the code:

    <?php if (! in_category('Political Artwork') )
    <p>Written by: <?php the_author_posts_link(); ?></p>

    in my content-single.php file, it is causing a parsing error. 🙁

    Any other suggestions?

    Thank you for the above tip, as well.

    Aw.. just realized it. You are missing a ?> in the first line. This should work

    <?php if (! in_category('Political Artwork') )?>
    <p>Written by: <?php the_author_posts_link(); ?></p>

    Awesome! Apparently, that was the problem. It is working fine now. Thank you so much! 🙂

    thecodeisclear,

    Unfortunately, I just noticed that, this code:

    <?php if (! in_category('Political Artwork') )?>
    <p>Written by <?php the_author_posts_link(); ?></p>

    is not doing the trick. 🙁

    For example, if you take a look at this post:
    http://www.original-republican.com/some-would-sacrifice-americas-ideals/
    which is posted in category, “Political Artwork,” the author information is still appearing.

    Any other suggestions?

    Thank you,
    Mark

    Somehow, I’m thinking that, by adding ?> at the end of the first line, it turned it into two separate php statements, when it needs to be one continuous statement.

    Still, I’m not sure, and don’t know how to fix it.

    Sorry, I hadn’t got your question correctly the first time. Now, I see the text that you want to remove. This is provided by one of the theme’s functions – twentyeleven_posted_on()

    In your functions.php file, find the function twentyeleven_posted_on() and add the category check in there.

    If you enclose the entire printf within the if construct, the entire part of “Written by Admin. Posted December 1, 2011” would not appear. If you want to retain the “Posted” section, you will have to write a new printf in the else section.

    That’s OK. Thank you for responding!

    I tried to edit the below code, in the functions.php file, to include the <?php if (! in_category(‘Political Artwork’) )?> statement, but I kept getting parsing errors.

    Would you happen to know where to place that statement, in the below code, so that it will work correctly?

    Thank you again,
    Mark

    if ( ! function_exists( 'twentyeleven_posted_on' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     * Create your own twentyeleven_posted_on to override in a child theme
     *
     * @since Twenty Eleven 1.0
     */
    function twentyeleven_posted_on() {
    	printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentyeleven' ),
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time() ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		esc_attr( sprintf( __( 'View all posts by %s', 'twentyeleven' ), get_the_author() ) ),
    		get_the_author()
    	);
    }
    endif;

    Here is how I added the code in my functions.php file

    if ( ! function_exists( 'twentyeleven_posted_on' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     * Create your own twentyeleven_posted_on to override in a child theme
     *
     * @since Twenty Eleven 1.0
     */
    function twentyeleven_posted_on() {
        if (! in_category('Political Artwork') ) {
    	printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentyeleven' ),
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time() ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		esc_attr( sprintf( __( 'View all posts by %s', 'twentyeleven' ), get_the_author() ) ),
    		get_the_author()
    	);
        }
    }
    endif;

    Hope this helps.

    Thank you again, for the help!

    Unfortunately, every time I try to update my functions.php file, including with the above code, I get a parsing error. 🙁

    May be I should stick with editing the content-single.php file?

    If you can send me your current functions.php file, I could edit it and send it back. (or you could load it somewhere like pastebin/pastie.org)

    Thank you for all the help!

    Here is the link, in pastebin, for my (Twenty Eleven Theme) functions.php file: http://pastebin.com/Vbj2u92E

    I really appreciate it.

    Mark

    Hi,

    Here is the updated functions.php file – http://pastie.org/4363507

    I tested this on my local install of WP. Here is the screenshot of the results
    http://thecodeisclear.in/wp-content/uploads/2012/07/category-author-details.png

    Awesome! That looks like it did the job! 🙂
    So, basically what you did, is redefine the “twentyeleven_posted_on” function.

    From there, I see that I can further customize the wording. i.e. Written by, as opposed to Posted by, etc.

    Thank you so much for doing that.

    thecodeisclear,

    IF you wouldn’t mind another “related” question, could you please tell me how I could exclude multiple categories? I would like to exclude category Quote, as well as Political Artwork.

    I have tried to get it myself, but not have much luck.

    Thank you.

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘If not category, then add Author Information’ is closed to new replies.