WordPress.org

Ready to get started?Download WordPress

Forums

Author(s) page (6 posts)

  1. Nessdufrat
    Member
    Posted 1 year ago #

    Hi!
    I'm having a bit of a problem here... I have 4 authors on my website, and I'd like to have both a page listing all authors and their description, bio, website, etc., and also a page listing one of the author with all of their posts. It seems I can't do it on the same page, because the template author.php needs the slugname to be passed to display something...
    What would be a good solution would be to tell this author.php that if no slugname is passed, it should display the list of authors, but if the user actually clicks on one of the authors' name in a post, he should be able to see this particular author's page.

    What I have so far is a mix of both, and I can't do any if statement since I don't know how to "tell" the script that if no user is specified, it should display only the list...
    I'm getting crazy with this. Please don't tell me to use a plugin, this is for a mobile theme, I'm using any theme switcher, so if I enable a plugin, it will enable it for the desktop version, and this one is perfectly fine right now and doesn't need any change.

    My code for the author.php page. The theme is Brunelleschi, not that it really matters, but who knows. I was kinda hoping I could do a "authors.php" page, but no luck there.

    <?php
    /*
    Template Name: Auteur
    */
    get_header(); ?>
    
     <h1>Liste des auteurs :</h1><?php wp_list_authors('show_fullname=1&optioncount=1&orderby=post_count&order=DESC&number=4'); ?>
    <!-- This sets the $curauth variable -->
    <!-- that's where I would like to put my if statement, so that it displays only the above when no specific author is selected -->
        <?php
        $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
        ?>
    <div id="content" class="<?php echo $curauth->user_nicename; ?>">
        <h1><?php echo $curauth->nickname; ?></h1>
        <dl>
            <dt>Site internet : </dt>
            <dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
            <dt>Biographie : </dt>
            <dd><?php echo $curauth->user_description; ?></dd>
        </dl>
    
        <h2>Les articles de <?php echo $curauth->nickname; ?> :</h2>
        <ul>
    <!-- The Loop -->
    
        <?php
    
    	query_posts( $query_string.'&showposts=9999');
    
    	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'); ?> dans <?php the_category('&');?>
            </li>
    
        <?php endwhile; else: ?>
            <p><?php _e('Aucun article trouv&eacute; pour cet auteur.'); ?></p>
    
        <?php endif; ?>
    
    <!-- End Loop -->
    
        </ul>
    
    </div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. Jo Biesta
    Member
    Posted 1 year ago #

    Hi Nessdufrat,

    What you would need to do is use is something along these lines:

    if (!isset($_GET['author_name'])) {
    // if its not set display list of all authors here
    }
    else {
    /display the single users author information
    }
    Hope this helps!

  3. Nessdufrat
    Member
    Posted 1 year ago #

    Yep, that's exactly what I was looking for! But actually, it doesn't work, I can't get the single author info by clicking on his name :(

    <?php
    /*
    Template Name: Auteur
    */
    get_header();
    if (!isset($_GET['author_name'])) {
    // if its not set display list of all authors here
    ?>
    <!-- This sets the $curauth variable -->
     <h1>Liste des auteurs :</h1><?php wp_list_authors('show_fullname=1&optioncount=1&orderby=post_count&order=DESC&number=4'); 
    
    }
    
    else {
        $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
        ?>
    <div id="content" class="<?php echo $curauth->user_nicename; ?>">
        <h1><?php echo $curauth->nickname; ?></h1>
        <dl>
            <dt>Site internet : </dt>
            <dd><a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a></dd>
            <dt>Biographie : </dt>
            <dd><?php echo $curauth->user_description; ?></dd>
        </dl>
    
        <h2>Les articles de <?php echo $curauth->nickname; ?> :</h2>
        <ul>
    <!-- The Loop -->
    
        <?php
    
    	query_posts( $query_string.'&showposts=9999');
    
    	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'); ?> dans <?php the_category('&');?>
            </li>
    
        <?php endwhile; else: ?>
            <p><?php _e('Aucun article trouv&eacute; pour cet auteur.'); ?></p>
    
        <?php endif; ?>
    
    <!-- End Loop -->
    
        </ul>
    
    </div>
    <?php }
    get_sidebar(); ?>
    <?php get_footer(); ?>

    (I think my code is correct, though)

  4. graphicgeek
    Member
    Posted 1 year ago #

    The problem is, I don't think author.php uses a url query (mysite.com/author/?author_name=bob). I think the structure is mysite.com/author/bob. If there is no author with the name bob, it redirects to the 404 page. Same thing if you just put in mysite.com/author

    What I would try is setting up a custom page template to list all the authors, and let author.php just handle the single author pages.

    For the custom page template you could try something like this:

    <?php
    
    //See https://codex.wordpress.org/Template_Tags/wp_list_authors for more args
     $args = array(
        'orderby'       => 'name',
        'order'         => 'ASC',
        'exclude_admin' => false,
        'hide_empty'    => true,
        'echo'          => false, //set to false so we can put the output into a variable
        'style'         => 'none', //set to 'none' so it will jsut return the names (without the <li> tags)
        'html'          => false
    	);
    
     $authors = explode(',', wp_list_authors($args)); //break the output into an array 
    
    foreach($authors as $author){
    
    	 ?>
    
    	<h2><?php echo $author; ?></h2>
    
    	<?php
    //see http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
    	$queryargs = array(
    	'author_name' => $author,
    	'posts_per_page' => -1 //show all posts
    	);
    
    	$query = new WP_Query( $queryargs ); //set up a query to display posts by this author
    
    	// The Loop
    	while ( $query->have_posts() ) { $query->the_post(); //start your loop
    	?>
    
    	<h3><?php the_title(); ?></h3>
       	<?php the_excerpt(); ?>
    
    	<?php
    	}//end your loop
    	wp_reset_postdata(); // Restore original Post Data
    
    }//end foreach
    ?>
  5. Nessdufrat
    Member
    Posted 1 year ago #

    Thanks! That's actually what I tried with my other page, that I called auteurs.php, and no way I could link to it... I tried, and it returned errors for get_header, and other functions. I had no idea why. It's like wordpress wouldn't accept it as a template.
    Like I said, I'm using any theme switcher, so I can't really mess up with the pages and stuff, so appart from hard-coding the link to the page (which didn't work), there's not really a solution. Or is there?

  6. Nessdufrat
    Member
    Posted 1 year ago #

    Ok, so finally I ended up making two pages, and playing around with templates and stuff, and it seems fine. I would have loved to know a solution to have only one page to display the list as well as each individual author's info, though, so if anyone knows or finds out how to do it, I would love to hear about it :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.