WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to turn ON/OFF links to my authors' author.php pages? (12 posts)

  1. WPChina
    Member
    Posted 5 years ago #

    Only some of my authors have full details, so I only want my users to have access to some of my authors' individual author.php pages.

    Is there a way to turn on/off the links to these pages on the post pages? For example, Author A writes a post and has full details on his author.php, so the URL link to his page is active. But Author B has no information so I don't want a URL to be active to his details page.

    Is there a plugin or solution to this problem?

  2. WPChina
    Member
    Posted 5 years ago #

    I'm playing around with things and tried adding this to my single.php, but it doesn't seem to work. I basically want to NOT put the link down for any post written by user with ID #2:

    <?php if (!is_author('2')) : ?>
    <?php the_author_posts_link(); ?>
    <?php endif ; ?>

    I know, I know, in the codex it says is_author is not supposed to take a parameter, but I tried anyway :)

    I thought that it I could create an array of all the IDs for authors that should NOT have links, that might be a way to solve my problem, but it doesn't work.

  3. taffeljohan
    Member
    Posted 5 years ago #

    is_author is meant for testing the role of the current user. It returns true if the currently logged in user has the author role. So (as I guess you realized) using it the way you tried to makes no sense.

    I would try testing get_the_author_description(). Like this:

    $desc = get_the_author_description();
    if ($desc!='') { // The description is not empty
        the_author_posts_link();
    }
  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Noooo, that's actually not what is_author does at all.

    is_author() returns true if the current page being displayed is an author template page. Like /author/otto or something.

    is_author(2) would return true only if the current page was the user number 2's author page.

    is_author('otto') would return true only on the author "otto"'s page.

    You can pass in the author number, nickname, or "nicename".

  5. taffeljohan
    Member
    Posted 5 years ago #

    Doh! Sorry, my bad. I think the rest of what I wrote makes sense, though.

  6. WPChina
    Member
    Posted 5 years ago #

    Thanks for the ideas, but then I am back at the beginning again.

    I want to place code on my post pages - not my author page. Each of my posts of course has an author, but only a few of my authors have descriptions and other details for them. Therefore, I do not want to to send my readers to blank author pages -- I only want to "activate" the author pages for authors who have details written on them.

    Therefore, I have thought of the following ways to do this, though I am still searching for the code since I'm not a PHP pro. I realize that this is the only code I can use, but there must be ways to call it or work around it to:
    <?php the_author_posts_link(); ?>

    1) Do an if/then statement that turns on the link to an author's page from the post page only if the description (i.e. "Biographical Info" is what it is called in the WordPress User area) has content within it. Therefore, if the author has information, the HTML might look like this:
    Posted by <a href="http://link-to-author-page">AUTHOR-NAME</a> but if the author has no description, the html on the post page would simply be this: Posted by AUTHOR-NAME

    2) Have a plugin that puts a tick box on each "Edit User" page in the admin panel that allows me to turn ON/OFF the link when this is used: <?php the_author_posts_link(); ?>

    Sp, that said, are there any ideas for going about this? I can usually take PHP code apart, but I'm not smart enought to build it ,so that is why I need help :(

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Look more closely at what taffeljohan posted above. He gave you the answer, using method #1 that you describe. More or less.

  8. taffeljohan
    Member
    Posted 5 years ago #

    @WordPressChina

    I understood what you meant the first time. That's what the code I posted above is supposed to do (your alternative 1). It checks to see if there is anything in the biographical info field using get_the_author_description() and only prints a link if there is. It is meant to be used on the posts page, of course, because if you were already on the author page, why would you want a link to that page?

  9. taffeljohan
    Member
    Posted 5 years ago #

    Thanks Otto42, you beat me to it.

  10. WPChina
    Member
    Posted 5 years ago #

    Yes, you are correct Otto42 and taffeljohan. This is a great snippet of code too because now we can all do lots of things based on the different fields missing or existing in a user's profile.

    But I still have some problem with the if/else statement becasue if I use your code it will not display anything if the author has no description (but I want to at least show their name). See this code that I'm trying to use. It will display the link to author page if there is a description, but if no description exists it will just show the author's nickname. I know this code is almost perfect, but I can't get the syntax correct:

    <?php $desc = get_the_author_description();
    if ($desc!='') { // The description is not empty
    echo '<i>Posted by: ';
        the_author_posts_link();
    echo '</i>';
    }
    else {
    echo '<i>Posted by: ';
        <?php the_author_nickname(); ?>
    echo '</i>';
         };
    ?>
  11. taffeljohan
    Member
    Posted 5 years ago #

    You have an extra semicolon at the end of your else block. You don't end blocks with ;, just statements.

  12. WPChina
    Member
    Posted 5 years ago #

    Ah, yes, fixed!! :)

    Thank you for bearing with me on this -- it works now and I've found that this code is useful for lots of things now with just placing different template tags within!

    tks!

Topic Closed

This topic has been closed to new replies.

About this Topic