- This topic has 21 replies, 3 voices, and was last updated 3 years, 7 months ago by Tom.
-
AuthorPosts
-
September 20, 2020 at 6:09 am #1450882Eroan
Hi,
I wand to call last 6 posts contents in 2 columns on a custom front-page.php template.
On front-page.php, I have :
<?php // The Query $query_options = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 6, 'order' => 'DESC', 'orderby' => 'ID', ); $the_query = new WP_Query( $query_options ); // The Loop if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query -> the_post(); generate_do_template_part( 'archive' ); endwhile; ?> <?php endif; ?>
On functions.php I have :
// Columns for sites custom post type in archive add_filter( 'generate_blog_columns','tu_portfolio_columns' ); function tu_portfolio_columns( $columns ) { if(is_archive() || is_front_page()){ return true; } return $columns; } // 2 columns on home add_filter( 'generate_blog_get_column_count','tu_search_column_count' ); function tu_search_column_count( $count ) { if ( is_front_page() ) { return 50; } return $count; }
I see that I can’t use columns here. Content is there but in full width. Even that way nothing happens so this is not a conditions issue :
// Columns for sites custom post type in archive add_filter( 'generate_blog_columns','tu_portfolio_columns' ); function tu_portfolio_columns( $columns ) { return true; }
On the page https://docs.generatepress.com/article/using-columns-in-the-blog/#adding-columns-to-your-custom-post-type it says that “You can use this filter to enable or disable columns under any conditions you need by using the WordPress conditional tags” but this is not true…
Can you please make the hook work on ANY page the next beta version ? I’m using 3.0.0-beta.1.
Thanks and well done, your theme is excellent work for a webperf addict as I am ๐
September 20, 2020 at 9:08 am #1451163LeoStaffCustomer SupportHi there,
Any chance you can link us to the site in question?
You can use the private information field.
Let me know ๐
September 21, 2020 at 12:13 am #1451711EroanHi,
I give you the details in private field.
I found a temporary solution by making str_replaces on ob_start / ob_get_clean and calling manually the css file in functions.php :
if ( is_front_page() ) { wp_enqueue_style( 'generate-blog', plugins_url().'/gp-premium/blog/functions/css/style.min.css' ); }
Thanks.
September 21, 2020 at 5:28 am #1452083EroanI succeeded in “tweaking” the code to make it word as expected.
But the main issue is finally that content (latest posts) is listed as single, not as archive elements :
- titles are h1
- there is no link to the article
Is there a simple way to activate “archive” semantics when calling generate_do_template_part( ‘archive’ ); ?
Thanks.
September 21, 2020 at 10:37 am #1452659TomLead DeveloperLead DeveloperI’m not able to see the site even with the change to my hosts file.
This shouldn’t be necessary: https://generatepress.com/forums/topic/generatepress-columns-on-custom-query/#post-1451711
However, make sure you’re using the latest GPP 1.12.0-beta.
Using
generate_do_template_part()
likely doesn’t make sense in this case as you’re already using a custom template. Instead, useget_template_part( 'content' )
.September 21, 2020 at 11:18 am #1452698EroanThanks for your answer.
I’m using latest GPP 1.12.0-beta plugin.
both get_template_part(‘content’,’post’); and generate_do_template_part( ‘archive’ ); give exactly the same html output : content with <h1> on titles and no links.
Now my code looks like :
<?php // The Query $query_options = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 6, 'order' => 'DESC', 'orderby' => 'date' ); $the_query = new WP_Query( $query_options ); // The Loop if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query -> the_post(); ob_start(); get_template_part('content','post'); // generate_do_template_part( 'archive' ); echo str_replace(array('<h1','</h1>','" itemtype="https://schema.org/CreativeWork"'), array('<a href="'.get_the_permalink(get_the_ID()).'"><h3','</h3></a>',' generate-columns mobile-grid-100 grid-parent grid-50" itemtype="https://schema.org/CreativeWork"'), ob_get_clean()); endwhile; ?> <?php endif; ?>
September 21, 2020 at 2:02 pm #1452924TomLead DeveloperLead DeveloperIt should just be
get_template_part('content');
That should load the
content.php
in the parent theme.You can always copy that file into your child theme and name it differently:
content-custom.php
Then you can do this:
get_template_part( 'content', 'custom' );
September 21, 2020 at 11:33 pm #1453309EroanThanks. Replacing the call with only ‘content’ didn’t change anything.
Looking at content.php template, I see that my issue comes from generate_get_the_title_parameters().
It does generate <h1> and no link where it shoulf be <h2> and link as in archive pages.
How can I hook in generate_get_the_title_parameters() to change behavior when is_front_page() condition is true ?
Thanks.
September 21, 2020 at 11:37 pm #1453314EroanGot it in theme_functions.php
Need to hook to mimic that behavior on home :
if ( ! is_singular() ) { $params = array( 'before' => sprintf( '<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark">', esc_url( get_permalink() ), 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '' ), 'after' => '</a></h2>', ); }
September 21, 2020 at 11:41 pm #1453318EroanDone :
// Display news in archive type on home add_action( 'generate_get_the_title_parameters','display_h2_on_home' ); function display_h2_on_home($params) { if(is_front_page()){ $params = array( 'before' => sprintf( '<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark">', esc_url( get_permalink() ), 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '' ), 'after' => '</a></h2>', ); } return $params; }
Now I just need to add the “generate columns” classes to post_class() : generate-columns tablet-grid-50 mobile-grid-100 grid-parent grid-33 :
add_filter('post_class','generate_columns_classes'); function generate_columns_classes($classes){ if(is_front_page()){ $classes[] = 'generate-columns'; $classes[] = 'mobile-grid-100'; $classes[] = 'grid-parent'; $classes[] = 'grid-50'; } return $classes; }
September 22, 2020 at 12:03 am #1453346EroanLast modification I have to make manually : post thumbnails are not clickable.
In featured-images.php you have conditions to add links or not but it’s limited to :
// If we're not on any single post/page or the 404 template, we must be showing excerpts. if ( ! is_singular() && ! is_404() ) {
How can I easily hook into that to have that same behavior on home ?
echo apply_filters( // phpcs:ignore 'generate_featured_image_output', sprintf( '<div class="post-image"> %3$s <a href="%1$s"> %2$s </a> </div>', esc_url( get_permalink() ), get_the_post_thumbnail( get_the_ID(), apply_filters( 'generate_page_header_default_size', 'full' ), $attrs ), apply_filters( 'generate_inside_featured_image_output', '' ) ) );
Thanks.
September 22, 2020 at 9:33 am #1454132TomLead DeveloperLead DeveloperI suppose you could do something like this:
add_action( 'generate_before_content', function() { if ( ! is_front_page() ) { return; } printf( // WPCS: XSS ok. '<div class="post-image"> %3$s <a href="%1$s"> %2$s </a> </div>', esc_url( get_permalink() ), get_the_post_thumbnail( get_the_ID(), apply_filters( 'generate_page_header_default_size', 'full' ), array( 'itemprop' => 'image', ) ) ); } );
September 22, 2020 at 11:23 am #1454290EroanIt didn’t work, I corrected it a bit since there was a syntax error. But nothing happens :
add_action( 'generate_after_entry_header', 'generate_after_entry_header_custom'); function generate_after_entry_header_custom() { printf( // WPCS: XSS ok. '<div class="post-image"> %3$s <a href="%1$s"> %2$s </a> </div>', esc_url( get_permalink() ), get_the_post_thumbnail( get_the_ID(), apply_filters( 'generate_page_header_default_size', 'full' ), array('itemprop' => 'image')) ); }
September 22, 2020 at 1:34 pm #1454441TomLead DeveloperLead DeveloperCan you try the updated code?: https://generatepress.com/forums/topic/generatepress-columns-on-custom-query/#post-1454132
September 23, 2020 at 12:01 am #1454820EroanStill not working, it doesn’t change anything, posts images have “featured-image page-header-image” classes instead of “post-image” that sould be rendered by the hook…
-
AuthorPosts
- You must be logged in to reply to this topic.