Support

Account

Home Forums General Issues Generate php – Where and how to put the code? Reply To: Generate php – Where and how to put the code?

  • But I only want to use the field group on specific template files.

    I’ve created a new php file acf-deals.php and uploaded it to the /php-includes/ directory in the root directory. It includes the following code:

    <?php
    
    if( function_exists('acf_add_local_field_group') ):
    
    acf_add_local_field_group(array(
    	'key' => 'group_deal1',
    	'title' => 'Deal One (displayed on homepage)',
    	'fields' => array(
    		array(
    			'key' => 'field_5c66e017f8359',
    			'label' => 'Title',
    			'name' => 'deal_title_one',
    			'type' => 'text',
    			'instructions' => '(max. 100 characters)',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-title',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '',
    			'append' => '',
    			'maxlength' => 100,
    		),
    		array(
    			'key' => 'field_5c6d43781bcb1',
    			'label' => 'Deal or Discount',
    			'name' => 'discount_deal_or_special_one',
    			'type' => 'radio',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => array(
    				array(
    					array(
    						'field' => 'field_5c66e017f8359',
    						'operator' => '!=empty',
    					),
    				),
    			),
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'choices' => array(
    				'Discount' => 'Discount',
    				'Deal' => 'Deal',
    			),
    			'allow_null' => 1,
    			'other_choice' => 0,
    			'default_value' => '',
    			'layout' => 'vertical',
    			'return_format' => 'value',
    			'save_other_choice' => 0,
    		),
    		array(
    			'key' => 'field_5c66e094f835a',
    			'label' => 'Description',
    			'name' => 'deal_description_one',
    			'type' => 'textarea',
    			'instructions' => '(max. 600 characters)',
    			'required' => 0,
    			'conditional_logic' => array(
    				array(
    					array(
    						'field' => 'field_5c66e017f8359',
    						'operator' => '==empty',
    					),
    				),
    			),
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-description',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'maxlength' => 600,
    			'rows' => '',
    			'new_lines' => '',
    		),
    		array(
    			'key' => 'field_5c66e0d4f835b',
    			'label' => 'Regular price',
    			'name' => 'regular_price_one',
    			'type' => 'number',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-regular-price',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '$',
    			'append' => '',
    			'min' => '',
    			'max' => '',
    			'step' => '',
    		),
    		array(
    			'key' => 'field_5c66e130f835c',
    			'label' => 'Discounted price / deal price',
    			'name' => 'discounted_price_one',
    			'type' => 'number',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-discounted-price',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '$',
    			'append' => '',
    			'min' => '',
    			'max' => '',
    			'step' => '',
    		),
    		array(
    			'key' => 'field_5c703c1fd5851',
    			'label' => 'Valid until',
    			'name' => 'valid_until_one',
    			'type' => 'date_picker',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'display_format' => 'm/d/Y',
    			'return_format' => 'm/d/Y',
    			'first_day' => 1,
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'post',
    			),
    			array(
    				'param' => 'post_format',
    				'operator' => '==',
    				'value' => 'aside',
    			),
    		),
    		array(
    			array(
    				'param' => 'post_format',
    				'operator' => '==',
    				'value' => 'status',
    			),
    		),
    	),
    	'menu_order' => 1,
    	'position' => 'acf_after_title',
    	'style' => 'default',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => 1,
    	'description' => '',
    ));
    
    endif;
    
    ?>

    I’ve added this code to my single-aside.php file:

    <?php get_template_part( '/php-includes/acf-deals.php', 'acf-deals' ); ?>

    Should that not work?