0

I am making a custom Genesis theme, and have a custom author.php file that pulls in various custom fields (Using Advanced Custom Fields), and author meta information to the page from the author profile page... It also displays their latest posts.

This works perfectly, IF the author has posts assigned to them. If they don't, the page doesn't output any of the content that is normally pulled from the authors profile...

I've searched StackExchange, and whilst this has been mentioned a few times, I can't seem to find an answer that works.

I need the author.php page to output the author information whether the user has posts or not. If they don't, the profile meta and custom fields should still display, and the recent posts section should not show any posts in it.

Here's my author.php code.

// remove Genesis default loop
remove_action( 'genesis_loop', 'genesis_do_loop' );

// Remove Header Markup
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_open', 5 );
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_before_loop', 'genesis_do_author_title_description', 15 );

//remove the default sidebar widget setup
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
remove_action( 'genesis_sidebar_alt', 'genesis_do_sidebar_alt' );

// Add in the layout sections
add_action('genesis_loop','add_top_author_section');
add_action('genesis_sidebar','add_sidebar_info');
add_action('genesis_after_content_sidebar_wrap', 'add_latest_posts');


function add_top_author_section() { 
    //vars
        $avatar = get_avatar( get_the_author_meta( 'ID' ), apply_filters( 'th_author_bio_avatar_size', 300 )  );
        $display_name = get_the_author_meta( 'display_name' );
    ?>
    <div class="author-details">
            <div class="left-profile">
                <?php echo $avatar; ?>
            </div>
            <div class="right-profile">
                <h2><?php echo $display_name; ?></h2>
                <p><?php the_author_meta('description'); ?>
            </div>
    </div>
<?php
}

function add_sidebar_info() {  

    // Cuisines
    $user_id = get_the_author_meta( 'ID' );
    $display_id = 'user_'.$user_id;

    $terms = get_field('types_of_cuisine2', $display_id);

    if ( $terms ) { 
        echo '<div class="widget-sidebar-section cuisines"><h2>Cuisines</h2>';
        foreach ( $terms as $term ) :?>
            <a href="http://tastehaus.flywheelsites.com/recipes/?_sfm_cuisine=<?php echo $term; ?>"><?php echo $term; ?></a>

        <?php endforeach; 
        echo '</div>';
        } 

    // Based In
    echo '<div class="widget-sidebar-section based-in"><h2>Based In</h2>';
    the_field('based_in', $display_id); 
    echo '</div>';

    // Status
    echo '<div class="widget-sidebar-section status"><h2>Status</h2>';
    the_field('status', $display_id); 
    echo '</div>';  

    // Signature Dish
    echo '<div class="widget-sidebar-section sig-dish"><h2>Signature Dish</h2>';
    the_field('signature_dish', $display_id); 
    echo '</div>';  

    // Amazon Store Button
    ?>
    <a href="<?php the_field('amazon_store_link', $display_id);?>"><button>See My Recommended Tools</button> </a>
<?
}


function add_latest_posts() { 

    echo '<div class="user-latest-posts">';
    echo '<h2 style="text-align:center;">Recent Posts by '.  get_the_author_meta( 'first_name' ) .'</h2>';
    echo '</div>';

    global $post;
    // arguments, adjust as needed
    $args2 = array(
        'author'            =>  get_the_author_meta( 'ID' ), 
        'post_type' => array( 'recipes', 'tools', ), 
        'orderby'           =>  'post_date',
        'order'             =>  'ASC',
        'posts_per_page'    =>  6,
    );
    /* 
    Overwrite $wp_query with our new query.
    The only reason we're doing this is so the pagination functions work,
    since they use $wp_query. 
    */
    global $wp_query;
    $wp_query = new WP_Query( $args2 );
    if ( have_posts() ) : 
        echo '<div class="posts-query">';
        while ( have_posts() ) : the_post(); ?>
                <div class="query-post">
                        <div class="query-padding">
                            <div clss="posts-image">
                                <?php the_post_thumbnail("thumbnail");?>
                            </div>
                            <div class="post-categories">
                            <?php $postType = get_post_type_object(get_post_type());
                                if ($postType) {
                                echo esc_html($postType->labels->singular_name);
                                } 
                            ?>
                            </div>                          
                            <div class="posts-title">
                                <a href="<?php the_permalink(); ?>">
                                    <h3> 
                                        <?php the_title() ?>
                                    </h3>
                                </a>
                            </div>
                        </div>
                    </div>
        <? endwhile; 
        echo '</div>';
        do_action( 'genesis_after_endwhile' );
    endif;
    wp_reset_query();
}
genesis();
3
  • 3
    Hi, thanks for your question... I'm sorry, but it's not a valid WP template. It's also ignoring WP best practices, because you should never define any functions in template files. So I'm afraid that you have to contact Genesis support (as a thirdparty solution it's not supported on this site). Nov 23, 2018 at 14:25
  • The problem is with Wordpress, not Genesis. Wordpress doesn't allow custom author.php files to show author details if the author doesn't have posts assigned to them. Nov 29, 2018 at 13:39
  • Still. Change for WordPress won’t make it work on genesis. Genesis is not a valid WP theme (it doesn’t follow best WP practices, so...). You should go to genesis support and they should be able to help you. Nov 29, 2018 at 13:41

1 Answer 1

1

I managed to work out the issue, so wanted to share in case others run into the same problem.

The issue was coming from get_the_author_meta() which only works if an author has posts assigned to them...

The solution was to change the code to:

$author = get_user_by( 'slug', get_query_var( 'author_name' ) );

Then grab the meta info:

$author->ID
$author->description
$author->display_name

etc

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.