Reputation: 42
I'm trying to get post to loop based on conditions
If (book_status === "published" || !empty($url)) {
while (have_posts()) : ?>
html code...
elseif (book_status !== "published" && || empty($url){
while (have_posts()) : ?>
html code alternative.
essentially trying to create two grids. One that shows the published work and one that shows the unpublished work. However, no matter where I put the if statement, I cannot seem to make it work with the loop. I reverted it back to testing if it works with the buttons. It does.
<!-- .entry-header -->
<?php
$url = get_post_meta( $post->ID, '_my_url', true );
$book_status = get_post_meta( $post->ID, '_book_published', true );
?>
<?php
if ( has_post_thumbnail() ) : ?>
<figure class="entry-thumbnail">
<caption><h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2></caption>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php if ( is_home() && $blog_sidebar_position == 'content-fullwidth' ) {
the_post_thumbnail('type-small');
} elseif ( ( is_archive() || is_search() ) && $archive_sidebar_position == 'content-fullwidth') {
the_post_thumbnail('type-small');
} else {
the_post_thumbnail('type-small');
}
?>
</a>
</figure><div class="container works-buttons">
<?php if( !empty( $url ) && $book_status == "published"){
echo '<button class="amazon fa fa-amazon" id="amazon" type="button"><a class="amazon-link" href="' . esc_url( $url ) . '">Buy</a></button>'; ?>
<button type="button" class="fa type22 fa-solid fa-bag-shopping"><a href="/shop">Signed</a></button>
<button type="button" class="fa type22 "><a href="<?php the_permalink(); ?>">About</a></button>
<?php
} elseif ( !empty( $url ) && $book_status !== "published"){
echo '<button class="amazon fa fa-amazon" id="amazon" type="button"><a class="amazon-link" href="' . esc_url( $url ) . '">Buy</a></button>'; ?>
<button type="button" class="fa type22 fa-solid fa-bag-shopping"><a href="/shop">Signed</a></button>
<button type="button" class="fa type22 "><a href="<?php the_permalink(); ?>">About</a></button>
<?php } else{ ?>
<button type="button" class="fa type22 fa-solid fa-bag-shopping"><a href="/shop">Get Updates</a></button>
<button type="button" class="fa type22 "><a href="<?php the_permalink(); ?>">About</a></button>
<?php }
?>
</div>
<?php
endif; ?>
</article>
</div>
<?php endwhile; ?>
</section>```
Upvotes: 0
Views: 44
Reputation: 108796
If you're inside the main post loop already when you do
while (have_posts()) {
...
the_post();
}
your code will use up all the posts in the main loop.
If you want to look at other posts based on the current post in your main loop, you need to use a different WP_Query object. Something like this.
$myQuery = new WP_Query( appropriate_query_args );
if ( $myQuery->have_posts() ) {
echo '<ul>';
while ( $myQuery->have_posts() ) {
$myQuery->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Upvotes: 1