簡體   English   中英

未捕獲的異常:在空視圖上調用成員函數 have_posts()

[英]Uncaught Exception: Call to a member function have_posts() on null View

我正在開發一個 wordpress 網站(電子商務),我們使用 Woocommerce 和 Sage 9 主題。 將 php 更新到 7.4 后,出現了這個問題。

未捕獲的異常:在 null 上調用成員函數 have_posts()(視圖:...views/template-home.blade.php)

這是我的代碼:template-home.blade.php

 @php
        woocommerce_product_loop_start();
        @endphp

         @while($get_products->have_posts())
            @php
              $get_products->the_post();
              //global $product;
              //$product = wc_get_product(get_the_ID());
              //echo Casey_Pro_Helper::getvar($product);
              //do_action('woocommerce_shop_loop');
              wc_get_template_part('content', 'product');
            @endphp
          @endwhile


        @php
        wp_reset_query();
        woocommerce_product_loop_end();
        //woocommerce_pagination();
        do_action('woocommerce_after_shop_loop');

        $total = $get_products->max_num_pages;
        $request_uri = $_SERVER['REQUEST_URI'];
        preg_match_all('/\//', $request_uri, $matches, PREG_OFFSET_CAPTURE);
        $current_page = 1;
        if (!empty($matches) && array_key_exists(1, $matches[0]) && array_key_exists(2, $matches[0])) {
            $first_slash_position = $matches[0][1][1];
            $last_slash_position = $matches[0][2][1];
            $length = strlen($request_uri) - $last_slash_position;
            $current_page = (int)substr($request_uri, $first_slash_position + 1, $length);
        }
        $base    = isset( $base ) ? $base : esc_url_raw( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) );
        $format  = isset( $format ) ? $format : '';
        @endphp

        <nav class="woocommerce-pagination">
        @php
          echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
            'base'         => $base,
            'format'       => $format,
            'add_args'     => false,
            'current'      => max( 1, $current_page ),
            'total'        => $total,
            'prev_text'    => '&larr;',
            'next_text'    => '&rarr;',
            'type'         => 'list',
            'end_size'     => 3,
            'mid_size'     => 3,
          ) ) );
        @endphp
        </nav>

      </div>
    </div>
  </div>

這是控制器:TemplateHome.php

<?php

namespace App\Controllers;

use Sober\Controller\Controller;

class TemplateHome extends Controller {

    /**
     * Return news
     *
     * @return array
     */
    public function GetProducts() {

        $include_ids = \App\run_equal_names_query();
        $request_uri = $_SERVER['REQUEST_URI'];
        preg_match_all('/\//', $request_uri, $matches, PREG_OFFSET_CAPTURE);
        $page = 1;
        if (!empty($matches) && array_key_exists(1, $matches[0]) && array_key_exists(2, $matches[0])) {
            $first_slash_position = $matches[0][1][1];
            $last_slash_position = $matches[0][2][1];
            $length = strlen($request_uri) - $last_slash_position;
            $page = (int)substr($request_uri, $first_slash_position + 1, $length);
        }
        $paged = $page;
        $posts_per_page = 9;
        $offset = (($paged-1)*$posts_per_page);
        $args = [
            'ep_integrate' => false,
            'post_type' => 'product',
            'posts_per_page' => $posts_per_page,
            'paged' => $paged,
            'offset' => $offset,
            'post__in' => $include_ids,
            'meta_query' => [
                [
                    'key' => '_stock',
                    'value' => 0,
                    'compare' => '>'
                ]
            ],
            'orderby' => 'modified',
            'order' => 'DESC'
        ];
        $home_products_query = new \WP_Query($args);
        return $home_products_query;
    }

我怎樣才能解決這個問題 ?

我已將控制器中的這些行添加到我的主頁模板中,這不是那么優雅,但現在它可以工作了,您認為如何以更優雅的方式解決它?

 $search_featured_image = get_field('product_placeholder_image_search','option'); $s_image = $search_featured_image['sizes']['thumbnail']; $include_ids = \\App\\run_equal_names_query(); $request_uri = $_SERVER['REQUEST_URI']; preg_match_all('/\\//', $request_uri, $matches, PREG_OFFSET_CAPTURE); $page = 1; if (!empty($matches) && array_key_exists(1, $matches[0]) && array_key_exists(2, $matches[0])) { $first_slash_position = $matches[0][1][1]; $last_slash_position = $matches[0][2][1]; $length = strlen($request_uri) - $last_slash_position; $page = (int)substr($request_uri, $first_slash_position + 1, $length); } $paged = $page; $posts_per_page = 9; $offset = (($paged-1)*$posts_per_page); $args = [ 'ep_integrate' => false, 'post_type' => 'product', 'posts_per_page' => $posts_per_page, 'paged' => $paged, 'offset' => $offset, 'post__in' => $include_ids, 'meta_query' => [ [ 'key' => '_stock', 'value' => 0, 'compare' => '>' ] ], 'orderby' => 'modified', 'order' => 'DESC' ]; $get_products= new \\WP_Query($args);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM