Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Different elements to author page - by role? WordPress

  • SOLVED

Hi there,
I run a community site. On the site we have three role types. Admins & Editors (who are staff), Subscribers (who are normal members) and a custom level called Premium (who are basically advertisers).

I would like to do two things depending on the users role.

1) If an article is added by either Staff or Advertisers I would like to show an icon over it's thumbnail in the archive. Saying either "sponsored" or "staff pick". As well as at the top of the single page.
I need help with how to form the "if" statement to select the different roles, and also how to place an image OVER a thumbnail.

Here is an example of the code used to show thumbnails in the archives:

/*** reader cat thumbs***/
/* Thumbs (actually post images) for singles */
function reader_cat_thumbs() {
if ( has_post_thumbnail() ) {
echo '<div class="post_wp_thumb" >';
the_post_thumbnail('single-post-thumbnail');
echo '</div>';
} else {
echo '<div class="post_wp_thumb_3col" style="width: 180px; border: 1px solid rgb(239, 239, 239); text-align: center; overflow:hidden;">';
$image_link = get_first_image();
if ($image_link != "external"){
echo '<img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=180&w=180&zc=1" alt="">';
} else {
echo '<img src="'. get_first_image_2() .'" height="180" alt="">';
}

echo '</div>';
}
?><div style="overflow: hidden; line-height: 15px; height: 30px; text-align: center; font-size: 12px;"><?php the_excerpt(); ?></div>
<?php
}
add_action('thesis_hook_after_teaser_headline', 'reader_cat_thumbs');


Screen shot of archive attached.

2) On the author pages I have added an Author "about" box at the top of the archive, I would like to be able to show different content for each. So again, it's the if statement that I need help with.



I am using Thesis theme, so all changes are made via hooks in the custom-functions file.

I hope that all makes sense.

Thanks in advance.

Kate

Answers (2)

2011-10-29

Luis Abarca answers:

To get the user role, get the author ID


/*** reader cat thumbs***/
/* Thumbs (actually post images) for singles */
function reader_cat_thumbs()
{
<strong>
$author_id = get_ the_author_meta('ID');

$user_data = get_userdata($author_id);

// current role
$user = new WP_User( $author_id );
$current_role = $user->roles[0];
$current_level=$user->user_level;
</strong>

if ( has_post_thumbnail() ) {
echo '<div class="post_wp_thumb" >';
the_post_thumbnail('single-post-thumbnail');
<strong>
switch ( $current_role ) {
case 'suscribers':
$role_image = 'your-subscriber-image.jpg';
break;

case 'editor':
$role_image = 'your-editor-image.jpg';
break;

case 'admin':
$role_image = 'your-admin-image.jpg';
break;

case 'premium':
$role_image = 'your-premium-image.jpg';
break;
}

echo '<div style="position: relative; top: 0; left: 0">';
echo '<img src="' . $role_image . '">';
echo '</div>';
</strong>
echo '</div>';
} else {
echo '<div class="post_wp_thumb_3col" style="width: 180px; border: 1px solid rgb(239, 239, 239); text-align: center; overflow:hidden;">';

$image_link = get_first_image();

if ($image_link != "external"){
echo '<img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=180&w=180&zc=1" alt="">';
} else {
echo '<img src="'. get_first_image_2() .'" height="180" alt="">';
}

echo '</div>';
}
?><div style="overflow: hidden; line-height: 15px; height: 30px; text-align: center; font-size: 12px;"><?php the_excerpt(); ?></div>
<?php
}

add_action('thesis_hook_after_teaser_headline', 'reader_cat_thumbs');



For the author page, its the same process, get the user ID, get the user data and look for the role and show the appropiate information.

2011-11-01

Sébastien | French WordpressDesigner answers:

The code of Luis is ok but doesn't work on my site. And in your site ?

The code of Luis work if i replace
get_ the_author_meta('ID') by get_the_author_meta('ID')
(blank after get_)

and if i replace $author_id = get_ the_author_meta('ID');
by
global $current_user;
get_currentuserinfo();
$author_id = $current_user->ID;

so, the code works like that :


and if i replace :

case 'admin':
by
case 'administrator':



function reader_cat_thumbs()
{
global $current_user;
get_currentuserinfo();
$author_id = $current_user->ID;

$user_data = get_userdata($author_id);
// current role
$user = new WP_User( $author_id );
$current_role = $user->roles[0];
$current_level=$user->user_level;
if ( has_post_thumbnail() ) {
echo '<div class="post_wp_thumb" >';
the_post_thumbnail('single-post-thumbnail');
switch ( $current_role ) {
case 'suscribers':
$role_image = 'your-subscriber-image.jpg';
break;
case 'editor':
$role_image = 'your-editor-image.jpg';
break;
case 'administrator':
$role_image = 'your-admin-image.jpg';
break;
case 'premium':
$role_image = 'your-premium-image.jpg';
break;
}
echo '<div style="position: relative; top: 0; left: 0">';
echo '<img src="' . $role_image . '">';
echo '</div>';
echo '</div>';
} else {
echo '<div class="post_wp_thumb_3col" style="width: 180px; border: 1px solid rgb(239, 239, 239); text-align: center; overflow:hidden;">';
$image_link = get_first_image();
if ($image_link != "external"){
echo '<img src="/wp-content/themes/thesis_18/custom/scripts/timthumb.php?src='. $image_link .'&h=180&w=180&zc=1" alt="">';
} else {
echo '<img src="'. get_first_image_2() .'" height="180" alt="">';
}
echo '</div>';
}
?><div style="overflow: hidden; line-height: 15px; height: 30px; text-align: center; font-size: 12px;"><?php the_excerpt(); ?></div>
<?php
}
add_action('thesis_hook_after_teaser_headline', 'reader_cat_thumbs');



for the "about" box you can use this code :


function about_box()
{
global $current_user;
get_currentuserinfo();
$author_id = $current_user->ID;

//$author_id = get_the_author_meta('ID');
$user_data = get_userdata($author_id);
// current role
$user = new WP_User( $author_id );
$current_role = $user->roles[0];
$current_level=$user->user_level;

echo '<div id="about_box">';
switch ( $current_role ) {
case 'suscribers':
echo $user_data->display_name.' is a subscriber'."<BR>";

echo "ID = ".$user_data->ID."<BR>";
echo "login = ".$user_data->user_login."<BR>";
//echo "pass = ".$user_data->user_pass."<BR>";
//echo "nicename = ".$user_data->user_nicename."<BR>";
//echo "email = ".$user_data->user_email."<BR>";
//echo "url = ".$user_data->user_url."<BR>";
//echo "user registered = ".$user_data->user_registered."<BR>";
//echo "display name = ".$user_data->display_name."<BR>";

break;
case 'editor':
echo $user_data->display_name.' is an editor'."<BR>";

echo "ID = ".$user_data->ID."<BR>";
echo "login = ".$user_data->user_login."<BR>";
echo "pass = ".$user_data->user_pass."<BR>";
echo "nicename = ".$user_data->user_nicename."<BR>";
//echo "email = ".$user_data->user_email."<BR>";
//echo "url = ".$user_data->user_url."<BR>";
//echo "user registered = ".$user_data->user_registered."<BR>";
//echo "display name = ".$user_data->display_name."<BR>";
break;
case 'administrator':
echo $user_data->display_name.' is an admin'."<BR>";

echo "ID = ".$user_data->ID."<BR>";
echo "login = ".$user_data->user_login."<BR>";
echo "pass = ".$user_data->user_pass."<BR>";
echo "nicename = ".$user_data->user_nicename."<BR>";
echo "email = ".$user_data->user_email."<BR>";
echo "url = ".$user_data->user_url."<BR>";
//echo "user registered = ".$user_data->user_registered."<BR>";
//echo "display name = ".$user_data->display_name."<BR>";
break;
case 'premium':
echo $user_data->display_name.' is a premium'."<BR>";

echo "ID = ".$user_data->ID."<BR>";
echo "login = ".$user_data->user_login."<BR>";
echo "pass = ".$user_data->user_pass."<BR>";
echo "nicename = ".$user_data->user_nicename."<BR>";
echo "email = ".$user_data->user_email."<BR>";
echo "url = ".$user_data->user_url."<BR>";
echo "user registered = ".$user_data->user_registered."<BR>";
echo "display name = ".$user_data->display_name."<BR>";
break;
}
echo '</div>';
}


where you want display your box, use this code : about_box();