[Support request] How to wrap loop (generate_do_template_part) ?

Home Forums Support How to wrap loop (generate_do_template_part) ?

  • This topic has 7 replies, 2 voices, and was last updated 4 months ago by Tom.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1508484
    Fabien

    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 !

    #1509881
    Tom
    Lead Developer
    Lead Developer

    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>';
    #1510230
    Fabien

    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> 
    #1511481
    Tom
    Lead Developer
    Lead Developer

    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.

    #1512037
    Fabien

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

    #1512702
    Tom
    Lead Developer
    Lead Developer

    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>';
    } );
    #1515216
    Fabien

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

    #1515297
    Tom
    Lead Developer
    Lead Developer

    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>';
        }
    } );
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.