我收到此错误
尝试在/wp-includes/post-template.php中获取非对象的属性
其他人建议使用wp_reset_postdata()
来解决问题,但它似乎无法正常工作。
我有以下post-template.php
的代码:
// If post password required and it doesn't match the cookie.
if ( post_password_required( $post ) )
return get_the_password_form( $post );
if ( $page > count( $pages ) ) // if the requested page doesn't exist
$page = count( $pages ); // give them the highest numbered page that DOES exist
$content = $pages[$page - 1];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
$content = explode( $matches[0], $content, 2 );
if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
$has_teaser = true;
} else {
$content = array( $content );
}
if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
$strip_teaser = true;
$teaser = $content[0];
if ( $more && $strip_teaser && $has_teaser )
$teaser = '';
$output .= $teaser;
以下是我的帖子功能/查询
function my_header_add_to_cart_fragment( $fragments ) {
ob_start();
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
function global_menu($location,$class){
wp_nav_menu( array(
'theme_location' => $location,//primary
'depth' => 2, // 1 = no dropdowns, 2 = with dropdowns.
'container' => null,
'menu_class' => $class,//navbar-nav mr-auto
'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
'walker' => new WP_Bootstrap_Navwalker(),
) );
}
add_filter( 'menu-init', 'global_menu' );
function portal_cart_link($url) {
?>
<a class="cart-contents" href="<?php echo $url; ?>" title="<?php esc_attr_e( 'View your shopping cart', permaslug() ); ?>">
<span class="cart-contents-count"><?php echo WC()->cart->get_cart_contents_count();?></span>
</a>
<?php
}
add_filter( 'woo-init', 'portal_cart_link' );
function product_query() {
$args = array(
'post_type' => 'product',
);
$loop = new WP_Query( $args );
if($loop->have_posts()):while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<div class="col-md-3 product"><a href="'.get_permalink().'"><img class="img-responsive" src="' . get_the_post_thumbnail_url( $post->ID) .'" /><br /><h3 class="text-center">' . get_the_title() . '</h3></a></div>';
endwhile;endif;
wp_reset_postdata();
}
add_filter( 'woo-init', 'product_query' );