How to display content based on post author's user role? - php

Most of the code I can find are about specific content being showed to the logged in user based on their role.
I want to do the opposite.
This is basically what I want to do:
If author has "Subscriber" role, show some html A. If author has "Customer" role, show html B.
I think this should be possible but I am not sure where to begin.
Here's the snippet I am using but not working so might have error somewhere?
<?php $current_author_roles = get_the_author_meta('roles');?>
<?php if ( in_array('Customer', $current_author_roles) ) { ?>
//do something
<?php } else { ?>
//do something else
<?php } ?>

Use a conditional. For example:
$current_author_roles = get_the_author_meta('roles');
if ( in_array('Customer', $current_author_roles) ) {
// do something
}
You can read more about get_the_author_meta() in the Codex.

Related

Home Template Loop customization - should fetch content based on post template

In the theme there are two files:
single.php
f-template.php → For default post type. It has different designs.
Home.php will fetch first 10 posts, and they can be among any of the above templates.
But, what is needed is when the content on home.php is coming from f-template.php
then these two things should be implemented on home.php
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
and
<script>
(function($) {
// do all your $ based jquery in here.
$(function() {
$('p.class2').prepend('<img src="http:/sample.com/img/folder.svg" width="50" height="25" alt="">');
});
})(jQuery);
</script>
I tried this:
if( is_page_template( 'f-template.php' ) ) {
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
}
the_content();
}
But this didn't work. actually it is flawed because the template that we are dealing is home.php.
So do we have any solution to achive what we wanted to achieve?
Correct me if I'm wrong but it sounds like you want to display a loop with posts where some posts have a different design depending on the Page Template you have selected for it.
You can check which template is being used with the get_page_template_slug() function. Use it inside the loop along with the ID of the post.
// checks if there are any posts that match the query
if (have_posts()) :
// If there are posts matching the query then start the loop
while ( have_posts() ) : the_post();
// Assign postId from within the loop
$postId = the_ID();
if( get_page_template_slug($postId) === 'f-template' ) {
// Display what you want to see when f-template is selected.
} else {
// Display what you want to see by default if no condition is met.
}
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
echo '<p>Sorry no posts matched your criteria.</p>';
endif;
The is_page_template() function won't work because it will check what the page template for the current page in the Main Query is.
It all depends on your use case but personally I would have added an extra field using Advanced Custom Fields for this effect.
Good luck!

Wordpress query users and display as taxonomy

I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.

How to put Wordpress Profile Meta in different pages?

I need three different pages showing different parts of one user profile on wordpress. Author.php is a standard feature and is great is loads on my website www.website.com/site1/user/nicename. However I want to have one profile which is sorted, but display the profile over three different areas of the website.
PLEASE NOTE: Username in this example would be joe-blogs1990 what ever the user decides, the nicename will be formatted joebloggs, first and surname combined for nice permalinks on wordpress.
www.website.com/site1/author/username
www.website.com/site2/agency/nicename
www.website.com/site3/meet-the-team/nicename
I simply wish to call user profile information in three different places, but they will have different information on each one, I’ve managed to create a place where everything is edited in one place, however to display information based on the URL is slightly bewildered me.
Here is what I have so far, but nothing is loading on any of the pages what im trying to achieve above.
<?php get_header(); ?>
<?php $blogname = get_bloginfo('name'); if ( $blogname == 'site1' ) {
get_the_author_meta('description');
} ?>
<?php $blogname = get_bloginfo('name'); if ( $blogname == 'site2' ) {
get_the_author_meta('first_name');
get_the_author_meta('last_name');
} ?>
<?php $blogname = get_bloginfo('name'); if ( $blogname == 'site3' ) {
get_the_author_meta('facebook_profile');
get_the_author_meta('twitter_profile');
} ?>
<?php get_footer(); ?>
Try adding echo in Front of the get_the_whatever methods. All methods prefixed with get_ just return strings; in order to see something you have to echo it.

Change page content based on condition Wordpress

I would like to change to homepage of a wordpress website depending on the role of the user who is logged in.
For the simple case ( i.e. whether a user is logged in or not ) I have tried using the function is_user_logged_in()
the code is as below:
if(!is_user_logged_in()){
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
}else{
echo "you are logged in";
}
But the problem is that it changes the content in the each and every page. I would like to do that for a particular page only.. ( i.e. homepage ). How do i do that? Any help would be greatly appreciated.
To do it you would have to add another condition(s), e.g. for the homepage:
if(!is_user_logged_in() && is_home()){
...
}
And similarly for the other type of the content: is_page(), is_category(), is_author(), etc. Check out the docs here.
check for the Id of the page and compare it to your wanted specific page
also you could set a category for the pages you want to alter and check for that

Whats the url that displays all posts in wordpress?

I simply want a link to my blog archives. They are just normal posts, but I cannot seem to find the right url for them to add to my menu.
right url for them to add to my menu.
http://yourwebsite.com/?post_type=post
You don't have to necessarily use a category to every post.
Actually the file to list all posts of any category and date is the index.php. You just write 'the loop' as told on codex.
So if you changed your index.php to make it as a fancy page and not the post list only, now you're trying to create another page to do that for you.
So, if you follow me, you're doing this the wrong way. What you should do is to create a separate page and assign it as the home page of your blog. It would then free the index.php file for you to use it as the blog list, as it is by default.
Assuming that you did it the correct way (as mentioned by Guilherme), you should have a page designated as the blog list page.
The URL for the blog list if using the page 'My Blog' to display posts and pretty links for the url should be something like http://mywebsite.com/my-blog.
<?php if ( is_front_page() && is_home() ) {
// Default homepage
echo "Default homepage";
} elseif ( is_front_page()){
echo "Static homepage";
// Static homepage
} elseif ( is_home()){
echo "Blog page";
// Blog page
} elseif ( is_page( 'cart' ) || is_cart()){
echo "cart";
// Blog page
} elseif (is_single()){
echo "is_single";
// Blog page
} elseif (is_product_category()){
echo "is_product_category";
}
else {
echo "Everything else";
// Everything else
} ?>

Categories