How to put Wordpress Profile Meta in different pages? - php

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.

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 do you check the category of the next/previous post?

I'm working on a wordpress site with several categories, where each of those categories have posts. Example: I have "Work" and "Careers" as two categories.
When you click on a "Work" post, at the top of the page there is a "Previous / Next" button to display the next or previous post with the category "Work". Right now, the code is set up like this:
<div class="campaign__previous l-campaign__previous">
<?php previous_post_link('%link','Previous Campaign', TRUE); ?>
</div>
<span>/</span>
<div class="campaign__next l-campaign__next">
<?php next_post_link('%link','Next Campaign', TRUE); ?>
</div>
The wordpress function next_post_link('%link','LINK TEXT', TRUE) automatically doesn't display the link if there are no more posts in that category. That's great, but I also want to not display the
<span>/</span>
if there are no more posts in that category. How do I check this myself? I have tried:
if( next_post_link('%link','Next Campaign', TRUE) ) {
echo '<span>/</span>';
}
This did not work. Any ideas?
For WordPress functions that print something to the screen automatically, there is usually a version with the prefix get_ that will simply return the value. I don't have the ability to test this now but I assume the following might work or at least get you close:
if( get_next_post_link('%link','Next Campaign', TRUE) == "" )
// No More
else
// More to Come
Now there definitely seems like there is a better way to do this but this was just the first thing I thought of when I read your code.
https://developer.wordpress.org/reference/functions/get_next_post_link/

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

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.

How to make and exception using "if" in a wordpress php page

I am using a wordpress theme that has a specific layout feature for the blog template anytime "category" is used.
The blog template displays the date in a ribbon graphic and there is additional control to turn it on or off through an additional "epanel" in wordpress.
I have 2 categories (Blog and Services). I would like to remove the DATE feature from being display only in the "Services" category.
It is added like this :
<?php if ( get_option('sky_postinfo1') ) { ?>
<div class="post-date">
sky_postinfo2 would mean that is off.
Each page has an H1 that echos the page title like this:
<h1 id="page-title"><?php echo esc_html( $et_page_title ); ?></h1>
Is there a way to write an exception to remove this only for the "Services" page?
Something like :
<?php
if {
( $et_page_title = ('Services') get_option('sky_postinfo1');
} else get_option('sky_postinfo2');
?>
I am sure I absolutely butchered the last part, but I am looking for direction if possible. Any help greatly appreciated. Is doing it this way practical or am I way of base?
mhhhh, assuming the $et_page_title variable has this value on this page...
<?php if ( get_option('sky_postinfo1') && $et_page_title != 'Services') { ?>
<div class="post-date">

Categories