php WooCommerce我的帐户自定义选项卡与自定义内容

idfiyjo8  于 2023-03-11  发布在  PHP
关注(0)|答案(1)|浏览(195)

请让我知道如何添加多个自定义菜单选项卡由端点与结果谁从数据库获得?下面的代码工程,但显示相同的内容为所有我的自定义菜单项,而有3个结果从数据库,因为你看到的图片

function pa_custom_endpoint_keys() {
    global $wpdb;
    $charset_collate = $wpdb->get_charset_collate();
    $pao  = $wpdb->prefix . 'pao';
    $results=$wpdb->get_results("select * from $pao");
        $endpointsdata =array();
        foreach($results as $row){
        $endpointsdata[$row->pao_name] = $row->pao_value;
        }
        return $endpointsdata;
}

add_action( 'init', 'pa_custom_endpoint' );
function pa_custom_endpoint() {
    foreach(pa_custom_endpoint_keys() as $endpointkey=>$endpointlable){
    add_rewrite_endpoint( $endpointkey, EP_ROOT | EP_PAGES );
    }
}
//
add_filter( 'query_vars', 'pa_custom_endpoint_query_vars', 0 );
function pa_custom_endpoint_query_vars( $vars ) {
    foreach(pa_custom_endpoint_keys() as $endpointkey=>$endpointlable){
    $vars[] = $endpointkey;
    }
    return $vars;
}
//
add_filter( 'woocommerce_account_menu_items', 'pa_custom_endpoint_link_my_account' );
function pa_custom_endpoint_link_my_account( $items ) {
    foreach(pa_custom_endpoint_keys() as $endpointkey=>$endpointlable){
    $items[$endpointkey] = $endpointlable;
    }
    return $items;
}
//
function pa_custom_endpoint_content() {
        foreach(pa_custom_endpoint_keys() as $endpointkey=>$endpointlable):
        endforeach;
        echo $endpointlable;
}
    foreach(pa_custom_endpoint_keys() as $endpointkey=>$endpointlable){
$activetabendpoint=$endpointkey;
$underline="_";
$endpoint="endpoint";
$activeendpointcontenthook="woocommerce_account_$activetabendpoint$underline$endpoint";
add_action( $activeendpointcontenthook, 'pa_custom_endpoint_content');
}
kqhtkvqz

kqhtkvqz1#

试试这个

function add_custom_menu_items( $items, $args ) {
if ( $args->theme_location == 'primary' ) { // Replace 'primary' with the name of your menu location
$endpoint_url = 'https://example.com/your-endpoint';
$data = wp_remote_get( $endpoint_url );
$data = json_decode( $data['body'], true );

foreach ( $data as $item ) {
$menu_item = '<li class="menu-item">';
$menu_item .= '<a href="' . esc_attr( $item['url'] ) . '">' . esc_html( $item['name'] ) . '</a>';
$menu_item .= '</li>';
$items .= $menu_item;
}
}

return $items;
}
add_filter( 'wp_nav_menu_items', 'add_custom_menu_items', 10, 2 );

相关问题