php ACF站点设置选项页面+代码段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php ACF站点设置选项页面+代码段相关的知识,希望对你有一定的参考价值。

// ACF Options

if( function_exists('acf_add_options_page') ) {


		acf_add_options_page(array(
			'page_title' 	=> 'Site Settings',
			'menu_title'	=> 'Site Settings',
			'menu_slug' 	=> 'site-settings',
			'capability'	=> 'edit_posts',
			'redirect'		=> false
		));


	acf_add_options_page(array(
		'page_title' 	=> 'Code Snippets',
		'menu_title'	=> 'Code Snippets',
		'menu_slug' 	=> 'theme-code-snippets',
		'capability'	=> 'edit_posts',
		'icon_url'	=> 'dashicons-media-code',
    'position' => 101,
		'redirect'		=> true
	));


  acf_add_options_sub_page(array(
      'page_title' 	=> 'Code Snippets for </body> Tag',
      'menu_title'	=> '</body> Tag',
      'parent_slug'	=> 'theme-code-snippets',
    ));


  acf_add_options_sub_page(array(
  		'page_title' 	=> 'Code Snippets for <head> Tag',
  		'menu_title'	=> '<head> Tag',
  		'parent_slug'	=> 'theme-code-snippets',
  	));

}


function code_snippets_head() {

	if( have_rows('code_snippets__head', 'option') ): ?>
	<?php // loop through the rows of data
		while ( have_rows('code_snippets__head', 'option') ) : the_row(); ?>
<?php the_sub_field('code', 'option'); ?>
	<?php endwhile; ?>
	<?php
	else :
		// no rows found
	endif;

}
add_action( 'wp_head', 'code_snippets_head', 100 );


function code_snippets_body() {

	if( have_rows('code_snippets__body', 'option') ): ?>
	<?php // loop through the rows of data
		while ( have_rows('code_snippets__body', 'option') ) : the_row(); ?>
<?php the_sub_field('code', 'option'); ?>
	<?php endwhile; ?>
	<?php
	else :
		// no rows found
	endif;

}
add_action( 'wp_footer', 'code_snippets_body', 100 );


// the ACF fields

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array (
	'key' => 'group_562fb3f3bc547',
	'title' => 'Code snippets: Body',
	'fields' => array (
		array (
			'key' => 'field_562fb3f3bfa7f',
			'label' => 'Code Snippets',
			'name' => 'code_snippets__body',
			'type' => 'repeater',
			'instructions' => 'Add code that belongs before the closing &lt;body&gt; tag',
			'required' => 0,
			'conditional_logic' => 0,
			'wrapper' => array (
				'width' => '',
				'class' => '',
				'id' => '',
			),
			'min' => 0,
			'max' => 0,
			'layout' => 'row',
			'button_label' => '+ Add Snippet',
			'collapsed' => '',
			'sub_fields' => array (
				array (
					'key' => 'field_562fb3f3c14f9',
					'label' => 'Code',
					'name' => 'code',
					'type' => 'textarea',
					'instructions' => '',
					'required' => 0,
					'conditional_logic' => 0,
					'wrapper' => array (
						'width' => '',
						'class' => '',
						'id' => '',
					),
					'default_value' => '',
					'placeholder' => '',
					'maxlength' => '',
					'rows' => '',
					'new_lines' => '',
					'readonly' => 0,
					'disabled' => 0,
				),
			),
		),
	),
	'location' => array (
		array (
			array (
				'param' => 'options_page',
				'operator' => '==',
				'value' => 'acf-options-body-tag',
			),
		),
	),
	'menu_order' => 0,
	'position' => 'normal',
	'style' => 'default',
	'label_placement' => 'top',
	'instruction_placement' => 'label',
	'hide_on_screen' => '',
	'active' => 1,
	'description' => '',
));

acf_add_local_field_group(array (
	'key' => 'group_562f9de4e4fbb',
	'title' => 'Code snippets: Head',
	'fields' => array (
		array (
			'key' => 'field_562f9deba8199',
			'label' => 'Code Snippets',
			'name' => 'code_snippets__head',
			'type' => 'repeater',
			'instructions' => 'Add code that belongs inside the &lt;head&gt; tags',
			'required' => 0,
			'conditional_logic' => 0,
			'wrapper' => array (
				'width' => '',
				'class' => '',
				'id' => '',
			),
			'min' => 0,
			'max' => 0,
			'layout' => 'row',
			'button_label' => '+ Add Snippet',
			'collapsed' => '',
			'sub_fields' => array (
				array (
					'key' => 'field_562f9df9a819a',
					'label' => 'Code',
					'name' => 'code',
					'type' => 'textarea',
					'instructions' => '',
					'required' => 0,
					'conditional_logic' => 0,
					'wrapper' => array (
						'width' => '',
						'class' => '',
						'id' => '',
					),
					'default_value' => '',
					'placeholder' => '',
					'maxlength' => '',
					'rows' => '',
					'new_lines' => '',
					'readonly' => 0,
					'disabled' => 0,
				),
			),
		),
	),
	'location' => array (
		array (
			array (
				'param' => 'options_page',
				'operator' => '==',
				'value' => 'acf-options-head-tag',
			),
		),
	),
	'menu_order' => 0,
	'position' => 'normal',
	'style' => 'default',
	'label_placement' => 'top',
	'instruction_placement' => 'label',
	'hide_on_screen' => '',
	'active' => 1,
	'description' => '',
));

endif;

以上是关于php ACF站点设置选项页面+代码段的主要内容,如果未能解决你的问题,请参考以下文章

php 使用自定义post_id为ACF选项页面设置正确的语言

php 添加此代码以将ACF字段添加到页面/帖子。务必检查wordpress仪表板顶部菜单中的屏幕选项

php 使用ACF添加页面选项

php ACF选项页面

php ACF Pro | Cusom选项页面

php ACF Pro |自定义选项页面