2015-04-16 95 views
0

我要讓這樣的簡碼[myshorcode:2:飲料],和查詢牧馬人我設法得到下面的查詢,顯示我2個產品一定分類的如何將查詢sql轉換爲wordpress中的短代碼?

PHP WP_Query:

$query = array (
    'paged' => 1, 
    'posts_per_page' => '2', 
    'offset' => 0, 
    'post_status' => 'publish', 
    'ignore_sticky_posts' => 0, 
    'orderby' => 'date', 
    'order' => 'DESC', 
    'tax_query' => 
    array (
    0 => 
    array (
     'taxonomy' => 'product_brand', 
     'field' => 'id', 
     'terms' => 
     array (
     0 => 30, 
    ), 
     'operator' => 'IN', 
     'include_children' => false, 
    ), 
), 
    'post_type' => 
    array (
    'product' => 'product', 
), 
); 

該查詢運行良好,但不給我選擇分類的名稱,只是ID。

這就是爲什麼我想在[product:2:name-taxonomy]或[product:name-taxonomy]的地方做一個shortcode,然後我把它修改。

謝謝你的意見和幫助。

回答

0

根據在WordPress標準方法,簡碼屬性輸入這樣的:

[productlist no_of_products="2" producttax="name-taxonomy"] 

以下爲簡碼功能的代碼:

function productlist_func($atts) { 
    $a = shortcode_atts(array(
     'no_of_products' => '2', 
     'producttax' => 'name-taxonomy', 
    ), $atts); 

    //write Query logic here 
} 
add_shortcode('productlist', 'productlist_func'); 

屬性將被轉換成一個關聯數組像以下,作爲其$ atts參數傳遞給處理函數:

array('no_of_products' => '2', 'producttax' => 'name-taxonomy') 

check Shortcode API on WordPress Codex for more details.