duannao1920 2018-11-12 23:00
浏览 30

如何在WP Query中使用woocommerce动作

I have written this code in my plugin to create custom links in the woocommerce my-account from custom post type. i have a 'post_type' => 'links'. but code not works. woocommerce codes works out of the loop properly. But inside the while of WP_Query Do not add any links. Please help me, I've been looking for the solution for the whole day

    $link_posts_args = array(
        'post_type' => 'links',
    );
    $link_posts = new WP_Query($link_posts_args);
    if ($link_posts->have_posts()) {
        while ($link_posts->have_posts()) {
            $link_posts->the_post();


            /**
             * Register new endpoint to use inside My Account page.
             */
            function wwd_custom_endpoints()
            {
                add_rewrite_endpoint('link1', EP_ROOT | EP_PAGES);
            }

            add_action('init', 'wwd_custom_endpoints');

            /**
             * Add new query var.
             *
             * @param array $vars
             * @return array
             */
            function wwd_custom_query_vars($vars)
            {
                $vars[] = 'link1';

                return $vars;
            }

            add_filter('query_vars', 'wwd_custom_query_vars', 0);

            /**
             * Flush rewrite rules on plugin activation.
             */
            function wwd_custom_flush_rewrite_rules()
            {
                add_rewrite_endpoint('link1', EP_ROOT | EP_PAGES);
                flush_rewrite_rules();
            }

            register_activation_hook(__FILE__, 'wwd_custom_flush_rewrite_rules');
            register_deactivation_hook(__FILE__, 'wwd_custom_flush_rewrite_rules');


            /**
             * Custom help to add new items into an array after a selected item.
             *
             * @param array $items
             * @param array $new_items
             * @param string $after
             * @return array
             */
            function wwd_custom_insert_after_helper($items, $new_items, $after)
            {
                // Search for the item position and +1 since is after the selected item key.
                $position = array_search($after, array_keys($items)) + 1;

                // Insert the new item.
                $array = array_slice($items, 0, $position, true);
                $array += $new_items;
                $array += array_slice($items, $position, count($items) - $position, true);

                return $array;
            }

            /**
             * Insert the new endpoint into the My Account menu.
             *
             * @param array $items
             * @return array
             */
            function wwd_custom_my_account_menu_items($items)
            {
                // Remove the logout menu item.
                $logout = $items['customer-logout'];
                unset($items['customer-logout']);

                // Insert your custom endpoint.
                $new_items = array();
                $new_items['link1'] = __('link name', 'woocommerce');

                // Insert back the logout item.
                $items['customer-logout'] = $logout;

                return wwd_custom_insert_after_helper($items, $new_items, 'orders');
            }

            add_filter('woocommerce_account_menu_items', 'wwd_custom_my_account_menu_items');


            /**
             * Endpoint HTML content.
             */
            function wwd_custom_endpoint_content()
            {

                get_the_content();

            }

            add_action('woocommerce_account_link1_endpoint', 'wwd_custom_endpoint_content');


            /*
             * Change endpoint title.
             *
             * @param string $title
             * @return string
             */
            function my_custom_endpoint_title($title)
            {
                global $wp_query;

                $is_endpoint = isset($wp_query->query_vars['link1']);

                if ($is_endpoint && !is_admin() && is_main_query() && in_the_loop() && is_account_page()) {
                    // New page title.
                    $title = __('My Custom Endpoint', 'woocommerce');

                    remove_filter('the_title', 'my_custom_endpoint_title');
                }

                return $title;
            }

            add_filter('the_title', 'my_custom_endpoint_title');


        }

    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路