Archived topic
Custom posty type loop page via elements doesn't work on homepage
3 replies · Started by Jorian on February 4, 2021
Hello,
I've created a page where i use an element to loop a CPT archieve
When i set this page as homepage, the loop is not working.
This code do i use:
<?php
$args = array(
'post_type' => 'woningen'
);
$the_query = new WP_Query($args);
?>
<?php if ($the_query->have_posts() ) : while ($the_query->have_posts() ) :$the_query->the_post(); ?>
<!-- hero-area-start-->
<div class="hero-area">
<div class="hero-left1">
<div class="hero-left-box">
<div class="hero-box-title">
<h3><a href="<?php the_permalink(); ?>"><?php the_field('adres'); ?>, <?php the_field('plaats'); ?></a><span class="hero-box-status"><?php the_field('verkocht'); ?></span></h3>
<a href="<?php the_permalink(); ?>">€ <?php the_field('prijs'); ?>,- kk</a>
</div>
<div class="hero-box-containt-fl">
<div class="hero-img"><?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'full' );
}
?></div>
<div class="hero-containt">
<div class="hero-single-text">
<h3>Bouwjaar: <?php the_field('bouwjaar'); ?></h3>
<h3>Inhoud: <?php the_field('inhoud'); ?> m3</h3>
</div>
<div class="hero-single-text">
<h3>Eigen grond: <?php the_field('eigen_grond'); ?> m2</h3>
<h3>Slaapkamers: <?php the_field('slaapkamers'); ?></h3>
</div>
<div class="hero-single-text-details">
<p><?php the_field('beschrijving'); ?></p>
</div>
</div>
</div>
<div class="hero-left-btn">
<a href="<?php the_permalink(); ?>">Foto’s</a>
<a href="<?php the_field('video'); ?>" target="_blank">Video</a>
<a href="<?php the_permalink(); ?>#plattegrond">Plattegrond</a>
<?php
$file = get_field('brochure');
if( $file ): ?>
<a href="<?php echo $file['url']; ?>" target="_blank">Brochure</a>
<?php endif; ?>
</div>
</div>
</div>
</div>
<!-- hero-area-end-->
<?php endwhile; ?>
<?php endif; ?>
Hi there,
Does it output anything at all?
The fact the page already has a query is likely the issue. What if you reset the query before your custom query?: https://developer.wordpress.org/reference/functions/wp_reset_query/
Hello Tom,
The page outputs only sidebar and navigation.
I put your code in my element, but i cannot see any change.
When i put it to functions.php i get an error:
Fatal error: Cannot redeclare wp_reset_query() (previously declared in /home/siebe/domains/waaijeradvies.nl/public_html/wordpress/wp-includes/query.php:114) in /home/siebe/domains/waaijeradvies.nl/public_html/wordpress/wp-content/themes/generatepress_child/functions.php on line 10
The code you add should just look like this: wp_reset_query();
If that doesn't work, you will likely need to build a custom page template and completely replace the default query with your own.