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

Need custom post type loop with if then statements. WordPress

  • SOLVED

Hey, so here's a normal loop I have for a custom post type:


<?php

$loop = new WP_Query( array( 'post_type' => 'team_member', 'posts_per_page' => 40 ) );
while ( $loop->have_posts() ) : $loop->the_post();

echo '<div class="team-profile">';
echo '"><img src="';
the_field('profile_pic');
echo '" alt="';
the_title();
echo '"></div>';
echo '<div class="team-bio">';
the_content();
echo '</div>';
endwhile;
?>


But I need to know how to loop in a custom post type and use IF and THEN statements. For example, in the above CPT, I have two additional fields not shown. One for a website URL and one for a Facebook URL. I don't want to show anything if the fields aren't filled out, but if they are, I want to show and icon linking to the link they gave.

I know how to do this on a normal WordPress PHP template page, but not inside a custom post type loop. Thanks!!!

Answers (5)

2015-04-01

Hariprasad Vijayan answers:

Hello,

Do you want to write a condition for displaying custom field? If custom field for facebook_url and website_url isn't empty want to display it?

the code would be like this


<?php
$loop = new WP_Query( array( 'post_type' => 'team_member', 'posts_per_page' => 40 ) );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="team-profile">';
echo '"><img src="';
the_field('profile_pic');
echo '" alt="';
the_title();
echo '"></div>';
echo '<div class="team-bio">';
the_content();
echo '</div>';

if(get_field('website_url'))
{
echo '<a href="'.get_field('website_url').'">Website URL</a>';
}
if(get_field('facebook_url'))
{
echo '<a href="'.get_field('facebook_url').'">Website URL</a>';
}
endwhile;
?>

Let me know if you need help.

Regards,
Hariprasad


Kyler Boudreau comments:

Harisprasad,

Your solution wins out of all of these good suggestions. I have one question before voting - I need to do the following to show an icon for the link:


if(get_field('website_link'))

{

echo '<a href="'.get_field('website_link').'"><img src="';
get_bloginfo('url');
echo '/media/social-facebook.png"></a>';


}


But it's not pulling the blog URL. Any ideas on that? THANKS.


Hariprasad Vijayan comments:

Hi,

get_bloginfo('url') doesn't echo the url on page. You have to use it like

echo get_bloginfo('url')

Try following one

if(get_field('website_link'))
{
echo '<a href="'.get_field('website_link').'"><img src="' . get_bloginfo('url') . '/media/social-facebook.png"></a>';
}


Let me know if you need help.

Thanks.


Kyler Boudreau comments:

Sweet! That's it. You rock dude. Voting now.

2015-04-01

Luis Abarca answers:

Take the $img_src example as a template to check if a field is not empty.



<?php

$loop = new WP_Query( array( 'post_type' => 'team_member', 'posts_per_page' => 40 ) );

if ($loop->have_posts()) {

while ( $loop->have_posts() ) : $loop->the_post();

$img_src = the_field('profile_pic');

echo '<div class="team-profile">';


if (!empty($img_src)) {
printf('<img src="%s" alt="%s">', $img_src, get_the_title());
}

echo '</div>';

echo '<div class="team-bio">';
the_content();
echo '</div>';

endwhile;

} // endif

?>

2015-04-01

Romel Apuya answers:

are these website URL and Facebook URL custom fields?


Romel Apuya comments:

if its a custom field

use


<?php
$loop = new WP_Query( array( 'post_type' => 'team_member', 'posts_per_page' => 40 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$id = get_the_ID();
$fb_url = get_post_meta($id, 'facebook_url', true);
$website_url = get_post_meta($id, 'website_url', true);
echo '<div class="team-profile">';
echo '"><img src="';
the_field('profile_pic');
echo '" alt="';
the_title();
echo '"></div>';
echo '<div class="team-bio">';
the_content();
echo '</div>';

if($website_url !='')
{
echo '<a href="'.$website_url.'">Website URL</a>';
}
if($fb_url !='')
{
echo '<a href="'.$fb_url.'">Website URL</a>';
}
endwhile;
?>

2015-04-01

timDesain Nanang answers:

Hi, try this code:
change website_url and facebook_url as the Field Name on ACF

$args = array(
'post_type' => 'team_member',
'post_status' => 'publish',
'posts_per_page' => 40,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'website_url',
'value' => '',
'compare' => '!=',
),
array(
'key' => 'facebook_url',
'value' => '',
'compare' => '!=',
),
),
);
$loop = new WP_Query( $args );


This query will show all team_member cpt <strong>that have website_url and facebook_url</strong>,
and works with wp pagination.
info: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters



but, if you want to show <strong>all team_member with if conditional</strong>:

$args = array(
'post_type' => 'team_member',
'post_status' => 'publish',
'posts_per_page' => 40,
);

$loop = new WP_Query( $args );

if($loop->have_posts()) :
while ( $loop->have_posts() ) : $loop->the_post();
$website_url = get_field('website_url');
$facebook_url = get_field('facebook_url');

the_title();

if(!empty($website_url))
echo '<a href="'.$website_url.'"><img src="website_icon" /></a>';

if(!empty($facebook_url))
echo '<a href="'.$facebook_url.'"><img src="facebook_icon" /></a>';

the_excerpt();

endwhile;
wp_reset_postdata();
endif;


2015-04-01

Arnav Joy answers:

Please try this code , so when website url and fb url is filled then it will show profile otherwise not , let me know if it is ok.


<?php

$loop = new WP_Query( array( 'post_type' => 'team_member', 'posts_per_page' => 40 ) );

while ( $loop->have_posts() ) : $loop->the_post();

if( !get_field('website_url') && !get_field('facebook_url') )
continue;

echo '<div class="team-profile">';

echo '"><img src="';

the_field('profile_pic');

echo '" alt="';

the_title();

echo '"></div>';

echo '<div class="team-bio">';

the_content();

echo '</div>';

endwhile;
?>