简体   繁体   English

使用 Sage 将 if 语句添加到 WP 查询

[英]Add if statement to WP query using Sage

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.如果没有找到帖子,我想要实现的是在 WP 循环之后显示信息。 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?它崩溃 Wordpress .. 怎么做?

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM