как использовать действия WooCommerce внутри WP Query - PullRequest
0 голосов
/ 13 ноября 2018

Я написал этот код в своем плагине для создания пользовательских ссылок в моей учетной записи woocommerce из пользовательского типа записи.у меня есть «post_type» => «ссылки».но код не работает.Код woocommerce работает из цикла правильно.Но в то же время WP_Query не добавляйте никаких ссылок.Пожалуйста, помогите мне, я искал решение в течение всего дня

    $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');


        }

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...