Site logo

Archived topic

Generatepress columns on custom query

21 replies · Started by Eroan on September 20, 2020

Viewing posts 1–15 of 22

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 :)

Hi there,

Any chance you can link us to the site in question?

You can use the private information field.

Let me know :)

Hi,

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.

I 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.

Thanks 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; ?>

It 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' );

Thanks. 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.

Got 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>',
		);
	}

Done :

// 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;
}

Last 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.

I 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',
            )
        )
    );
} );

It 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'))
    );
}

Still 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...

This archived topic is closed to new replies.