问题描述:

I made a page with some articles and a gallery. Everything is displayed perfectly, but I'd like to load some special article, so I use this code:

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts("category_name=actualites&paged=$paged;");

?>

But in the end, the gallery doesn't show...

How could I display special articles, AND display the gallery which is in the loop? (I copy the code of my gallery)

<!-- beginning of the gallery -->

<div id="gallerie">

<?php ;?>

<?php $images = get_field('image_gallery');?>

<?php if( $images ): ?>

<div class="image-home">

<?php foreach( $images as $image ): ?>

<a href="<?php echo $image['url']; ?>">

<img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />

</a>

<?php endforeach; ?>

</div>

<?php endif; ?>

</div>

<!-- end of the gallery -->

网友答案:

Add wp_reset_query(); after your first block of code. This resets the query back to its original. Without doing so the code in your second block will fail.

It's also worth noting that query_posts() isn't meant to be used in plugins or themes. Read more about that here: https://codex.wordpress.org/Function_Reference/query_posts

A better way of displaying only one category would be like so:

function my_custom_page( $query ) {
    if ( $query->is_page( '{name}' ) ) {
        $query->set( 'cat', '{number}' );
    }
}
add_action( 'pre_get_posts', 'my_custom_page' );
相关阅读:
Top