php 使用自定义post_id为ACF选项页面设置正确的语言
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 使用自定义post_id为ACF选项页面设置正确的语言相关的知识,希望对你有一定的参考价值。
<?php
class ACF_SampleOptions_Page {
static $slug = 'sample_options_slug';
static $post_id = 'sample_options_id';
public static function init() {
// This sets the options page `post_id` to the correct value based on the current language
if ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE !== 'en' ) {
self::$post_id .= '_' . ICL_LANGUAGE_CODE;
}
if ( function_exists( 'acf_add_local_field_group' ) ) {
acf_add_local_field_group( array(
'key' => self::$slug,
'title' => __( 'Sample Options', 'rmg' ),
'fields' => array(
[
'key' => self::$slug . 'samplefield1',
'label' => __( 'Basic Text Field', 'textdomain' ),
'name' => 'sample_field_1',
'type' => 'text',
'instructions' => '',
],
[
'key' => self::$slug . 'samplefield2',
'label' => __( 'Basic Text Field', 'textdomain' ),
'name' => 'sample_field_2',
'type' => 'text',
'instructions' => '',
],
[
'key' => self::$slug . 'samplefield3',
'label' => __( 'Basic Text Field', 'textdomain' ),
'name' => 'sample_field_3',
'type' => 'text',
'instructions' => '',
],
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => self::$slug,
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array(
'position' => 'normal',
'layout' => 'default',
'hide_on_screen' => array(),
),
'menu_order' => 0,
) );
}
if ( function_exists( 'acf_add_options_sub_page' ) ) {
acf_add_options_sub_page( array(
'menu_title' => 'Sample Options',
'menu_slug' => self::$slug,
'post_id' => self::$post_id,
'parent_slug' => 'edit.php?post_type=page'
) );
}
}
}
add_action( 'acf/init', [ 'ACF_SampleOptions_Page', 'init' ] );
// To retrieve the value from ACF you can use a call like this:
$field_value = get_field( 'sample_field_1', ACF_SampleOptions_Page::$post_id );
以上是关于php 使用自定义post_id为ACF选项页面设置正确的语言的主要内容,如果未能解决你的问题,请参考以下文章
php 使用ACF选项页面的WP全局自定义字段
php ACF Pro |自定义选项页面
php ACF Pro - 自定义帖子类型的选项页面
php Sage 9 ACF自定义帖子类型和选项页面
Wordpress ACF 字段如何从自定义帖子类型中获取选项
php ACF的自定义TinyMCE选项