0

I'm ussing in my site that code:

category-anuncios.php

    <div class="listado-anuncios">
                <?php
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;                 
                    $query=array("category_name"=>"anuncios","order"=>"ASC","posts_per_page"=>"1","page"=>$paged);
                    $objAnuncio = new WP_Query($query);
                    if ($objAnuncio->have_posts()){
                        ?>
                        <ul>
                            <?php
                            while($objAnuncio->have_posts()){
                                $objAnuncio->the_post();
                                ?>
                                <li>
                                    <?php
the_title();
                                    ?>
                                </li>
                                <?php
                            }
                            ?>
                        </ul>
                        <?php
                        if (function_exists("wp_pagenavi")){wp_pagenavi(array("query"=>$objAnuncio));}
                        wp_reset_postdata();
                    }           
                ?>          
            </div>      

I can see the content of my post well and show too the navigation...but, if I try to navigate to the second, third or other I have the error 404.

How can use that navigation with my custom query?

Edit:

Another way to test to fix the error:

Change the WP_Query for query_posts like that

global $wp_query;
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;    
                $categoria = $wp_query->query_vars["cat"];
                query_posts('cat='.$categoria.'&posts_per_page=1&paged='.$paged);

But I've the same result: error 404

4
  • Do not use a custom query, use pre_get_posts to alter the main query. This should solve your issue Jan 13, 2016 at 10:19
  • I use other way:, I try to use the "normal" loop in wordpress (query_posts) but I've the same result: error 404...I wrote the code in the question too
    – Marcos
    Jan 13, 2016 at 11:04
  • query_posts is not the normal loop. You should never ever use query_posts, it is a really bad wayy to create custom queries. Use the default loop, and use pre_get_posts to alter the main query if you need to alter the main query Jan 13, 2016 at 11:06
  • Yes, you are right @PieterGoosen... I can fixed ussing pre_get_post in my functions file. I will write the answer...Thanks!
    – Marcos
    Jan 13, 2016 at 15:20

1 Answer 1

1

Just work including that code in the functions file:

function mod_query($query){
    $entradas = get_category_by_slug('anuncios');    
    if ((!is_admin() && $query->is_main_query())){
        $query->set('posts_per_page',"2");
        $query->set('cat','8');
    }
}
add_action('pre_get_posts','mod_query');

The file to show the content of the category is a normal file (without extra configuration)

Thanks to @Pieter Groosen for the orientation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.