A
A
alekamisleforma2018-10-21 20:26:36
WordPress
alekamisleforma, 2018-10-21 20:26:36

How to display all posts except the first one in WordPress?

Hello! I don't know much about PHP and WordPress yet and I'm learning them in parallel. I could not find the answer to the question in Google, so I hope for your help)
There is a function that receives an array of records of the 'services' type. Recorded, as expected, in functions.php:

function getFaq(){
    $args = array(
      'orderby'     => 'date',
      'order'       => 'ASC',
      'post_type'   => 'services',
    );
    return get_posts( $args );
  }

Further, I need to use this function in two places of layout, but in such a way that only the first post is displayed in one place, and all posts except the first one in the other.
This is how the output of only the first post is implemented. This is achieved by breaking at the end. Works fine:
spoiler
<div class="twelve columns">
          <?php foreach(getFaq() as $post): ?>
        		<h1><?php echo $post->post_title; ?><span>.</span></h1>

  	         <hr />      	         

  	         <p><?php echo $post->post_content; ?></p>
          <?php break; endforeach; ?>
        </div>

And here is the second place where the difficulties arise:
<?php foreach(getFaq() as $post): ?>
        	    <div class="bgrid">	               
        
                <h3><?php echo $post->post_title; ?></h3>
                <div class="service-content"> 
                  <p><?php echo $post->post_content; ?></p>
                </div> 

              </div> 
            <?php  endforeach; ?>

In the code above, all posts are displayed in general, including the first one. There was an attempt to remove the first post using a condition, but it does not work as intended:
<?php foreach(getFaq() as $post):
            if(!getFaq()[0]){ ?>
        	    <div class="bgrid">	               
        
                <h3><?php echo $post->post_title; ?></h3>
                <div class="service-content"> 
                  <p><?php echo $post->post_content; ?></p>
                </div> 

              </div> 
            <?php } endforeach; ?>

If the writing is translated into human language, then the idea was this - "If NOT the first element of the array ($args[0], since it is the array $args that the function returns), then the code in brackets is triggered." And because the foreach loop, judging by the documentation, goes through all the elements of the array, then the first post should be skipped, and the rest should be displayed. But for some reason it doesn't work like that. What is wrong with me?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
be_a_man, 2018-10-21
@alekamisleforma

5bcccb293bdd3256866255.png
https://wp-kama.ru/function/wp_query
get_posts() - https://wp-kama.ru/function/get_posts#offset-chislo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question