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.

Custom template part file for Grid Shortcode

  • Creator
    Topic
  • #13830
    Andre Goersch
    Participant

    The title should be pretty self-explanatory, but I have a custom post type for which I use a custom template part file to display different information with each post. It works fine following the JumpStart’s documentation for both the custom post type and a related taxonomy’s archives, except that the “initial” page I use to display the list of posts uses a Post Grid shortcode (since my client wants to have control over some additional content displayed above the grid).

    I can setup the shortcode without problems to display posts from that custom post type, except that it is not using the template part I setup. Here is the code I’m using to change the template files:

    function my_template_parts( $parts ) {
        if ( is_post_type_archive('franquia') ) {
            $parts['archive'] = 'franquia-grid';
        }
        if ( is_tax( 'praca_interesse' ) ) {
            $parts['archive'] = 'franquia-grid';
        }
        return $parts;
    }
    add_filter( 'themeblvd_template_parts', 'my_template_parts' );
    
    /**
     * Manually trigger grid mode.
     */
    function my_theme_mode_override( $mode ) {
        if( is_post_type_archive('franquia') || is_tax( 'praca_interesse' ) ) {
            $mode = 'grid';
        }
        return $mode;
    }
    add_filter( 'themeblvd_theme_mode_override', 'my_theme_mode_override' );

    Here is the page I want to change the grid format: http://bastockler.com.br/wordpress/franqueado/

    And this is the archive for the same custom post type: http://bastockler.com.br/wordpress/franquia/

    The different isn’t that big, but I needed all images to have links automatically, and instead of a “Read More” button, I need that button that opens a modal form.

    Is there an easy way to accomplish this, or do I need to make a complete new template with a custom WP_Query?

    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Author
    Replies
  • #13837
    Jason Bobich
    Keymaster

    Hello,

    There are two filters at play here:

    • themeblvd_template_parts — Applied to all template parts as one array before the loop.
    • themeblvd_template_part (no “s”) — Applied to each individual template part called within the loop.

    So, currently you’re using that first one, but I think you want to use the second one. Here’s an example of what I think you’re going for:

    function my_template_part( $part, $type ) {
    
    	if ( $type == 'archive' ) {
    
    		if ( is_post_type_archive('franquia') || is_tax( 'praca_interesse' )  ) {
    	        $part = 'franquia-grid';
    	    }
    
    	}
    
    	return $part;
    }
    add_filter( 'themeblvd_template_part', 'my_template_part', 10, 2 );
    #13839
    Andre Goersch
    Participant

    I tried your code, but I’m having the same result, the actual archive pages work fine, but not that shortcode. Doesn’t the fact that I’m using the shortcode on a normal page requires different expressions?

    #13840
    Jason Bobich
    Keymaster

    Aw, yeah missed that you meant in the shortcode on a page. So, you’d need to adjust your conditional accordingly. You could is_page('whatever') — with that page where you’re using the shortcode. But the shortcode itself isn’t happening in the primary loop. So the the loop happening for the shortcode output doesn’t correspond to the WordPress conditional tags, if that makes sense.

    #13841
    Andre Goersch
    Participant

    That’s the part that is getting me. I tried something similar before, but if I just create a check like…

    if ( is_page('franqueado') ) {
    	$part = 'franquia-grid';
    }

    … what happens is that the entire loop for the page “franqueado” changes to my custom template, displaying the normal page content as a single element of the grid. I don’t know how to limit the change to the secondary loop the shortcode creates.

    #13842
    Andre Goersch
    Participant

    I found the function is_main_query() that might work here, but from the documentation it looks like I need to compare it to a query object… which I don’t have.

    Am I on the right path here?

    #13843
    Jason Bobich
    Keymaster

    Try using our conditional like this:

    if ( themeblvd_was( 'page', 123 ) ) {
    	$part = 'franquia-grid';
    }

    Make sure to use the numerical page ID and not the slug. Does that work?

    #13844
    Andre Goersch
    Participant

    That gives the same result as using is_page('franqueado'), it changes the main loop to use that template part.

    #13845
    Jason Bobich
    Keymaster

    Aw, probably because you’re not also checking the $type variable? You need to make sure $type is equal to ‘grid’ first before your conditional.

    function my_template_part( $part, $type ) {
    
    	if ( $type == 'archive' ) {
    
    		if ( is_post_type_archive('franquia') || is_tax( 'praca_interesse' )  ) {
    	        $part = 'franquia-grid';
    	    }
    
    	} else if ( $type == 'grid' ) {
    		
    		if ( themeblvd_was( 'page', 123 ) ) {
    			$part = 'franquia-grid';
    		}
    	
    	}
    
    	return $part;
    }
    add_filter( 'themeblvd_template_part', 'my_template_part', 10, 2 );
    #13846
    Andre Goersch
    Participant

    Bingo, we have a winner. That works perfectly. I wasn’t really clear on what was the purpose of the $type parameter before, but now it makes sense.

    Thanks very much Jason, you’ve been great help as always.

    #14428
    Eleven Sites
    Participant

    I’m so glad I found this thread. I’ve got to figure out how to make some templates for displaying my custom post types, and I’m excited to work through this using the Template Parts documentation and these code examples. Thanks for sharing!

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.