Reputation: 11
I am trying to query from Custom Post Type and ACF, unable to display image on the front page. Following is my wp_query
<?php
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => '4'
);
$property_loop = new WP_Query( $args );
if ( $property_loop->have_posts() ) :
while ( $property_loop->have_posts() ) : $property_loop->the_post();
// Set variables
$title = get_the_title();
$description = get_the_content();
$property_image2 = get_field('property_image2');
// Output
?>
<div class="property">
<img src="<?php echo $property_image1; ?>" alt="<?php echo $title; ?>">
<h2><?php echo $title; ?></h2>
<img src="<?php echo $property_image1; ?>" alt="property-detail" class="property-detail align-right">
<?php echo $description; ?>
<p><a href="<?php echo $download; ?>" target="_blank" name="Spec Sheet">Download Spec Sheet</a></p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Can anyone assist me?
Upvotes: 1
Views: 2308
Reputation: 72
please working code please try custome field image display for fornt side
<?php
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => '4'
);?>
<?php $recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
<div class="property">
<?php $property_image2 = get_field('property_image2');?>
<img src="<?php echo $property_image2['url'];?>" alt="<?php echo $property_image2['alt'];?>">
<h2><?php the_title(); ?></h2>
<img src="<?php echo $property_image2['url'];?>" alt="<?php echo $property_image2['alt'];?>" class="property-detail align-right">
<?php the_content(); ?>
<p><a href="<?php the_field('your_download_field_name'); ?>" target="_blank" name="Spec Sheet">Download Spec Sheet</a></p>
</div>
<?php wp_reset_postdata(); endwhile;?>
Upvotes: 1
Reputation: 1084
I have seen the answers here and some of them are correct but not optimised.
Change 1: Used the WP_Query as opposed to the none necessary foreach and if statement.
Change 2: The images were pulling in an incorrect alt tag, set the image in the ACF fields page to Image Object and allow it to pull the dynamic alt to that image.
Change 3: No need to use an if statement to check if there are posts, as the query only runs while there are posts.
Change 4: There is no need to define the_content and the_title, instead, they are just called inside the loop.
Change 5: I have added the call for the download link, this will need updating to your field name for the download.
<?php
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => '4'
);?>
<?php $recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
<div class="property">
<?php $property_image2 = get_field('property_image2');?>
<img src="<?php echo $property_image2['url'];?>" alt="<?php echo $property_image2['alt'];?>">
<h2><?php the_title(); ?></h2>
<img src="<?php echo $property_image2['url'];?>" alt="<?php echo $property_image2['alt'];?>" class="property-detail align-right">
<?php the_content(); ?>
<p><a href="<?php the_field('your_download_field_name'); ?>" target="_blank" name="Spec Sheet">Download Spec Sheet</a></p>
</div>
<?php wp_reset_postdata(); endwhile;?>
Whilst 1 of the answers below suggests that using WP_Query is none preferred when not using a pagination is sort of correct. However, the fact it's limited to 4 per page suggests that there is a pagination and also using the query allows for future proofing should you want to use a pagination.
Upvotes: 0
Reputation: 2088
You are echoing $property_image1 while you have set $property_image2.
And where do $download come from ?
Check this answer for wp_query vs get_posts : https://wordpress.stackexchange.com/a/191934/134453
In your case, if you have no pagination (i think you don't have any because you set posts_per_page to 4), it's better to use get_posts()
. It's also faster.
Test this, i think this will work great :
<?php
// Get posts args
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => '4'
);
// Get properties
$properties = get_posts($args);
if (!empty($properties)) :
foreach($properties as $post) : setup_postdata($post) :
$property_image2 = get_field('property_image2');
?>
<div class="property">
<img src="<?php echo $property_image2; ?>" alt="<?php the_title(); ?>">
<h2><?php the_title(); ?></h2>
<img src="<?php echo $property_image2; ?>" alt="property-detail" class="property-detail align-right">
<?php the_content(); ?>
<p><a href="<?php echo $download; ?>" target="_blank" name="Spec Sheet">Download Spec Sheet</a></p>
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Upvotes: 0
Reputation: 4851
It looks like you are trying to echo $property_image1
which does not exist. You need to either define it or use $property_image2
which does exist.
Upvotes: 0