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

Need help with a PHP IF statement WordPress

  • SOLVED

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 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();
}

?>

Answers (2)

2020-11-05

timDesain Nanang answers:

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 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();
}


Kyler Boudreau comments:

Beautiful. That worked!!! Thank you.

2020-11-05

Reigel Gallarde answers:

Is it not something like below?

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


Kyler Boudreau comments:

Hey Reigel,

Thanks man. I tried your code, but not sure where to define the default image URL. Could you expound on that? Thanks!