I'm trying to find a way to display a logged-in user's individual avatar/gravatar (based on their email address) on one of my pages.
So far, I've only been able to find methods to display the page author's avatar, or the same avatar for all users.
I gather that I need to add get_avatar to my functions.php, using something like this:
< a href = "<?php echo get_author_posts_url($post->post_author); ?>"
title = "<?php the_author_meta( 'display_name', $post->post_author ); ?>" >
<? php
if (get_the_author_meta('user_custom_avatar', $post - > post_author) != '') { ?>
< img src = "<?php the_author_meta( 'user_custom_avatar', $post->post_author ); ?>"
alt = "" / >
<? php echo $curauth - > display_name; ?>
<? php
} else {
$current_user = wp_get_current_user();
echo get_avatar($current_user - > user_email, $post - > post_author), '80');
} ?>
< /a>
But I also need to know how to display it in the content of my page/post. Is there something I can use include when editing the page using Wordpress?
Any help would be greatly appreciated!
In any page or post just call it like this
$custom_avatar = get_user_meta($user_id, 'user_custom_avatar', true);
Related
Inside a template part file there's a variable I'd like to be able to use in my page templates. Page templates are in the theme directory, and the template part file are in a sub-directory called template parts.
My template part file is called 'roster-get-annual-data.php', and the variable is the title of a custom post type. How can I make this title variable available in my page template?
Inside my template file (roster-get-annual-data.php) - within a wp_query:
if ($rosterQuery->have_posts()) {
while ($rosterQuery->have_posts()) {
$rosterQuery->the_post();
$post_title = $post->post_title;
set_query_var('post_title', $post_title);
?>
<h1><?php echo $post_title; ?></h1>
<div class="teams-wrapper">
<?php
$teams = get_field('single_rosters');
foreach ($teams as $post) {
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post);
// set the individual roster's ID
$rosterId = $post->ID;
// get the Class Title
if (!empty(get_field('class', $rosterId)) && !empty(get_field('grad_year', $rosterId))) {
$classTitle = ucwords(get_field('class', $rosterId)) . ' - Class of ' . get_field('grad_year', $rosterId);
} else {
$classTitle = get_the_title();
}
?>
<h2><?php echo $classTitle; ?></h2>
<?php
// create empty $players array
$players = array();
// loop through the players acf repeater
if (have_rows('players', $rosterId)) {
?>
<?php
while (have_rows('players', $rosterId)) {
the_row();
// place players into an array
$players[] = get_sub_field('name');
}
?>
<?php
}
// display the PLAYERS NAMES
?>
<p class="players"><?php echo implode(', ', $players); ?></p>
<?php
}
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata();
?>
</div>;
?>
And then in my page template:
$post_title = get_query_var('post_title');
get_template_part('template-parts/roster-get-annual', 'data');
I thought this would give me just the $post_title variable, but instead it outputs the entire template without the $post_title variable.
It´s not 100% clear to me what you want to achieve. But if you want pass args to a template you can do it with the 3rd parameter:
get_template_part('template-part', 'name', array('customkey' => 'value') );
see doc:
https://developer.wordpress.org/reference/functions/get_template_part/
To begin with, I think this is a longshot but hopefully there is someone out there that can help.
To explain the current situation, at the moment I have a custom plugin that grabs various bits of information about a user and their 4 most recent posts.
I am also using the WPBook plugin (so this is on facebook and just just a typical wordpress site)
Ok, so here is my code that is grabbing the 4 most recent posts for a user:
// The Query
query_posts('posts_per_page=4&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
?>
<div class="oe-grid-box">
<a href="<?php the_permalink() ?>" <?php the_title()?></a>
<div class="oe-grid-box-content">
<?php echo the_content( '...' ); ?>
</div>
<div class="oe-grid-pic">
<a href="<?php the_permalink() ?>">
<span class="<?php echo strtolower($category); ?>"></span>
</a>
<?php echo $image; ?>
</div>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
<div id="load-more">Load More</div>
I tried following this tutorial but instead of a separate plugin, placing the code in my existing plugin but now my page won't load:
http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/
Ideally, want I want to is when the load more button is clicked, fetch 4 more posts and show them.
If anybody could help I'd really appreciate it.
UPDATE
Ok,
so far I've added in my jQuery to call the php file which should hopefully return posts but it doesn't work.
I am thinking that it's because it doesn't know what the WordPress functions are?
If I put a simple echo in my load more script, the jQuery success function shows an alert to say it worked, but if I start doing WordPress stuff I get an internal server error, this is the code in the load-more.php file:
// load wordpress into template
$path = $_SERVER['DOCUMENT_ROOT'];
define('WP_USE_THEMES', false);
require($path .'/wp-load.php');
$blogger_id = $_GET['id'];
$firstname = $_GET['firstname'];
$surname = $_GET['surname'];
$posts = $_GET['posts'] + 4;
$category = $_GET['category'];
$image = $_GET['image'];
// The Query
query_posts('paged='.get_query_var('paged').'&posts_per_page=' . $posts . '&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// then the same loop as in my page that is making the ajax call.
After a lot of effort, I found the answer, here is a solution for those stuck in the same position.
This goes in your plugin page where you are getting posts for a user etc.
<script type="text/javascript">
$(document).ready(function() {
posts = 8;
author_posts = parseInt(<?php echo $author_posts; ?>);
$("#link_selector").click(function() {
// alert('posts - ' + posts + 'author posts - ' + author_posts);
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
else {
var category = '<?php echo strtolower($category); ?>';
var id = '<?php echo $blogger_id; ?>';
var firstname = '<?php echo $firstname; ?>';
var surname = '<?php echo $surname; ?>';
// alert(posts + category + id + firstname + surname);
$.ajax({
type: "GET",
url: "/wordpress/wp-admin/admin-ajax.php",
dataType: 'html',
data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}),
success: function(data){
$('#ajax_results_container').hide().fadeIn('slow').html(data);
posts = posts + 4;
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
}
});
}
});
});
</script>
Then in your theme's functions.php, put your function to do your ajax request:
function loadMore() {
$blogger_id = $_GET['id'];
// get your $_GET variables sorted out
// setup your query to get what you want
query_posts('posts_per_page=' . $posts . '&author='.$blogger_id);
// initialsise your output
$output = '';
// the Loop
while (have_posts()) : the_post();
// define the structure of your posts markup
endwhile;
// Reset Query
wp_reset_query();
die($output);
}
Then, just after the closing of your function, put in the action hooks
add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed
That's it!
On my site when a user is registering there is an option to pick from an additional option that pears their account up with a non-profit. Once the user has registered and viewing specific pages of the site I want the site to be tailored to them using php that grabs their meta info. For this I will echo a button that tailors the front-end based on what meta value they have selected when registering.
If they have no meta key, then nothing is shown.
Here is my code attempt, but does not work!
<?php global $current_user;
get_currentuserinfo(); //wordpress global variable to fetch logged in user info
$userID = $current_user->ID; //logged in user's ID
$havemeta1 = get_user_meta($userID,'nch',true); //stores the value of logged in user's meta data for 'National Coalition for the homeless'
$havemeta2 = get_user_meta($userID,'rotary-international',true); //stores the value of logged in user's meta data for 'Rotary International'
$havemeta3 = get_user_meta($userID,'khan-academy',true); //stores the value of logged in user's meta data for 'Khan Academy'
$havemeta4 = get_user_meta($userID,'wwf',true); //stores the value of logged in user's meta data for 'World Wildlife Fund (WWF)'
$havemeta5 = get_user_meta($userID,'bcrf',true); //stores the value of logged in user's meta data for 'The Breast Cancer Research Foundation'
?>
<!--add if statement to figure out what button to show to logged in user-->
<?php if ($havemeta1) { ?>
<div <p>nch</p> class="Button1"></div>
<?php } elseif ($havemeta2) { ?>
<div <p>rotary-international</p>class="Button2"></div>
<?php } elseif ($havemeta3) { ?>
<div <p>khan-academy</p>class="Button3"></div>
<?php } elseif ($havemeta4) { ?>
<div <p>wwf</p>class="Button4"></div>
<?php } elseif ($havemeta5) { ?>
<div <p>bcrf</p>class="Button5"></div>
<?php } else { ?>
<div><p>None - No Matching Affiliation</p></div>
<?php }?>
-----------------------New Code----------------------
This allows me to see what the affiliation variable is pulling for the user
The result is this: 'User Affiliation: khan-academy'
<?php global $current_user;
get_currentuserinfo();
echo 'User Affiliation: ' . $current_user->affiliation . "\n";
?>
can you pass the meta into a session var, ie $_SESSION['havemeta5'];
get_user_meta is set up wrong. Try this:
$havemeta1 = get_user_meta( $userID, 'nch', true );
...and so on. You need to pass the $userID as the first parameter of get_user_meta() - not $affiliation.
update
If your user meta is 'affiliation' (which allows you to call $current_user->affiliation), you can do something like this:
<?php
global $current_user;
get_currentuserinfo();
$userID = $current_user->ID;
$affiliation = get_user_meta( $userID, 'affiliation', false );
$affiliation = $affiliation[0];
if( 'nch' == $affiliation ){
print '<div class="Button1"><p>nch</p></div>';
} elseif( 'rotary-international' == $affiliation ){
print '<div class="Button2"><p>Rotary International</p></div>';
} elseif( 'khan-academy' == $affiliation ){
print '<div class="Button3"><p>Khan Academy</p></div>';
} elseif( 'wwf' == $affiliation ){
print '<div class="Button4"><p>wwf</p></div>';
} elseif( 'bcrf' == $affiliation ){
print '<div class="Button5"><p>bcrf</p></div>';
} else {
print '<div><p>None - no matching affiliation</p></div>';
print '<div>User ID: '.$userID.', Affiliation: '.$affiliation.'</div>';
}
I am working on a wordpress plugin. In plugin, a user first add a slider and then add images of relevant slider.
First i make this with shortcode. User enter the short name of the slider in the shortcode and images slides of relevant slider like this [foo_slider slider=slider_one] or [foo_slider slider=slider_two].
Now i "also" want the snippet, that user can add snippet else shortcode in the code like this echo wp_foo_slider(slider_two). But i dont get that.
Please guide me, how can i do this.
Here is my Code That works for shortcode:
<?php
function wp_foo_sliders($atts) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
ob_start();
extract(shortcode_atts(array(
'slider' => '',
), $atts));
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
return ob_get_clean();
}
add_shortcode("foo_slider", "wp_foo_sliders");
?>
I also try this by my own <?php echo wp_foo_sliders("slider_two") ?> or <?php echo wp_foo_sliders(slider_two) ?>, in code and when i refresh the browser only <div class="foo_main_slider"> </div> appears and no images show.
Edit: I want that user can use short code <?php echo do_shortcode('[foo_slider slider=slider_one]'); ?> or user can use snippet <?php echo wp_foo_sliders("slider_two") ?>, only shortcode is working, snippet not work.
What i make mistake please help me.
When you call the shortcode function directly, you are passing a string to it. When you use the shortcode way WordPress will convert the arguments into an associative array.
Try refactoring your code
if( is_array( $atts ) ) {
//Called using shortcode so $atts is an array
extract(shortcode_atts(array(
'slider' => '',
), $atts));
} else {
//Function called directly so $atts is a string
$slider = $atts;
}
So i get my solution by my own.
I make a new function for this:
<?php
function foo_sliders($foo_short_name) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
ob_start();
return $foo_short_name;
return ob_get_clean();
}
?>
And in theme code:
<?php foo_sliders(short_name) ?>
and its work great
To begin with, I think this is a longshot but hopefully there is someone out there that can help.
To explain the current situation, at the moment I have a custom plugin that grabs various bits of information about a user and their 4 most recent posts.
I am also using the WPBook plugin (so this is on facebook and just just a typical wordpress site)
Ok, so here is my code that is grabbing the 4 most recent posts for a user:
// The Query
query_posts('posts_per_page=4&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
?>
<div class="oe-grid-box">
<a href="<?php the_permalink() ?>" <?php the_title()?></a>
<div class="oe-grid-box-content">
<?php echo the_content( '...' ); ?>
</div>
<div class="oe-grid-pic">
<a href="<?php the_permalink() ?>">
<span class="<?php echo strtolower($category); ?>"></span>
</a>
<?php echo $image; ?>
</div>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
<div id="load-more">Load More</div>
I tried following this tutorial but instead of a separate plugin, placing the code in my existing plugin but now my page won't load:
http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/
Ideally, want I want to is when the load more button is clicked, fetch 4 more posts and show them.
If anybody could help I'd really appreciate it.
UPDATE
Ok,
so far I've added in my jQuery to call the php file which should hopefully return posts but it doesn't work.
I am thinking that it's because it doesn't know what the WordPress functions are?
If I put a simple echo in my load more script, the jQuery success function shows an alert to say it worked, but if I start doing WordPress stuff I get an internal server error, this is the code in the load-more.php file:
// load wordpress into template
$path = $_SERVER['DOCUMENT_ROOT'];
define('WP_USE_THEMES', false);
require($path .'/wp-load.php');
$blogger_id = $_GET['id'];
$firstname = $_GET['firstname'];
$surname = $_GET['surname'];
$posts = $_GET['posts'] + 4;
$category = $_GET['category'];
$image = $_GET['image'];
// The Query
query_posts('paged='.get_query_var('paged').'&posts_per_page=' . $posts . '&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// then the same loop as in my page that is making the ajax call.
After a lot of effort, I found the answer, here is a solution for those stuck in the same position.
This goes in your plugin page where you are getting posts for a user etc.
<script type="text/javascript">
$(document).ready(function() {
posts = 8;
author_posts = parseInt(<?php echo $author_posts; ?>);
$("#link_selector").click(function() {
// alert('posts - ' + posts + 'author posts - ' + author_posts);
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
else {
var category = '<?php echo strtolower($category); ?>';
var id = '<?php echo $blogger_id; ?>';
var firstname = '<?php echo $firstname; ?>';
var surname = '<?php echo $surname; ?>';
// alert(posts + category + id + firstname + surname);
$.ajax({
type: "GET",
url: "/wordpress/wp-admin/admin-ajax.php",
dataType: 'html',
data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}),
success: function(data){
$('#ajax_results_container').hide().fadeIn('slow').html(data);
posts = posts + 4;
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
}
});
}
});
});
</script>
Then in your theme's functions.php, put your function to do your ajax request:
function loadMore() {
$blogger_id = $_GET['id'];
// get your $_GET variables sorted out
// setup your query to get what you want
query_posts('posts_per_page=' . $posts . '&author='.$blogger_id);
// initialsise your output
$output = '';
// the Loop
while (have_posts()) : the_post();
// define the structure of your posts markup
endwhile;
// Reset Query
wp_reset_query();
die($output);
}
Then, just after the closing of your function, put in the action hooks
add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed
That's it!