• Resolved saurav.rox

    (@sauravrox)


    I have created my own author page (author.php) where all the posts from that author is listed. The link to that page is – localhost/abc/author/new-user/.
    This is working fine in localhost. But the same code is not working in my live site. The url in the live site is same as localhost- example.com/author/new-user/.
    When visited this url, it redirects to archive.php and nothing found message is displayed even though posts are there for the user.

    The problem with the code is, If I remove $user_roles[0] == 'trip_vendor' from if condition to make it just if( is_author() ) then it works in live site as well. I am surprised why this if( $user_roles[0] == 'trip_vendor' && is_author() ) is returning false on live site and not on local.

    I have a function to handle this. Below is the code how I am handling this.

            add_filter( 'template_include', array( $this, 'abc' ),100 );
                function abc( $template_path ) {
                    global $post;
                    $user_meta = get_userdata($post->post_author);
                    $user_roles = $user_meta->roles;
                    if( $user_roles[0] == 'trip_vendor' && is_author() )
                    {
                        if ( $theme_file = locate_template( array ( 'author.php' ) ) ) {
                            $template_path = $theme_file;
                        } else {
                            $template_path = VENDOR_BASE_PATH . '/public/vendor-templates/profile/author.php';
                        }
                    }
        }

    Hopefully someone can shade some light on this.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can try like following. Your concept of fetching global $post is wrong I guess. May be you are trying to get user ID of current logged in user rather than author of the post.

    add_filter( 'template_include', 'abc', 100 );
    function abc( $template_path ) {
    	$current_author = wp_get_current_user();
    	$cur_role = $current_author->roles[0];
    	if ( 'administrator' === $cur_role ) {
    		$template_path = locate_template( array ( 'administrator.php' ) );
    	}
    	return $template_path;
    }
    Thread Starter saurav.rox

    (@sauravrox)

    Thanks for the reply. But may be this code will be useful for logged-in users.

    Ok, then you can fetch user like this.

    if ( is_author() ) {
    	$author = get_user_by( 'slug', get_query_var( 'author_name' ) );
    	$role = $author->roles[0];
    	if ( 'administrator' === $role ) {
    		// Fetch admin template
    	}
    }
    Thread Starter saurav.rox

    (@sauravrox)

    Thank you. Now, it seems to work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom author page for custom role’ is closed to new replies.