[Support request] Custom page templates (WordPress version page-templatename.php) and Elements

Home Forums Support [Support request] Custom page templates (WordPress version page-templatename.php) and Elements

Home Forums Support Custom page templates (WordPress version page-templatename.php) and Elements

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2171659
    Michelle

    Hi, is there any way to set Elements to be active/inactive based on the Page Template? I mean the WordPress version of that (https://developer.wordpress.org/themes/template-files-section/page-template-files/), ie page-templatename.php as set in the GeneratePress child theme and selected on a specific Page.

    If not, here’s what I’m trying to solve for – maybe there is a better way to do this. ๐Ÿ™‚

    Right now I have two Page Hero Elements, one that shows the Featured Image with fallback, and another that has a different layout/design and no Featured Image.

    I want editors to have an easy way to switch between one and the other on a per-page basis, without having to go into Appearance > Elements and set up a bunch of inclusion/exclusion rules for each individual page. That’s going to be too clunky and difficult to maintain for sites with hundreds of pages.

    My thought was to use the old-school Page Template setup to handle this, and tie the template to my already created Page Hero Element. But Page Templates don’t appear in the rules area for Elements.

    Would it be possible to add that kind of rule? I am used to working with ACF where that’s an option and am still learning my way through GeneratePress / GenerateBlocks. I have pro versions of everything.

    Thanks!

    #2171863
    Elvin
    Staff
    Customer Support

    Hi Michelle,

    There are multiple ways of doing this.

    One way is by creating a template php file. See David’s solution here – https://generatepress.com/forums/topic/old-school-wordpress-templates/#post-2168183

    It’s slightly different but the concept behind the this seems similar to what you’re trying to do.

    Another way is by adding a metadata to the page using ACF and then use this metadata value to identify which GP element should be used.

    Using ACF, you can set a dropdown field. The dropdown field values can be used on this filter. https://docs.generatepress.com/article/generate_element_display/

    Using this filter and the metadata, we can do with some PHP snippets that may look something like this –

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        $template_dropdown = get_field( "template_dropdown", get_the_ID() ); //get ACF value of the current page.
    
        if ( !empty( $template_dropdown ) && is_page() ) { //checks if ACF field for dropdown has selected value.
            if( 1 === $element_id ){ //target element 1
                if ( $template_dropdown == 'template_1' ) { //use template 1 if current page's ACF dropdown value is 'template_1'
                    $display = true;
                } else { //else, don't use this GP Element
                    $display = false;
                }
            } if( 2 === $element_id ){ //target element 2
                if ( $template_dropdown == 'template_2' ) { //use template 2 if current page's ACF dropdown value is 'template_2'
                    $display = true;
                } else { //else, don't use this GP Element
                    $display = false;
                }
            }
            
            
        }
    
    return $display;
    }, 10, 2 );

    A wise man once said:
    "Have you cleared your cache?"

    #2172733
    Michelle

    Thanks Elvin! I wasn’t aware of that filter, that’s just what I needed. In case anyone else needs it here’s how I solved for this, using a WordPress page template + elements.

    First I created a page template in my GeneratePress child theme named page-header-alt.php. I’ve posted that file here for anyone who wants to see it: https://gist.github.com/FriendlyWP/1337828a651ecf7bd1249eb40c6ce0a2

    I had already created two Page Hero Elements in my control panel, with IDs of 53 (default) and 66 (alt).

    I then added the following in my child theme’s functions.php:

    // Switch to use correct Element when WordPress Page Template "No Image Header" is set for specific Post or Page.
    // Note this assumes Element 66's Display Rules are set to "Exclude - Entire Site" and Element 53's are set to "Display - Entire Site"
    add_filter( 'generate_element_display', function( $display, $element_id ) {
    
      $page_template = get_page_template_slug( get_the_ID() );
    
      if ( !empty( $page_template )  ) { 
        if ($page_template == 'page-header-alt.php') {
          if ( 53 === $element_id ) { // Don't show Element ID 53 "Page Header - Default"
              $display = false;
          } elseif ( 66 === $element_id ) { // Do Show Element ID 66 "Page Header - Alt"
              $display = true;
          }
        }  
      } 
    
    return $display;
    }, 10, 2 );
    #2173128
    Elvin
    Staff
    Customer Support

    Nice one! Thanks for sharing it with us. Glad you got it sorted. ๐Ÿ˜€

    A wise man once said:
    "Have you cleared your cache?"

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