Site logo

[Resolved] Making grid with child pages

Home Forums Support [Resolved] Making grid with child pages

Home Forums Support Making grid with child pages

Viewing 15 posts - 1 through 15 (of 25 total)
  • Author
    Posts
  • #2271543
    Joey

    I have a website where I have lists in the form of grids that are linked to specific pages. For example: The parent page is “contributors” and within that page, there is a grid with photos of the contributors. If the user clicks on a photo, it goes to the appropriate child page that gives more information. I used to use WP Show Posts and just entered the Post IDs of the appropriate pages.

    I’m trying to do the same thing with Query Loop in GenerateBlocks. I can list Pages, but it includes all pages on the website. Is there a way to only place child pages of a specific parent page in that grid? (Or at least, is there a way to include only specific pages? I can select “Include Posts” or “Exclude Posts”, but it doesn’t seem to be working with “Pages”.)

    Any help would be appreciated. Thank you.

    #2271651
    David
    Staff
    Customer Support

    Hi there,

    today you can select the Post Type as Pages, and then use the: Include posts parameter … excuse the semantics there, we’ll look to see if that option can be change to Include Pages or whatever the post type selected is.

    In the next release of GB Pro you will be able to show Child Pages of the current page.

    #2272219
    Joey

    Nice, Child Pages would save time. So the dropdown menu said “No options” and if I tried to type the Page ID in manually, (with or without the #), the number disappeared and there was nothing in the list. So I erased all my custom PHP on the site to test it, and suddenly your solution works. So I’m going to troubleshoot the code one by one and see if I can find the culprit. It seems like some of my code is screwing with the WordPress editor when I use GenerateBlocks. Same issue with posts per page. Thank you.

    #2272243
    Joey

    Okay, I found the issue, but I’m not quite sure how to fix it. Previously I had set my blog to a grid, with the featured image occupying the entire top space on desktop. The trouble was that it looked bad because by default, with 12 grids, because there was an empty space in the bottom right hand of the page. So to fix it, you helped me with a code that changed it to 13 grids when there is a featured image, filling that space:

    
    add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 );
    function tu_change_posts_per_page( &$query ) {
        if ( ! is_main_query() ) {
            return;
        }
    
        if ( ! $query->is_paged ) {
            $query->set( 'posts_per_page', 13 );
        }
    }
    

    With this code on my website, I can’t use the “Include post” parameter with “Pages” when this PHP is in my website. It also creates another issue where no matter what I set the “Posts per page” as, whether pages or posts, it always shows 13. So I understand why the second issue is happening, but not the first.

    Do you know of a way to solve this issue while still allowing 13 posts on the first page of the press/blog? Thanks a lot.

    #2272264
    Fernando
    Customer Support

    Hi Joey,

    Can you try altering that PHP code into this?:

    add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 );
    function tu_change_posts_per_page( &$query ) {
        if (  is_admin() || ! $query->is_main_query() ) {
            return;
        }
    
        if ( ! $query->is_paged ) {
            $query->set( 'posts_per_page', 13 );
        }
    }

    Kindly let us know how it goes.

    #2273388
    Joey

    That worked Fernando. Thank you very much to both of you.

    #2273392
    Fernando
    Customer Support

    You’re welcome Joey! Glad that worked!

    #2274245
    Joey

    Fernando, there is a bug with your code that I didn’t realize. Your code fills up the space that is missing, but when I go to the next page (it has the same effect whether the blog is paged or whether I use infinite scroll with the “More” button), it repeats the post. So for example, since my blog is set up on infinite scroll, it forces two identical posts next to each other and looks bad. It only does this the first time. (For example, page 3 or 4, or clicking “More” later on the page, doesn’t repeat a post.) I have the same issue with categories, with the search function, etc. Is there a way to fix this?

    #2274301
    Fernando
    Customer Support

    I see. This is an offset issue. With the previous version of the code, did that issue exist as well? Can you try reverting it back temporarily?

    To clarify, may I know why you’re not setting the posts per page simply through Settings > Reading > Blog pages show at most?

    Kindly let us know.

    #2274365
    Joey

    The old code does it too. The reason I don’t want to do it through Settings is because my featured image fills the entire top row—when the reader clicks on “More”, it would load 13 new slides (instead of 12) and there would be a gap on the bottom row.

    Worst case, I could get rid of the “More” button, and set the posts per page to an odd number that fills the entire space. But if I can have the first page show 13 posts, and have the “More” button load the default 12 without repeating, it would let reader access old posts, and it fills up the entire space whether desktop (3 rows) or tablet (2 rows).

    #2275467
    Fernando
    Customer Support

    Do you have this layout only in the blog page where there’s a “featured” post?

    As an alternative, maybe we can just add this featured post through a Query Loop Block in a Block Element, and then exclude this post in the query of the front page?

    Kindly let us know.

    #2275592
    Joey

    Correct. I just use CSS:

    .generate-columns-container .featured-column {
        float: left;
        width: 100% !important;
    }
    
    @media(min-width: 769px) {
        .featured-column h2.entry-title {
            font-size: 40px;
            font-weight: 900;
    		padding-top: 30px;
        }
    	.featured-column .entry-summary {
            font-size: 20px;	
        }
    }
    
    @media(min-width: 769px) {
        .separate-containers .featured-column .post-image img {
            max-width: 66%;
            float: left;
            margin-right: 2em;
        }
        .separate-containers .featured-column .inside-article {
            padding-bottom: 0;
        }
    }

    Featured post shows up on Categories and Search too, which is fine, but the only place the featured post needs to be is on the first page of the blog. The blog is not my front page, by the way.

    I’m a little confused. Are you saying to place the featured post as a Query Loop Block at the top of the page, the size and format I want, and have the blog populate afterwards? How would I exclude the first post? If I used a Query Loop Block, would I even need a dedicated blog page? I’m putting a link to the blog page in the information below.

    #2275625
    Fernando
    Customer Support

    I see.

    Basically, create a Block Element hooked to before_main_content for instance. This will have a Query Loop Block, querying only one post. It may be harder to implement for categories and the search page, we’ll need more code to implement it properly (filter the category).

    But for instance, you have this Block Element: https://share.getcloudapp.com/ApuXXxJe

    If you add a Snippet like this:

    function remove_first_post( $query ) {
        if ( ( $query->is_home() || $query->is_category() || $query->is_search() ) && $query->is_main_query() ) {
            $args = array('posts_per_page' => -1,
    			'fields' => 'ids');
    		$posts = get_posts($args);
    		$query->set( 'post__not_in', array($posts[0]) );
    		
        }
    	
    	if ( ( $query->is_home() || $query->is_category() || $query->is_search() ) && $query->is_paged() && $query->is_main_query() ) {
    		 add_filter( 'generate_block_element_display', function( $display, $element_id ) {
    
    			if ( 396706 === $element_id && is_main_query() && is_paged() ) {
    				$display = false;
    
    			} 
    			return $display;
    		}, 15, 2 );
    		
        }
    }
    add_action( 'pre_get_posts', 'remove_first_post' );

    You’ll need to replace 396706 with your Block Element ID.

    Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets

    Then set the blogs per page to 12 in Settings > Reading, the blog page should work as expected.

    This is the approach I’m looking at.

    #2276679
    Fernando
    Customer Support

    Hi Joey,

    Did some research and we can try to fix the offset instead of using a Block Element as I suggested. It’s a bit tricky though and I’m not sure if it will work:

    But, you can try removing this code:

    add_action( 'pre_get_posts', 'tu_change_posts_per_page', 1 );
    function tu_change_posts_per_page( &$query ) {
        if (  is_admin() || ! $query->is_main_query() ) {
            return;
        }
    
        if ( ! $query->is_paged ) {
            $query->set( 'posts_per_page', 13 );
        }
    }

    Then set the posts per page to 13 in Settings > Reading.

    Then add this PHP snippet instead:

    function itsme_category_offset( $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = $query->query_vars[ 'paged' ];
    	$current_page = get_query_var('paged');
        $paged = 12;
    
        if( !is_admin() && $query->is_home() && $query->is_main_query() ) {
            if( is_paged() ) {
    
                $query->set( 'posts_per_page', $paged );
    			$paged_offset = $ppp + ( $paged *  ( $current_page - 2 ) );
    			if($paged_offset < 0) {
    				$paged_offset = $ppp;
    			}
            	$query->set('offset', $paged_offset );
    
            }
        }
    }
    add_action( 'pre_get_posts', 'itsme_category_offset' );
    
    function itsme_adjust_category_offset_pagination( $found_posts, $query ) {
        $ppp = get_option( 'posts_per_page' );
        $first_page_ppp = $query->query_vars[ 'paged' ];
    	
    
        if( !is_admin() && $query->is_home() && $query->is_main_query() ) {
    		if( is_paged() ) {
    			return( $found_posts + 1 );
    		} else {
    			return( $found_posts - 1 );
    		}
            
        }
        return $found_posts;
    }
    add_filter( 'found_posts', 'itsme_adjust_category_offset_pagination', 10, 2 );

    Reference: https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination

    It would be good to take a backup of your site before doing this so that if it doesn’t work, you can revert the changes back. Tested it though from my end, and it’s working as expected.

    Hope this helps!

    #2276814
    Joey

    Fernando, sorry for not responding sooner—I was fixing another part of the site and hadn’t come back to this. The offset and pagination custom query worked well. I’m checking for any bugs now, but so far GP Blocks is working as it should, and it doesn’t repeat posts anymore on the blog or categories.

    I’m going to try it with a Block Element too, just to learn how to do it. But it solved the issue. Thank you for all your help, it looks good.

Viewing 15 posts - 1 through 15 (of 25 total)
  • You must be logged in to reply to this topic.