Help with query_post Wordpress function

Asked

Viewed 48 times

0

I’m trying to make custom posts in my Wordpress theme, however I’m having a problem. I want to pick up seven posts on the main page, my index. However the first post I will style differently from the other six. I’m repeating the query_posts(). At first I put like this:

I’m using this to pick up the first post.

query_posts('posts_per_page=1')
if (have_posts()) while (have_posts() : the_post();

And for the others I’m repeating the code with a different parameter. query_posts('posts_per_page=5')
if (have_posts()) while (have_posts() : the_post();

The problem is that the first post is repeating itself in the two blocks. I was wondering if there is any parameter that I can use in the second block to take a "jump" start picking up posts from the 2.

2 answers

0

Guys, I found the solution! Just pass the following parameter in the query_posts() function: query_posts('showposts=4&offset=1');

0

It is not recommended to use the query_posts function. See what is said on wordpress documentation:

Note that using this method can extend the page loading, since the main query is called more than once. In some scenarios it can be worse, even doubling the number of unnecessarily executed processes. Although simple to run, the function is also prone to future problems.

The suggestion is to use the class Wp_query or the function get_posts. Example of using the Wp_query class:

<?php

    // WP_Query arguments
    $args = array(
    );

    // The Query
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
        echo '<p>Nenhum post encontrado</p>'
    }
    /* Restore original Post Data */
    wp_reset_postdata();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.