Horje
Need help with a PHP IF statement - Wordpress Solution
I've got a PHP code section that is displaying a "profile_pic" image that is uploaded to a folder on my site (not the WP database). I need to add an if statement that says to only show the post if the $profile_pic exists. And if that doesn't exist, to skip to the next post. If that's not possible, then replacing the missing image with a default pic would work. So the line that has the echo $profile_pic; part is the part I want to say "if $profile_pic exists then" but "if it doesn't show this: https://mysites.com/temp.png" Here's the working code:
<?php

							global $post;

							if ( post_type_exists( 'team_member' ) ) {

								 $profile_query = new WP_Query( array(
										 'post_type' => 'team_member',
										 'orderby' => 'rand',
										 'posts_per_page' => -1
								 ) );

								 if ( $profile_query->have_posts() ) {
										$random_int = rand( 0, $profile_query->post_count - 1 );
										$post = $profile_query->posts[$random_int];
										setup_postdata( $post );

										//$profile_cover_pic = get_post_meta(get_the_ID(), '_profile_cover_pic', true);
										$profile_pic = get_post_meta(get_the_ID(), '_profile_pic', true);
										$pic_path = "/wp-content/uploads/profile-images/";
										//$profile_cover_pic = $pic_path.$profile_cover_pic;
										$profile_pic = $pic_path.$profile_pic;

										echo '<a rel="nofollow" href="';
										the_permalink();
										echo '"><div class="find-og-circle-one"><img src="';
										echo $profile_pic;
										echo '" class="tooltips" title="';
										the_field('first_name');
										echo ' ';
										the_field('last_name');
										echo '" alt="';
										the_field('first_name');
										echo ' ';
										the_field('last_name');
										echo '"></div></a>';
								 }
								 // Restore original post data
								 wp_reset_postdata();
							}

						?>


Solution - 1

try the following code:

<?php

global $post;

if ( post_type_exists( 'team_member' ) ) {

//default pic
$profile_pic = 'https://domain.com/pic.png';
$pic_path = "/wp-content/uploads/profile-images/";


$profile_query = new WP_Query( array(
'post_type' => 'team_member',
'orderby' => 'rand',
'posts_per_page' => -1
) );

if ( $profile_query->have_posts() ) {	
$random_int = rand( 0, $profile_query->post_count - 1 );
$post = $profile_query->posts[$random_int];
setup_postdata( $post );

//$profile_cover_pic = get_post_meta(get_the_ID(), '_profile_cover_pic', true);
$get_pic = get_post_meta(get_the_ID(), '_profile_pic', true);
if(!empty($get_pic)){	
	//$profile_cover_pic = $pic_path.$profile_cover_pic;
	$profile_pic = $pic_path.$get_pic;
}

echo '<a rel="nofollow" href="';
the_permalink();
echo '"><div class="find-og-circle-one"><img src="';
echo $profile_pic;
echo '" class="tooltips" title="';
the_field('first_name');
echo ' ';
the_field('last_name');
echo '" alt="';
the_field('first_name');
echo ' ';
the_field('last_name');
echo '"></div></a>';
}

// Restore original post data
wp_reset_postdata();
}


Solution - 2

Is it not something like below?


if (!empty($profile_pic)) {
echo $profile_pic;
} else {
echo $default_profile_pic;
}





Wordpress

Related
There are no options in my dashboard on the left where the pages and p - Wordpress Solution There are no options in my dashboard on the left where the pages and p - Wordpress Solution
Need to auto select authors on import - Wordpress Solution Need to auto select authors on import - Wordpress Solution
Merging post data from post ID on edit screen - Wordpress Solution Merging post data from post ID on edit screen - Wordpress Solution
Shortcode not working on AJAX call - Wordpress Solution Shortcode not working on AJAX call - Wordpress Solution
Extent the_post_thumbnail() function with a third parameter - Wordpress Solution Extent the_post_thumbnail() function with a third parameter - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
19