WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] If not category, then add Author Information (22 posts)

  1. markross1967
    Member
    Posted 2 years ago #

    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

  2. thecodeisclear
    Member
    Posted 2 years ago #

    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') ) :?>

  3. markross1967
    Member
    Posted 2 years ago #

    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.

  4. thecodeisclear
    Member
    Posted 2 years ago #

    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>
  5. markross1967
    Member
    Posted 2 years ago #

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

  6. markross1967
    Member
    Posted 2 years ago #

    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

  7. markross1967
    Member
    Posted 2 years ago #

    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.

  8. thecodeisclear
    Member
    Posted 2 years ago #

    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.

  9. markross1967
    Member
    Posted 2 years ago #

    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;
  10. thecodeisclear
    Member
    Posted 2 years ago #

    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.

  11. markross1967
    Member
    Posted 2 years ago #

    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?

  12. thecodeisclear
    Member
    Posted 2 years ago #

    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)

  13. markross1967
    Member
    Posted 2 years ago #

    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

  14. thecodeisclear
    Member
    Posted 2 years ago #

    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

  15. markross1967
    Member
    Posted 2 years ago #

    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.

  16. markross1967
    Member
    Posted 2 years ago #

    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.

  17. markross1967
    Member
    Posted 2 years ago #

    thecodeisclear,

    I got it! I answered my own, above, question:
    On the top line, I just needed to create an array, so that I can exclude multiple categories:

    if (! in_category( array('Political Artwork', 'Quotes') )) {

  18. markross1967
    Member
    Posted 2 years ago #

    Now, I am able to include the "twentyeleven_posted_on" function of my chosen categories, which is great. However, I was wondering if you could help me with a second option?

    In the below code, we are saying, if not categories: Political Artwork, and Quotes, include the following information.

    I would also like to have an "else" statement: whereas, if the category is Political Artwork, or Quotes, include just the "postdate."

    I would appreciate your help.

    Thank you,
    Mark

    function twentyeleven_posted_on() {
        if (! in_category( array('Political Artwork', 'Quotes') )) {
        printf( __( '<span class="sep">Written<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>. Posted </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></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;
  19. thecodeisclear
    Member
    Posted 2 years ago #

    Hi,

    This piece of code should work.

    function twentyeleven_posted_on() {
        if (! in_category( array('Political Artwork', 'Quotes') )) {
            printf( __( '<span class="sep">Written<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>. Posted </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></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()
            );
        }
        else {
            printf( __( 'Posted <a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a>', 'twentyeleven' ),
                esc_url( get_permalink() ),
                esc_attr( get_the_time() ),
                esc_attr( get_the_date( 'c' ) ),
                esc_html( get_the_date() )
            );
        }
    }
    endif;

    Within the printf each %n identifies the corresponding argument (values returned from the esc_* functions). So %1$s --> esc_url( get_permalink() ). I have removed the arguments 5, 6 & 7 which correspond to the author details.

  20. markross1967
    Member
    Posted 2 years ago #

    Awesome!
    That is working great! Thank you again! :)

    After some thinking, I came up with a third, but I think, final, option. I tried to work it out on my own, but I just keep getting parsing errors. :(

    I am following the above logic, but where to put else statements, and the number of brackets, is what is getting me.

    If you would like to help me with this final option, it would be very appreciated. Let me explain, and hopefully you can help me with it:

    I want to go back to the first option, which excluded 'Political Artwork' posts from any of the tags.

    Then, add an additional if (or elseif) statement, and say, if in category 'Scripture,' or 'Quotes':

    if ( in_category( array('Scripture', 'Quotes') ))

    Then
    'Posted by (Author), on (Date)'

    And, lastly, would be the else statement, which would say:
    'Written by (Author), on (Date)'

    The result would be, all 'Political Artwork' posts are completely unattributed. The Scripture and Quotes, would have a posted by attribution. And, all other posts would be Written by, which would be all posts that are actually authored by a person on my site.

    I think that would do it. From there, I could always exclude future categories etc, with that code, as they come along.

    If you could help me to work that out, it would resolved, and really appreciated.

    Thank you again.
    Mark

  21. thecodeisclear
    Member
    Posted 2 years ago #

    Welcome. Glad I could help. I didn't get much time to test this out except for checking for parsing errors and a quick check on a couple of posts.

    function twentyeleven_posted_on() {
        if ( in_category( 'Political Artwork' ) ) {
            // If the category is Political Artwork, write out Posted Date
            printf( __( 'Posted <a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a>', 'twentyeleven' ),
                esc_url( get_permalink() ),
                esc_attr( get_the_time() ),
                esc_attr( get_the_date( 'c' ) ),
                esc_html( get_the_date() )
            );
        }
        elseif ( in_category( array('Scripture', 'Quotes') )) {
            // If the category is "Scripture" or "Quotes", use "Posted by"
            printf( __( '<span class="sep">Posted<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>. Posted </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></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()
            );
        }
        else {
            // All other categories, use "Written by"
            printf( __( '<span class="sep">Written<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>. Posted </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></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;
  22. markross1967
    Member
    Posted 2 years ago #

    thecodeisclear,

    That is EXACTLY what I was looking for! It works perfectly. Thank you so much! Now, I can add or subtract categories, as needed.

    I really appreciate ALL the help!

    Take it easy, and I will mark this topic resolved. :)

    Mark

Topic Closed

This topic has been closed to new replies.

About this Topic