Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress可以';t访问globals and functions.php类别编辑页面_Php_Wordpress - Fatal编程技术网

Wordpress可以';t访问globals and functions.php类别编辑页面

Wordpress可以';t访问globals and functions.php类别编辑页面,php,wordpress,Php,Wordpress,我在functions.php中获得了注册的边栏列表函数 在post选项页面(wp admin/post.php)上,一切都非常完美 我可以通过我的函数获得所有已注册的边栏 但是在类别编辑页面(wp admin/edit tags.php)中, 我甚至无法访问全局变量$wp_registered_侧栏,更不用说函数本身了 下面是函数 function sidebars_list(){ global $wp_registered_sidebars; $sidebar = array(); if

我在functions.php中获得了注册的边栏列表函数 在post选项页面(wp admin/post.php)上,一切都非常完美 我可以通过我的函数获得所有已注册的边栏

但是在类别编辑页面(wp admin/edit tags.php)中, 我甚至无法访问全局变量$wp_registered_侧栏,更不用说函数本身了

下面是函数

function sidebars_list(){

global $wp_registered_sidebars;
$sidebar = array();

if (!empty($wp_registered_sidebars)):
  foreach ($wp_registered_sidebars as $key => $access):
      $sidebar[$key] = $access['name'];
  endforeach;
endif;

var_dump($sidebar);

return $sidebar;
}
正如我所说,它可以完美地在后端编辑文章和页面(前端也是如此)

我试着加上它,运气不好。 甚至无法访问“类别编辑”页面中的全局变量

global $wp_registered_sidebars;
var_dump($wp_registered_sidebars);
返回空数组。 但是当我在函数中转储var_时,它会按预期返回。
出了什么问题?

在浪费了大量的时间搜索并想出了这个解决方案之后。在加载侧边栏值之前,先通过类别页面进行访问。因此,您可以在该位置放置占位符div,并在加载页面后使用Ajax&jQuery在下拉列表或复选框中加载值,从而进行变通。那对你有用

在主题的functions.php中粘贴以下代码

// Call Actio to modify form on Add New Category
add_action( 'category_add_form_fields', 'edit_category_fields');

// Call Action to modify form on Edit Category
add_action( 'category_edit_form_fields', 'edit_category_fields');


function edit_category_fields($tag, $taxonomy)
{
    // Get the Current Value if any
    $leftsidebar_to_show = get_term_meta( $tag->term_id, 'leftsidebar_to_show', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="leftsidebar_to_show">Select Left Sidebar to Show</label></th>
        <td>
            <div id="leftsidebar_to_show_wrap">
                <select name="leftsidebar_to_show" id="leftsidebar_to_show">

                </select>
            </div>
            <!-- Store the current value as hidden input in order to Get Selected Option in jQuery -->
            <input type="hidden"  id="leftsidebar_to_show_val" value="<?php echo $leftsidebar_to_show; ?>" />

            <!-- Category ID as hidden Input -->
            <input type="hidden" name="term_id_val" value="<?php echo $tag->term_id; ?>" />
        </td>
    </tr>

    <?php
}

// Call Actio to Save Values on Add New Category
add_action( 'edited_category', 'save_category', 10, 2);

// Call Action to Save Values on Edit Category
add_action( 'create_category', 'save_category', 10, 2);

function save_category(){  
    update_term_meta( $_POST['term_id_val'], 'leftsidebar_to_show', $_POST['leftsidebar_to_show']);
}

// Function to enqueue Javascript file in admin
function my_enqueue($hook) {
    wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/js/sidebars.js' );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

// Action Function to get Sidebars through Ajax Call
function prefix_ajax_get_sidebars() {
    $sidebarval = $_REQUEST['sidebarval'];
    $sidebarid = $_REQUEST['sidebarid'];
    $string = '<select id="'.$sidebarid.'" name="'.$sidebarid.'">';
    foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) {
        if($sidebarval == $sidebar['id']){
            $selected = ' selected';
        }else{
            $selected = '';
        }
        $string .= '<option value="'.$sidebar['id'].'"'.$selected.'>'.$sidebar['name'].'</option>';
    }
    $string .= '</select>';
    echo $string;
    die();
}
add_action( 'wp_ajax_get_sidebars', 'prefix_ajax_get_sidebars' );
add_action('wp_ajax_nopriv_get_sidebars', 'prefix_ajax_get_sidebars');
并将上面创建的文件移动到主题的js文件夹中。现在,您可以在添加和编辑类别表单中看到作为下拉列表列出的侧栏

希望这是清楚的


谢谢

正如您所说,
全局
变量值未找到。我假设您的变量被
空数组
覆盖了该值

我建议您更改变量名,然后使用var_转储变量重试

global $wp_registered_sidebars_custom;
var_dump($wp_registered_sidebars_custom);

我不明白你为什么要这么做。类别编辑页面上运行的钩子和过滤器很早就开始运行了,它们甚至在侧边栏注册之前就运行了,这就是为什么你会得到一个空数组你不明白是什么?我需要这样做,然后告诉我一种获取侧边栏的方法?为什么在类别编辑页面中需要侧边栏?我将为每个类别的自定义侧边栏列出它们
global $wp_registered_sidebars_custom;
var_dump($wp_registered_sidebars_custom);