• I have a custom post type (online_exhibit) that is hierarchical and has page attributes. I wish to use the Post List Grid to display a single post’s children beneath the single post’s content. My code to achieve this is below:

    $pc_args = array(
      'posts_per_page'  => -1,
      'post_type'       => 'online_exhibit',
      'post_parent'     => get_the_ID(),
      'orderby'         => 'menu_order, title',
      'order'           => 'ASC',
      'post_status'     => 'publish'
    );
    $pc_query = new WP_Query($pc_args);
    if ($pc_query->have_posts()) {
      while ($pc_query->have_posts()) {
        $pc_query->the_post();
        czr_fn_render_template(
          'modules/grid/grid_wrapper',
          array(
            'model_id'    => 'post_list_grid',
            'model_args'  => array(
              'grid_columns'    => 2,
              'image_centering' => 'js-centering',
              'show_thumb'      => 1
            )
          )
        );
      }
      wp_reset_postdata();
    }

    My issue is that instead of displaying the child posts in the same grid, a new grid is started for each individual post. I wish to use two columns, but the resulting display is a single column of posts. The column width is half the width of the overall page. When I look at the source code, I can see that each post is contained in its own “grid-container” div. The correct behavior would be for all the posts to be contained within a single “grid-container” div.

    What am I doing wrong?

    Note: This is for a local development site, so I cannot provide a link.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi Jjbte,

    You will need to customize the theme for this. You can also consult with a developer.

    Have a good day!

    Thread Starter jjbte

    (@jjbte)

    Hi @emranemranx. Thank you for your reply. I am customizing the theme. I have a child theme in which I have created a custom template for my custom post type (online_exhibit). In that custom template, beneath the content, I have placed the code above. It works except for the issue I mentioned where a new grid is created for each child post. The desired result is for all child posts to appear within a single grid.

    My code is very similar to the code used in the source theme’s template templates/parts/loop.php:

    while ( have_posts() ) {
      the_post();
      czr_fn_render_template(
        $loop_item['loop_item_tmpl'],//<= is a relative path
        $loop_item['loop_item_model']
      );
    
    }

    In my case, $loop_item[‘loop_item_tmpl’] = modules/grid/grid_wrapper.

    I also used the WordPress WP_Query examples for assistance in writing my code.

    Hi Jjbte,

    I’m not fully sure about it, you might need to find some developer who can help you with this.

    Have a pleasant day!

    Hi @jjbte,

    I am trying to achieve the same thing, as I want to display a query of multiple posts with a custom template.

    Currently I use the following code in my functions.php to hook to the loop of my template and modify the query:

    //setup hooks in the template_redirect action => once the main WordPress query is set
    add_action( 'template_redirect', 'hooks_setup' , 20 );
    function hooks_setup() {
        if (is_page(987236)) { // is tag page
            add_action( '__before_loop'     , 'tagpage_query' );
    	add_action( '__after_loop'      , 'tagpage_query', 100 );
        } else {
    	return;
        }
    }
    
    function tagpage_query() {
        global $wp_query, $wp_the_query;
        switch ( current_filter() ) {
        	case '__before_loop':
        		//replace the current query by a custom query
    		    //Note : the initial query is stored in another global named $wp_the_query
    			$tagpage = get_page_by_path( 'tag', 'OBJECT', 'page')->ID;
    			$args_for_query4 = array(
    				'post_type' => 'page',
    				'nopaging' => true,
    				'post_parent' => $tagpage,
    				'order' => 'ASC',
    				'orderby' => 'title',
    			);
    			$wp_query->posts = new WP_Query($args_for_query4)->posts;
    			$wp_query->post_count = $query4->post_count;
    			break;
        	default:
        		//back to the initial WP query stored in $wp_the_query
        		$wp_query = $wp_the_query;
    	        break;
        }
    }

    All the posts are displayed on the page, but I can’t figure out where to put your czr_fn_render_template code to get the grid pattern.

    Have you ever worked this out? Any help would be much appreciated. Thanks!

    Thread Starter jjbte

    (@jjbte)

    Hi @jojota,

    The czr_fn_render_template() function needs to be run within the while ($wp_query->have_posts()) loop.

    if ($wp_query->have_posts()) {
      while ($wp_query->have_posts()) {
        $wp_query->the_post();
        czr_fn_render_template(
          'modules/grid/grid_wrapper',
          array(
            'model_id'    => 'post_list_grid',
            'model_args'  => array(
              'grid_columns'    => 2,
              'image_centering' => 'js-centering',
              'show_thumb'      => 1
            )
          )
        );
      }
      wp_reset_postdata();
    }

    Thank you @jjbte for that fast response. I got it working now. My mistake was not modifying the $wp_query correctly.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Displaying a grid of page children’ is closed to new replies.