WordPress中WooCommerce页面上的当前页面ID


Current page ID on WooCommerce page in WordPress

我正在为WordPress使用WooCommerce插件。现在在文件page.php中,我试图通过$post->IDthe_id()获取当前打开的页面id,但我得到的只是产品id,而不是当前页面id。可能是因为WooCommerce覆盖了循环?

我能做什么?

可能已经太晚了,但我试图得到和你一样的结果,下面这对我来说效果很好:

<?php 
  if(is_shop()){
    $page_id = woocommerce_get_page_id('shop');
  }else{
    $page_id = $post->ID;
  }
?>

希望这对你有帮助。

如果您正在尝试获取特定的页面id,请尝试使用此方法。

if ( wc_get_page_id( 'name of page' ) != get_the_ID()): 

这对我来说很好

global $post;$post->ID;global $wp_query;$wp_query->post->ID;不适用于wooccommerce。你可以在这里获得wooccommerce页面的所有id。

woomocommerce_product_archive_description的启发,您可以尝试一下

add_action('woocommerce_after_main_content', 'custom_name', 11, 2); 
function custom_name() {
  if ( is_post_type_archive( 'product' ) && 0 === absint( get_query_var( 'paged' ) ) ) :
    global $post;
    $shop_page = get_post( wc_get_page_id( 'shop' ) );
    if ( $shop_page ) {
      $post = $shop_page;
    }
    // your code here
  endif;
}
$woo_pages_id = array(
    //get_option( 'woocommerce_shop_page_id' ), 
    get_option( 'woocommerce_cart_page_id' ), 
    get_option( 'woocommerce_checkout_page_id' ), 
    //get_option( 'woocommerce_pay_page_id' ), 
    //get_option( 'woocommerce_thanks_page_id' ), 
    get_option( 'woocommerce_myaccount_page_id' ),
    //get_option( 'woocommerce_edit_address_page_id' ), 
    //get_option( 'woocommerce_view_order_page_id' ), 
    get_option( 'woocommerce_terms_page_id' )
);
$current_page_id = get_the_ID();
if ( in_array($current_page_id, $woo_pages_id) ) : 
    get_template_part('content/woo', 'pages');
else :
    get_template_part('content', 'page');
endif;

我正在将其用于我的page.php文件。

将这一行放在代码上方或脚本开头。

global $post;

现在您可以使用$post->ID;

感谢

如果同样的问题在这里找到了解决方案我在商店页面上使用的代码是:

<?php echo woocommerce_get_page_id('shop'); ?>

基本上将shop值替换为您想要获得的任何wooccommerce页面。

关于该函数和其他一些有用函数的更多文档可以在这里找到