This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Post Grid Conditional Tag

  • Creator
    Topic
  • #5957
    askwpgirl
    Participant

    Jason,

    Can you think of an examples of ways to use a conditional statement with the template grid in the functions file that might work? For example, I find that one would want to significantly modify the content grid for one application of it, but not for the rest of the site. I may be asking too much, but I’m curious. I like to break things.

    For example: as illogical as this may sound, since the grid may not be aware of the page ID, here is what ideally I’d like to do:

    function my_template_parts( $parts ) {
         
          if ( is_page(4) )
    	   $parts['grid_paginated'] = 'mygrid';		
        
        return $parts;
    }
    add_filter( 'themeblvd_template_parts', 'my_template_parts' );

    Thank you for indulging me. No hurry.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Author
    Replies
  • #5959
    askwpgirl
    Participant

    Ah! I got it. I just had to get up above things. I hope you’re proud of me. I’m trying!

    $myquery = new WP_Query( 'page_id=24' );
    if ( $myquery ) 
    $parts['grid_paginated']	= 'mygrid';
    #5964
    askwpgirl
    Participant

    I realize this is heavy handed and breaks any custom queries that might be happen in the custom fields. So, I need to pass the page ID to the function and then do query on the page ID. Maybe give me some more time to figure this out. 🙂

    #5965
    Jason Bobich
    Keymaster

    I honestly don’t know that I follow the original issue fully. So, you just want to include a different template part if it’s a certain page, right? Why wouldn’t the original thing you posted work? Why would you be setting up a new query?

    function my_template_parts( $parts ) {
    
    	if ( is_page(4) )
    		$parts['grid_paginated'] = 'mygrid';		
    
    	return $parts;
    }
    add_filter( 'themeblvd_template_parts', 'my_template_parts' );
    #5982
    Jason Bobich
    Keymaster

    You and @timworx seem to be onto some sort of similar issue here that I’m just not following.

    http://support.themeblvd.com/forums/topic/multiple-post-grids/#post-5978

    #6009
    askwpgirl
    Participant

    Yes, for example, I have a page that uses the Grid Template. On this page, I want the template grid to look differently than on other pages that might use the Grid Template.

    For example, this is a portfolio page and it includes other data, such as custom fields, a different image dimension, etc. However, if (is_page(4)) does not work. I can get it to use the new content-mygrid.php by default or by passing the page ID via WP_Query to my conditional, but the normal is_page is not recognized. And, it’s probably that the grid does not know what page it is on.

    new WP_Query results in nothing being found if a custom field is also used on the page to retrieve posts from a particular category. I imagine it’s because you are also running WP-Query on the custom fields to retrieve the custom field data and the two queries are bashing into each other.

    I am not having layout issues, etc, that Tim is describing, so I don’t know what his deal is. I can get my content-mygrid.php to work fine, I just can’t select it conditionally for only a specific page.

    I’m thinking if the page ID could be passed to the filter, I could get it work.

    If you will try to use if (is_page(4)), you will see that you simply cannot conditionally set the content part based on the page ID for the grid template.

    #6015
    Jason Bobich
    Keymaster

    Ohhh, ok, I understand now. Hmm, here’s one idea I have. —

    You could get the current page ID earlier in the loading process and save it to some sort of global variable. Then, check that when you apply the themeblvd_template_parts filter.

    /**
     *  Set global post/page ID before anything alters the main WP query.
     */
    function my_set_global_post_id(){
    	global $my_post_id;
    	$my_post_id = get_the_ID();
    }
    add_action( 'wp', 'my_set_global_post_id' );
    
    /**
     *  Filter the template parts.
     */
    function my_template_parts( $parts ) {
    	global $my_post_id;
      
    	if ( $my_post_id == 4 )
    		$parts['grid_paginated'] = 'mygrid';		
    
    	return $parts;
    }
    add_filter( 'themeblvd_template_parts', 'my_template_parts' );

    This is just coming off the top of my head. Let me know if this doesn’t work. Also, make sure you’re using some sort of unique identifier in all the places I use “my” — Especially in this case, you don’t want to be throwing around random global variables named like “$post_id”.

    #6105
    askwpgirl
    Participant

    That worked, off the top of your head. Thanks! I’ll try not to throw any random global variables around. That could be dangerous.

    #6106
    Jason Bobich
    Keymaster

    lol what I meant is that the code was not tested or anything.

    #8403
    Jason Bobich
    Keymaster

    A note on this topic —

    As of Jump Start 1.1.0 (framework v2.3.0), you will no longer need this workaround. Template parts will now be established and filtered globally at the start of the framework’s loading process, opposed to down within the loop when actually calling the template part.

    In other words, simply doing this should work just fine:

    function my_template_parts( $parts ) {
    
    	if ( is_page(4) )
    		$parts['grid_paginated'] = 'mygrid';		
    
    	return $parts;
    }
    add_filter( 'themeblvd_template_parts', 'my_template_parts' );
Viewing 9 replies - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.