第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 產(chǎn)品頁面上顯示作者元數(shù)據(jù)

在 WooCommerce 產(chǎn)品頁面上顯示作者元數(shù)據(jù)

PHP
偶然的你 2022-01-02 15:47:00
我在我的 WordPress 網(wǎng)站上使用 WooCommerce 產(chǎn)品供應(yīng)商插件。每個供應(yīng)商都有自己的供應(yīng)商資料。我想在每個供應(yīng)商產(chǎn)品上顯示作者元數(shù)據(jù),例如 LinkedIn、facebook、wikipedia 等。社交資料數(shù)據(jù)存儲在常規(guī) WordPress 用戶資料中。如果您在 WP 安裝上創(chuàng)建新用戶,您應(yīng)該在聯(lián)系信息表單表下看到 facebook 個人資料 URL、維基百科、LinkedIn 等。因此,每個用戶只需在他們的 WordPress 個人資料中輸入他們的社交個人資料。我是 PHP 新手,所以可能是一個非?;镜膯栴}。我試圖通過大量的跟蹤和錯誤使我的代碼工作。但是,當我嘗試從標題中獲取用戶 ID 時,我的代碼遇到了死胡同。如果我做 var_dump( $vendor_data ); 我得到了我需要的所有用戶信息:array(1) { [63]=> array(21) { ["notes"]=> string(0) "" ["logo"]=> string(0) "" ["profile"]=> string(0) "" ["email"]=> string(0) "" ["admins"]=> array(1) { [0]=> int(20) } ["commission"]=> int(70) ["commission_type"]=> string(10) "percentage" ["paypal"]=> string(0) "" ["timezone"]=> string(5) "UTC+2" ["enable_bookings"]=> string(2) "no" ["per_product_shipping"]=> string(2) "no" ["instant_payout"]=> string(2) "no" ["term_id"]=> int(63) ["name"]=> string(9) "Tobias M." ["slug"]=> string(8) "tobias-m" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(63) ["taxonomy"]=> string(20) "wcpv_product_vendors" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(2) } }使用上面的 var_dump,我將第 11 行更改為:$vendor_data[ $vendor_id ] = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor_id )->name;`在做了一些研究之后,我發(fā)現(xiàn) -> 無效,因為 -> 意味著我正在訪問一個對象,但就我而言,我需要從數(shù)組中檢索數(shù)據(jù)。因此我嘗試了這樣的事情,但仍然沒有運氣:// get vendor data$vendor_data = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor_id['name'] );$name = $vendor_data['name'];
查看完整描述

1 回答

?
茅侃侃

TA貢獻1842條經(jīng)驗 獲得超22個贊

我從未使用過 WooCommerce Vendors 插件,所以我沒有測試過這段代碼。更具體地說,它看起來$vendor_id可能只是與供應(yīng)商關(guān)聯(lián)的常規(guī)用戶 ID。如果是這種情況,那么用于獲取用戶 ID 的子例程將是不必要的,但是,以防萬一這是錯誤的,我將包含包含它的代碼的第二個版本。我也在重寫它以避免在里面沒有任何東西時輸出一個空的 div。


此外,woocommerce_after_add_to_cart_button是一個出現(xiàn)在single-product模板中的鉤子,因此您不需要測試您是否正在顯示帶有is_product().


/**

 * Output a Wikipedia image-link for a a Woocommerce Vendor

 * If $vendor_id is equal to $user_id

 **/

add_action( 'woocommerce_after_add_to_cart_button', 'our_vendor_info' );


function our_vendor_info() {


    // get vendor id from product id

    $vendor_id = WC_Product_Vendors_Utils::get_vendor_id_from_product( get_the_ID() );


    if ( $vendor_id && get_the_author_meta( 'wikipedia', $vendor_id ) ) {


        // Get Wikipedia profile

        $wikipedia = get_the_author_meta( 'wikipedia', $vendor_id );   


        echo '<div class="large-3 columns venprofilelink">';


        echo '<span class="some-talent">

            <a href="'. $wikipedia .'" target="blank" rel="noopener noreferrer">

                <img src="/wp-content/uploads/2019/09/wikipedia-logo.svg" />

            </a>

         </span>'; 


        echo '</div>';


    }


}

如果情況并非如此,而且$vendor_id確實是一個唯一的 ID 號,這意味著將它與特定用戶 ID 相關(guān)聯(lián)的編碼人員不必費心將后者包含在 $vendor_id 數(shù)據(jù)或其任何實用程序功能或過濾器或操作中,那么您需要一些更復(fù)雜的東西,比如你或其他編碼員寫的:


/**

 * If $vendor_id is *not* equal to $user_id

 **/

add_action( 'woocommerce_after_add_to_cart_button', 'our_vendor_info' );


function our_vendor_info() {


    // get vendor id from product id

    $vendor_id = WC_Product_Vendors_Utils::get_vendor_id_from_product( get_the_ID() );


    //if there's no vendor_id then nothing to do here

    if ( $vendor_id ) {


        // get vendor data so we can get User ID

        $vendor_data = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor_id ); 


        //get_userdatabylogin() is deprecated, also $vendor_data does not include a "login," but does include a slug

        //(which should be reliable)

        $user_id = get_user_by( 'slug', $vendor_data['slug'] )->ID ; 


        //don't output anything unless there's a reason to do it

        if ( get_the_author_meta( 'wikipedia', $user_id ) ) {


            // Get Wikipedia profile url

            $wikipedia = get_the_author_meta( 'wikipedia', $user_id );   


            echo '<div class="large-3 columns venprofilelink">';


            echo '<span class="some-talent">

                <a href="'. $wikipedia .'" target="blank" rel="noopener noreferrer">

                    <img src="/wp-content/uploads/2019/09/wikipedia-logo.svg" />

                </a>

             </span>'; 


            echo '</div>';


        }


    }


}


查看完整回答
反對 回復(fù) 2022-01-02
  • 1 回答
  • 0 關(guān)注
  • 325 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號