add_cap('edit_theme_options'); // 编辑主题选项
$role->add_cap('customize'); // 自定义器
$role->add_cap('switch_themes'); // 切换主题
$role->add_cap('edit_themes'); // 编辑主题
$role->add_cap('update_themes'); // 更新主题
$role->add_cap('install_themes'); // 安装主题
$role->add_cap('delete_themes'); // 删除主题
$role->add_cap('upload_themes'); // 上传主题
}
}
// 在主题激活时运行一次
add_action('after_switch_theme', 'enhance_editor_capabilities');
// 确保在插件加载后运行一次
add_action('init', 'enhance_editor_capabilities');
// 在导航菜单管理页面显示菜单ID
function nenghui_show_menu_id_in_nav_menus() {
// 只在nav-menus.php页面加载
global $pagenow;
if ($pagenow !== 'nav-menus.php') {
return;
}
// 获取所有菜单
$menus = wp_get_nav_menus();
if (empty($menus)) {
return;
}
?>
id === 'nav-menus') {
$screen->add_help_tab(array(
'id' => 'nenghui-menu-ids',
'title' => '菜单ID使用',
'content' => '
如何使用菜单ID
' .
'每个菜单都有一个唯一的数字ID,您可以在短代码中使用这些ID来指定特定的菜单。
' .
'About导航短代码示例:
' .
'' .
'[nenghui_about_nav] - 使用自定义器中设置的默认菜单 ' .
'[nenghui_about_nav menu_id="2"] - 使用ID为2的菜单 ' .
'[nenghui_about_nav menu_id="5"] - 使用ID为5的菜单 ' .
'
' .
'注意:菜单ID在页面顶部的信息框中显示,请使用正确的ID。
'
));
}
}
add_action('current_screen', 'nenghui_add_menu_help_info');
// 导入主题配置文件
// require_once 'theme-options.php'; // 暂时注释掉,文件不存在
// 导入小工具配置
require_once 'widgets/widgets-config.php';
// 刷新重写规则以确保FAQ归档页面正常工作
function nenghui_flush_rewrite_rules() {
// 只在主题激活时执行一次
if (get_option('nenghui_rewrite_rules_flushed') !== '1') {
flush_rewrite_rules();
update_option('nenghui_rewrite_rules_flushed', '1');
}
}
add_action('after_switch_theme', 'nenghui_flush_rewrite_rules');
// 当FAQ文章类型注册后刷新重写规则
function nenghui_flush_rewrite_rules_on_init() {
static $flushed = false;
if (!$flushed && get_option('nenghui_faq_rewrite_flushed') !== '1') {
flush_rewrite_rules();
update_option('nenghui_faq_rewrite_flushed', '1');
$flushed = true;
}
}
add_action('init', 'nenghui_flush_rewrite_rules_on_init', 999);
// 所有功能已拆分到独立的配置文件中:
// - 主题设置: inc/theme-setup.php
// - 后台管理: inc/admin-config.php
// - 资源加载: inc/assets-loader.php
// - 媒体配置: inc/media-config.php
// - 初始化配置: inc/init-config.php
// - 清理优化: inc/cleanup.php
// - 性能配置: inc/performance-config.php
// - 自定义器: inc/customizer.php
// - 短代码: inc/shortcode.php
/**
* 自定义导航菜单Walker类,支持多级下拉菜单
*/
class Nenghui_Walker_Nav_Menu extends Walker_Nav_Menu {
// 开始输出子菜单
function start_lvl(&$output, $depth = 0, $args = null) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent\n";
}
// 显示元素
function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output) {
$id_field = $this->db_fields['id'];
if (is_object($args[0])) {
$args[0]->has_children = !empty($children_elements[$element->$id_field]);
}
return parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
// 开始输出菜单项
function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) {
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
// 检查是否有子菜单
$has_children = !empty($args->has_children);
if ($has_children) {
$classes[] = 'menu-item-has-children';
}
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
$id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args);
$id = $id ? ' id="' . esc_attr($id) . '"' : '';
$output .= $indent . '';
$attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
$attributes .= ! empty($item->target) ? ' target="' . esc_attr($item->target ) .'"' : '';
$attributes .= ! empty($item->xfn) ? ' rel="' . esc_attr($item->xfn ) .'"' : '';
$attributes .= ! empty($item->url) ? ' href="' . esc_attr($item->url ) .'"' : '';
$item_output = isset($args->before) ? $args->before : '';
$item_output .= '';
$item_output .= (isset($args->link_before) ? $args->link_before : '') . apply_filters('the_title', $item->title, $item->ID) . (isset($args->link_after) ? $args->link_after : '');
// 如果有子菜单,添加下拉箭头
if ($has_children) {
$item_output .= ' ▼';
}
$item_output .= '';
$item_output .= isset($args->after) ? $args->after : '';
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
// 结束输出菜单项
function end_el(&$output, $item, $depth = 0, $args = null) {
$output .= "\n";
}
}
function get_post_meta_by_key($key, $cate_type) {
global $wpdb;
$table = $wpdb->postmeta;
$result = $wpdb->get_var($wpdb->prepare(
"SELECT meta_value FROM $table WHERE meta_key = %s AND post_id IN (
SELECT post_id FROM $table WHERE meta_key = 'cate_type' AND meta_value LIKE %s
) LIMIT 1",
$key,
'%' . $wpdb->esc_like($cate_type) . '%'
));
return $result;
}
add_action('wp_ajax_load_more_products', 'load_more_products_callback');
add_action('wp_ajax_nopriv_load_more_products', 'load_more_products_callback');
function load_more_products_callback() {
check_ajax_referer('load_more_products_nonce', 'nonce');
$offset = intval($_POST['offset']);
$per_page = intval($_POST['per_page']);
$category = isset($_POST['category']) ? sanitize_text_field($_POST['category']) : '';
$meta_query = array();
if (!empty($category) && $category !== 'all') {
$meta_query[] = array(
'key' => 'cate_type',
'value' => $category,
'compare' => 'LIKE'
);
}
$query_args = array(
'post_type' => 'products',
'posts_per_page' => $per_page,
'offset' => $offset,
'meta_query' => $meta_query,
'post_status' => 'publish'
);
$products_query = new WP_Query($query_args);
$products = array();
if ($products_query->have_posts()) {
while ($products_query->have_posts()) {
$products_query->the_post();
$post_id = get_the_ID();
$cate_type = get_post_meta($post_id, 'cate_type', true);
$cover_image = get_the_post_thumbnail_url($post_id, 'product-cover-medium');
if (empty($cover_image)) {
$cover_image = get_post_meta($post_id, '_product_banner_url', true);
}
if (empty($cover_image)) {
$cover_image = get_template_directory_uri() . '/assets/images/products-1.webp';
}
$usage_scenario_data = get_post_meta($post_id, '_usage_scenario_data', true);
$description = '';
if (!empty($usage_scenario_data) && isset($usage_scenario_data['bottom_text'])) {
$description = $usage_scenario_data['bottom_text'];
}
$attr_keys = get_post_meta($post_id, 'attr_key', true);
$attr_values = get_post_meta($post_id, 'attr_value', true);
$attr_keys_array = !empty($attr_keys) ? explode(',', $attr_keys) : array();
$attr_values_array = !empty($attr_values) ? explode(',', $attr_values) : array();
$attrs = array();
for ($i = 0; $i < min(4, count($attr_keys_array)); $i++) {
if (isset($attr_keys_array[$i]) && isset($attr_values_array[$i])) {
$attrs[] = array(
'key' => trim($attr_keys_array[$i]),
'value' => trim($attr_values_array[$i])
);
}
}
$efficiency = get_post_meta($post_id, 'efficiency', true);
$is_new = get_post_meta($post_id, 'is_new', true);
$is_new_value = filter_var($is_new, FILTER_VALIDATE_BOOLEAN);
ob_start();
?>
ob_get_clean()
);
}
wp_reset_postdata();
}
wp_send_json_success(array('products' => $products));
}
?>