Add if statement to WP query using Sage

690 views Asked by At

so it might be something really stupid/easy but I am not able to solve that. What I'm trying to achieve is to display an information after WP loop if no posts are found. So I've got this piece of code:

 @php
            $args = array (
            'post_type' => 'jobs',
            'order' => 'ASC',
            'posts_per_page' => '-1',
            //'paged' => '1',
            );

            $loop = new WP_Query( $args );


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


            <div class="col-lg-12" data-postID='{!! get_the_ID() !!}' >
                <div>
                    <a href="{!! the_permalink() !!}">
  
                        <div class="jobButton">
                            {!! the_title() !!}
                            <div class="arrow">
                            </div>
                        </div>
                    
                    </a>               
                </div>
            </div>
            

            @php
            endwhile; 

            wp_reset_postdata(); 
 @endphp 

and it works. But when I've tried to use something like:

@noposts <content here> @endnoposts

or

@if ($loop->have_posts()) <content here> @endif

it crashes Wordpress.. how to do that?

1

There are 1 answers

0
Nitin Sharma On

Have you tried like this

@php
            $args = array (
            'post_type' => 'jobs',
            'order' => 'ASC',
            'posts_per_page' => '-1',
            //'paged' => '1',
            );

            $loop = new WP_Query( $args );

if($loop->have_posts()):
            while ( $loop->have_posts() ) : $loop->the_post(); 
            @endphp


            <div class="col-lg-12" data-postID='{!! get_the_ID() !!}' >
                <div>
                    <a href="{!! the_permalink() !!}">
  
                        <div class="jobButton">
                            {!! the_title() !!}
                            <div class="arrow">
                            </div>
                        </div>
                    
                    </a>               
                </div>
            </div>
            

            @php
            endwhile; 
else:
  echo "No post found";
  endphp;
            wp_reset_postdata(); 
 @endphp