Support

Account

Home Forums General Issues Second time posting this issue….Haven't gotten a response. Please Help!

Solving

Second time posting this issue….Haven't gotten a response. Please Help!

  • Hello,

    In my main/parent theme, I have created a custom post type and taxonomy. I’ve also created several posts using the custom post type. When I create the post, I select the post template.

    My post template PHP file starts with this at the top

    /**
    * Template Name: Staff Member
    * Template Post Type: post, staff-member
    */

    Now in my custom field group location, I have set the Post Template = Staff Member. So depending on which post template is selected it will display my custom field group.

    Everything works the way it should in my parent theme. When I select the dropdown for the Post Template, it shows all my post templates but when I active my child theme it doesn’t load my post template options in the dropdown. It only loads one which is “default template.” When I switch back to my parent theme, it works again, and I can see my list of post templates.

    Any ideas? I’m using ACF Pro 5.5.14

  • I looked at your original request and I’ve been looking for time to test things. You need to submit a new support ticket here https://support.advancedcustomfields.com/new-ticket/ It seems that ACF is not detecting template files for anything other than pages in child themes.

  • Thanks @hube2 I just submitted it!

  • Just got hit by this. Hoping to hear about a resolution soon.

  • Your best bet to hear anything is to submit a support ticket. Unless the OP has heard something. I don’t know the status of this. https://support.advancedcustomfields.com/new-ticket/

  • Ok thanks @hube2. I had assumed a ticket already existed for this.

    I did some digging and turns out it might be a bug in WP. ACF uses wp_get_theme()->get_post_templates(); to get available templates as it probably should but WP_Theme::get_post_templates() fails to request files from the parent theme folder and there are no params or hooks to force it to.
    I created a ticket on trac. https://core.trac.wordpress.org/ticket/41717

    Going to take a shot at a workaround and I’ll submit here and in a new ticket if it works.

  • I was able to modify /includes/locations/class-acf-location-post-template.php with a workaround. I’ll post the whole file here for clarity. I added a class method acf_location_post_template::acf_get_post_templates() then changed both calls to the native get_post_templates()

    
    <?php 
    
    if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    if( ! class_exists('acf_location_post_template') ) :
    
    class acf_location_post_template extends acf_location {
    	
    	
    	/*
    	*  __construct
    	*
    	*  This function will setup the class functionality
    	*
    	*  @type	function
    	*  @date	5/03/2014
    	*  @since	5.0.0
    	*
    	*  @param	n/a
    	*  @return	n/a
    	*/
    	
    	function initialize() {
    		// vars
    		$this->name = 'post_template';
    		$this->label = __("Post Template",'acf');
    		$this->category = 'post';
    		$this->public = acf_version_compare('wp', '>=', '4.7');
        	
    	}
    	
    	
    	/*
    	*  get_post_type
    	*
    	*  This function will return the current post_type
    	*
    	*  @type	function
    	*  @date	25/11/16
    	*  @since	5.5.0
    	*
    	*  @param	$options (int)
    	*  @return	(mixed)
    	*/
    	
    	function get_post_type( $screen ) {
    		
    		// vars
    		$post_id = acf_maybe_get( $screen, 'post_id' );
    		$post_type = acf_maybe_get( $screen, 'post_type' );
    		
    		
    		// post_type
    		if( $post_type ) return $post_type;
    		
    		
    		// $post_id
    		if( $post_id ) return get_post_type( $post_id );
    		
    		
    		// return
    		return false;
    		
    	}
    	
    	
    	/*
    	*  rule_match
    	*
    	*  This function is used to match this location $rule to the current $screen
    	*
    	*  @type	function
    	*  @date	3/01/13
    	*  @since	3.5.7
    	*
    	*  @param	$match (boolean) 
    	*  @param	$rule (array)
    	*  @return	$options (array)
    	*/
    	
    	function rule_match( $result, $rule, $screen ) {
    		
    		// vars
    		$templates = array();
    		$post_id = acf_maybe_get( $screen, 'post_id' );
    		$page_template = acf_maybe_get( $screen, 'page_template' );
    		$post_type = $this->get_post_type( $screen  );
    		
    		// bail early if no post_type found (not a post)
    		if( !$post_type ) return false;
    		
    		
    		// get templates (WP 4.7)
    		if( acf_version_compare('wp', '>=', '4.7') ) {
    			
    			// $templates = wp_get_theme()->get_post_templates();
    			$templates = $this->acf_get_post_templates();
    			
    		}
    		
    		
    		// 'page' is always a valid pt even if no templates exist in the theme
    		// allows scenario where page_template = 'default' and no templates exist
    		if( !isset($templates['page']) ) {
    			
    			$templates['page'] = array();
    			
    		}
    		
    		
    		// bail early if this post type does not allow for templates
    		if( !isset($templates[ $post_type ]) ) return false;
    		
    		
    		// get page template
    		if( !$page_template ) {
    		
    			$page_template = get_post_meta( $post_id, '_wp_page_template', true );
    			
    		}
    		
    		
    		// new post - no page template
    		if( !$page_template ) $page_template = "default";
    		
    		
    		// match
    		return $this->compare( $page_template, $rule );
    		
    	}
    	
    	
    	/*
    	*  rule_operators
    	*
    	*  This function returns the available values for this rule type
    	*
    	*  @type	function
    	*  @date	30/5/17
    	*  @since	5.6.0
    	*
    	*  @param	n/a
    	*  @return	(array)
    	*/
    	
    	function rule_values( $choices, $rule ) {
    		
    		// vars
    		$choices = array(
    			'default' => apply_filters( 'default_page_template_title',  __('Default Template', 'acf') )
    		);
    		
    		
    		// get templates (WP 4.7)
    		if( acf_version_compare('wp', '>=', '4.7') ) {
    			// $templates = wp_get_theme()->get_post_templates();
    			$templates = $this->acf_get_post_templates();
    			$choices = array_merge($choices, $templates);
    		}
    		
    		
    		// return choices
    		return $choices;
    		
    	}
    
    	function acf_get_post_templates() {
    		$post_types = acf_get_post_types(array(
    			'exclude'	=> array('attachment')
    		));
    		$post_templates = array();
    		foreach ($post_types as $post_type) {
    			if ( $files = wp_get_theme()->get_page_templates(null, $post_type) ) {
    				if ( ! isset( $post_templates[ $post_type ] ) ) {
    					$post_templates[ $post_type ] = array();
    				}
    				$post_templates[ $post_type ] = $files;
    			}
    		}
    		return $post_templates;
    	}
    	
    }
    
    // initialize
    acf_register_location_rule( 'acf_location_post_template' );
    
    endif; // class_exists check
    
    ?>
    
  • Thanks for the work-a-round code, this too should go to the dev in a ticket. I help out here on the forum, but I’m not part of the support that works the ticket system, so I really don’t have a clue what tickets there are or the status of them… unless someone says something about them here.

    It sounds to me like the problem is in core WP if, but I just tried following get_post_templates() and basically got confused so I can tell if it parent theme templates are ever loaded or not. Just checked the WP ticket and what your saying there makes sense to me, also following it there. I’ll be very interested to see how this works out.

    I haven’t looked into this myself because I haven’t had the need for post type templates yet… but I am planing to use them as soon a reason to do so presents itself. Honestly, I have something in the works that will need this but I just haven’t gotten that far yet.

  • I created a acf ticket but don’t have a link to it yet.
    For my situation a post template makes sense. I created an online training system where each lesson is comprised of a series of Topics which are custom post types. Some topics include a piece of audio which requires its own acf group. Being able to leverage all of the core topic logic and yet create an alternative version via a template is a perfect solution.

  • Just a quick update. For an upgrade-proof workaround, just save the entire contents of the file I posted earlier in this thread to your theme and use require_once within your functions.php file.
    require_once 'class-acf-location-post-template.php
    It will override the built in one and provide the fix.

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Second time posting this issue….Haven't gotten a response. Please Help!’ is closed to new replies.