• Resolved markross1967

    (@markross1967)


    Hi,
    My blogsite is https://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 6 replies - 16 through 21 (of 21 total)
  • Thread Starter markross1967

    (@markross1967)

    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') )) {

    Thread Starter markross1967

    (@markross1967)

    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;

    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.

    Thread Starter markross1967

    (@markross1967)

    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

    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;
    Thread Starter markross1967

    (@markross1967)

    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

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