Site logo

Archived topic

How to wrap loop (generate_do_template_part) ?

7 replies · Started by Fabien on October 28, 2020

Viewing posts 1–8 of 8

Hi,

I am using the following function to change the loop on my category pages :

/**
 * Change loop on archive.
 */

add_filter( 'generate_do_template_part', function( $do ) {
    if ( is_category() ) {
        return false;
    }
    return $do;
} );

add_action( 'generate_before_do_template_part', function() {
    if ( is_category() ) {
        get_template_part('partials/content', 'archive');
}
} );

How can I wrap my loop with some <div></div> ?

Thanks !

Hi there,

Your markup would ideally go into the partials/content-archive.php file.

However, you could also do this:

echo '<div>';
    get_template_part('partials/content', 'archive');
echo '</div>';

Thanks Tom, but if I do that, all posts are individually wrapped... I just want to wrap around my loop like :

<div class="wrapper">
    <article>...</article>
    <article>...</article>
    <article>...</article>
</div> 

You could try this:

add_action( 'generate_before_do_template_part', function() {
    echo '<div class="wrapper">';
} );

add_action( 'generate_after_do_template_part', function() {
    echo '</div>';
} );

You'd just need to add your conditions to each function.

Same issue, it wraps the whole post (item) and not the whole loop...

Aha, ok, you'll need to do this:

add_action( 'generate_before_main_content', function() {
    echo '<div class="wrapper">';
} );

add_action( 'generate_after_main_content', function() {
    echo '</div>';
} );

Thanks Tom, but if I do that, it also wraps the page-header...

For archives, you may need to do this:

add_action( 'generate_archive_title', function() {
    if ( is_archive() ) {
        echo '<div class="wrapper">';
    }
}, 20 );

add_action( 'generate_after_loop', function() {
    if ( is_archive() ) {
        echo '</div>';
    }
} );
This archived topic is closed to new replies.