1

I have a page with profiles in which people can post or comment on a post. The problem is that if this user chooses to comment instead of post, he or she will not get an author page until it has submitted a post. Is there a way to show the author page anyway? Because I have a function that shows the comments on the author page, but because it has no posts in it, that won't show too...

I hope someone can help me out.

PS: I already tried several plugins, but those things didn't work out.

FYI: this is default behavior of Wordpress and I am using the default author.php template which has an "if have posts" loop, if not, loop-no-posts loop. So the author page is not created is there is no post. That is the whole point.

Edit: my code:

        <?php if (have_posts()) : ?>

            <ul><?php
    global $wpdb;
    $user_id = $post->post_author;  
    $where = 'WHERE comment_approved = 1 AND user_id = ' . $user_id ;

    $user = get_userdata($user_id);

    ?>

   <?php while (have_posts()) : the_post(); ?>

            <?php responsive_entry_before(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>       

                <div class="post-entry"><blockquote>
                    <?php if ( has_post_thumbnail()) : ?>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >

                        </a>
                    <?php endif; ?>
                    <?php the_excerpt(); ?>

            </article>    
            <?php responsive_entry_after(); ?>

        <?php 
        endwhile; 

        /* get_template_part( 'loop-nav' ); */

    else : 

        get_template_part( 'loop-no-posts' ); 

    endif; 
    ?>  


<?php
    $args = array(
        'user_id' => $user->ID,
        'number' => 10, // how many comments to retrieve
        'status' => 'approve'
        );

    $comments = get_comments( $args );

    if ( $comments )
    {
        foreach ( $comments as $c )
        {
        $output.= '<a href="'.get_comment_link().'">';
        $output.= get_the_title();
        $output.= '</a>';
        }

        echo $output;
    } else { echo "bla";}?>

</div>
13
  • 1
    It is really hard to say without seeing what code you are already using, and whether this is a theme your making on your own, or one that is already out there etc etc. Please try and describe your question with more detail, it would be hard to advise you without further details.
    – Vigs
    Jul 17, 2013 at 19:37
  • Well, I don't see what makes is hard, because this is default behaviour of Wordpress. I just want to know if it is possible to show an user author page (seeing it created) if there is no post and if so, how.. I am just using the default author.php of Wordpress. So the one with "if has posts" then a loop, else loop-no-posts.
    – Johan
    Jul 17, 2013 at 19:46
  • So your saying that, if a user has no posts, then wordpress does not produce a yoursite.com/authorname page for that user. I was not aware of that behavior. Btw there is no default author.php page for wordpress, that file is created by the author of the theme, there could be any code in that file. The author.php file may be similar but will differ from theme to theme. Hence my original comment.
    – Vigs
    Jul 17, 2013 at 19:50
  • If the author.php page is missing, wordpress then falls back to the index.php file to populate the author page, this behavior applies to many other templates files aswell with index.php being the fallback.
    – Vigs
    Jul 17, 2013 at 19:50
  • Jep, that's exactly what Wordpress does (or does not). It does not create the author page. Frankly, if someone has commented, but did not posted, the link to the author page is an ID instead of the username. If the same person submits the post, the link will change to the author's username... And yes, sorry, there is no default author page in twentyeleven or ten. I made it myself, but that's no hocus pocus. Just "if have post" and an else... Can I use a different function instead of if_have_posts that will show me the author page?
    – Johan
    Jul 17, 2013 at 19:51

1 Answer 1

0
<?php 
        $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); 
        ?>
        <?php if ( have_posts() ) ?>
        <h2>Posts by <?php echo $curauth->nickname; ?>:</h2>
            <ul>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                <li>
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
                    <?php the_title(); ?></a>,
                    <?php the_time('d M Y'); ?> in <?php the_category('&');?>
                </li>
            <?php endwhile; else: ?>
                <p><?php _e('No posts by this author.'); ?></p>
            <?php endif; ?>
            </ul>
    <?php
        $args = array(
            'user_id' => $curauth->ID,
            'number' => 10, // how many comments to retrieve
            'status' => 'approve'
            );

        $comments = get_comments( $args );

        if ( $comments )
        {
            $output.= "<ul>";
            foreach ( $comments as $c )
            {
            $output.= '<li>';
            $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
            $output.= get_the_title($c->comment_post_ID);
            $output.= '</a>';
            $output.= "</li>\n";
            }
            $output.= '</ul>';

            echo $output;
        } else { 
            echo "bla";
        }
?>

Modified from here.

Edit: Added OP's code

4
  • Thanks Ron, but it does not do what I want. I already can show the posts if they exist and show anything I like if I don't have them, but the problem is that if there is no post, the COMMENTS the user has made are not shown too... I updated my code to show you how I have implemented the comments... so now, if there is no post, then no comment will shown. If there is a post, all comments the user has made will shown too... That is not what I want.
    – Johan
    Jul 17, 2013 at 20:56
  • not to be blunt but your making some basic mistakes, ones I explained above in a previous comment. In the snippet you updated above your logic works like so: IF POSTS -> GET USER, THEN GET POSTS. IF NO POSTS -> GET COMMENTS FROM USER. You cannot get the comments using $user->user_id, when you haven't even set $user because $user is inside of if has posts...
    – Vigs
    Jul 17, 2013 at 21:00
  • i update my answer to include your comments output that will display under the posts regardless if there are any posts or not.
    – Vigs
    Jul 17, 2013 at 21:06
  • excellent im happy it worked, cheers!
    – Vigs
    Jul 17, 2013 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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