dsjq62428 2016-07-18 19:29
浏览 129
已采纳

检测WooCommerce“我的帐户”页面的仪表板

How can I detect if the "myaccount/my-account.php" template is used on the Dashboard.

Currently I use:

<?php
    global $wp;
    if ( !isset($wp->query_vars['page']) ) {
?>
    <a href="/mein-konto/">Back to my Account</a>
<?php } ?>

<div class="myaccount_content">
    <?php
        do_action( 'woocommerce_account_content' );
    ?>
</div>

But that feels kind of hacky. Isn't there something like a is_myaccount_dashboard() function?

  • 写回答

3条回答 默认 最新

  • doumao1047 2016-07-18 20:33
    关注

    Update: Detecting specifically the My account "Dashboard" page

    <?php
        global $wp;
        $request = explode( '/', $wp->request );
    
        // If NOT in My account dashboard page
        if( ! ( end($request) == 'my-account' && is_account_page() ) ){ 
    ?>
        <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id')); ?>">Back to my Account Dashboard</a>
    <?php 
        } 
    ?>
    
    <div class="myaccount_content">
        <?php
            do_action( 'woocommerce_account_content' );
        ?>
    </div>
    

    Tested and works.


    Original answer:

    Yes of course there is is_account_page() native WooCommerce conditional that returns true on the customer’s account pages.

    Here is an example using is_account_page() and is_user_logged_in(). To get the my account link url you can use: get_permalink( get_option('woocommerce_myaccount_page_id') ).

    if ( !is_account_page() ) { // User is NOT on my account pages
    
        if ( is_user_logged_in() ) { // Logged in user
    
        // Link to "My Account pages dashboard". 
    ?>  
        <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account', 'woocommerce'); ?>"><?php _e( 'My Account', 'woocommerce' ); ?></a>
    <?php }
        else { // User is NOT logged in
    
        // Link to "Login / register page".
    ?>  
        <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e( 'Login / Register','woocommerce' ); ?>"><?php _e( 'Login / Register', 'woocommerce' ); ?></a>
    
    <?php 
        } 
    } 
    ?>
    

    Reference:


    After that you can Override WooCommerce Templates via a Theme using my account templates to fine tune even more WooCommerce behaviors…

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流