- This topic has 10 replies, 2 voices, and was last updated 2 years, 4 months ago by
David.
-
AuthorPosts
-
October 27, 2022 at 9:17 am #2390173
Rafy
Hi,
How do we combine two or three different content template designs for all archive pages? And set them to show up in a loop. For example, the first post in the first design, the second post in the second design, the third post in the third design, fourth post in the first design again.
https://share.cleanshot.com/e62pha
Thanks.
October 27, 2022 at 9:23 am #2390181Rafy
Kindly check on the following link:
October 27, 2022 at 10:27 am #2390245David
StaffCustomer SupportHi there,
you could create 3 x Block Element Content Templates.
Take a note of each of the IDs.
And DO NOT set any Display Rules.Then try this PHP Snippet to programmatically count posts in the loop and display the relevant element.
// Set up global counter add_action( 'generate_before_loop', function() { global $loop_count; $loop_count = 0; }); // Increase counter for each post add_action( 'generate_before_do_template_part', function() { global $loop_count; $loop_count++; }); // Display element based on counter modulo add_filter( 'generate_element_display', function( $display, $element_id ) { global $loop_count; if ( is_archive() || is_home() ) { if ( 38 === $element_id && $loop_count % 1 == 0 ) { $display = true; } if ( 129 === $element_id && $loop_count % 2 == 0 ) { $display = true; } if ( 131 === $element_id && $loop_count % 3 == 0 ) { $display = true; } } return $display; }, 10, 2 );
Note the 39, 129 and 131 are the IDs that need updating to match your element IDs
October 27, 2022 at 10:54 pm #2390825Rafy
Hi,
Thanks for the code. I applied that, but it seems to return the same results, the same post twice. I have two templates, so I removed the 3rd from the code and added the element IDs to the other two. I removed all display rules. I pasted the following code to the themes functions.php file. It returns the results but repeats the same post twice.
// Set up global counter add_action( 'generate_before_loop', function() { global $loop_count; $loop_count = 0; }); // Increase counter for each post add_action( 'generate_before_do_template_part', function() { global $loop_count; $loop_count++; }); // Display element based on counter modulo add_filter( 'generate_element_display', function( $display, $element_id ) { global $loop_count; if ( is_archive() || is_home() ) { if ( 55403 === $element_id && $loop_count % 1 == 0 ) { $display = true; } if ( 55460 === $element_id && $loop_count % 2 == 0 ) { $display = true; } } return $display; }, 10, 2 );
October 28, 2022 at 3:31 am #2391193David
StaffCustomer SupportHmmm…. it works on my local install, so it should work here.
Can you edit this code:
add_action( 'generate_before_do_template_part', function() { global $loop_count; $loop_count++; });
And change it to:
add_action( 'generate_before_do_template_part', function() { global $loop_count; $loop_count++; echo 'loop counter: ' . $loop_count; });
This should print out the loop counter value before each post, so we can see if thats working.
October 28, 2022 at 5:00 am #2391300Rafy
I tried that code, but it has started to display loop counter:1, loop counter:2, loop counter:3, with no change to the output.
Screenshot: https://share.cleanshot.com/VMpN4R
Here is the current code:
// Set up global counter add_action( 'generate_before_loop', function() { global $loop_count; $loop_count = 0; }); // Increase counter for each post add_action( 'generate_before_do_template_part', function() { global $loop_count; $loop_count++; echo 'loop counter: ' . $loop_count; }); // Display element based on counter modulo add_filter( 'generate_element_display', function( $display, $element_id ) { global $loop_count; if ( is_archive() || is_home() ) { if ( 55403 === $element_id && $loop_count % 1 == 0 ) { $display = true; } if ( 55460 === $element_id && $loop_count % 2 == 0 ) { $display = true; } } return $display; }, 10, 2 );
October 28, 2022 at 6:08 am #2391398David
StaffCustomer SupportThats good, it shows that the counter value is working.
Would it be possible to get admin access to the site so i can see why that filter isn’t working ?October 28, 2022 at 9:43 am #2391925Rafy
Sure, here:
October 30, 2022 at 7:29 am #2393881David
StaffCustomer SupportOk, sorry for the delay.
Complete change of plan 🙂You require only One Content Template that combines both of the templates. Stacked one above the other.
Each should have their own top level Container to keep them separate, and in those containers we need to add a CSS Class.For template one container add:
content-template-one
For template two container add”content-template-two
Now we use this snippet and the render_block filter to remove the container:
add_filter( 'render_block', function( $block_content, $block ) { global $wp_query; $index = $wp_query->current_post + 1; if ( !is_admin() && ! empty( $block['attrs']['className'] ) ) { if ( 'content-template-one' === $block['attrs']['className'] && $index % 2 == 0 ) { $block_content = null; } } return $block_content; }, 10, 2 ); add_filter( 'render_block', function( $block_content, $block ) { global $wp_query; $index = $wp_query->current_post + 1; if ( !is_admin() && ! empty( $block['attrs']['className'] ) ) { if ( 'content-template-two' === $block['attrs']['className'] && $index % 2 == 1 ) { $block_content = null; } } return $block_content; }, 10, 2 );
October 31, 2022 at 4:19 am #2394994Rafy
It worked! Many thanks.
October 31, 2022 at 6:17 am #2395133David
StaffCustomer SupportPhew…. glad to hear that.
-
AuthorPosts
- You must be logged in to reply to this topic.