Site logo

Archived topic

Archive Loop

13 replies · Started by Tesco on August 10, 2022

Viewing posts 1–14 of 14

I need to style the looks of posts in archive.

Is there any way to customize the loop and the html/css of this page?

Hi Fernando,

Thanks but that does not allow-me to insert custom PHP or HTML inside that loop where I need it.

I noticed that the theme offers this filter.

I just don't really know how to apply it to put my custom loop in place of the archive.php.

I think this is the part I need to "filter":

if ( generate_has_default_loop() ) {
				if ( have_posts() ) :

					/**
					 * generate_archive_title hook.
					 *
					 * @since 0.1
					 *
					 * @hooked generate_archive_title - 10
					 */
					do_action( 'generate_archive_title' );

					/**
					 * generate_before_loop hook.
					 *
					 * @since 3.1.0
					 */
					do_action( 'generate_before_loop', 'archive' );

					while ( have_posts() ) :

						the_post();

						generate_do_template_part( 'archive' );

					endwhile;

					/**
					 * generate_after_loop hook.
					 *
					 * @since 2.3
					 */
					do_action( 'generate_after_loop', 'archive' );

				else :

					generate_do_template_part( 'none' );

				endif;
			}

Hi there,

are you wanting to replace the entire Loop ie.

while ( have_posts() ) :

	the_post();

	generate_do_template_part( 'archive' );

endwhile;

Or just the content template that is displayed inside the loop ?

Hi david,

Hard question...

I need to have some custom args eg: ('showposts' => 20','post__not_in' => get_option( 'sticky_posts' ), etc.)

But if I can not to alter the core files, ideally I would just like to replace the_post(); by my own custom PHP and HTML...

Note that I also need to have my custom pagination:

endwhile;
// Pagination
echo wp_pagenavi( array( 'query' => $archive_posts_query) );

Without modifying templates. You can:

1. Use the pre_get_posts hook to add your own query vars:

https://developer.wordpress.org/reference/hooks/pre_get_posts/
https://jdsteinbach.com/wordpress/changing-wordpress-query-vars-specific-archive-pages/

2. Hook in your own wp_pagenavi - see here for an example:

https://generatepress.com/forums/topic/first-previous-next-last-navigation/#post-793257

3. The content template which displays the actual posts in the loop, is displayed using this function:

generate_do_template_part( 'archive' );

This calls the relevant content.php template. Ideally the easiest solution would be to add your won custom templates in a child theme. But in functions.php you can do:

i) Disable the default template:

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

ii) Then use generate_after_do_template_part Hook:

https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/theme-functions.php#L594

To hook in your own function:

add_action( 'generate_after_do_template_part', 'your_custom_callbak' );

Awesome learning!

I followed your instructions and it's almost perfect... so, there's still one thing that I must to ask you.

I have my loop, but I must know how to deal with this:

I have an element that renders the loop, but It should star with <div class="generate-columns-container "> this and and end with this </div>.

This is not what I espect.

Just trying to competent your instructions because that div must be placed before and after the loop

The generate_blog_columns is a boolean filter ie. it can be TRUE or FALSE.
It is the same as you enabling the Columns in the Customizer > Layout > Blog

It enables many functions that do many things, like adding the necessary body_class and post_class for the columns CSS, it loads the necessary stylesheet and it hooks in the <div class="generate-columns-container "> and its closing </div>

I am not sure what exactly it is you're trying to achieve, but i get the feeling your making it more complicated then it should be :)

Great help, once again!

Thanks, David!

You're welcome

This archived topic is closed to new replies.