以下代碼成功從我的Wordpress網站獲取category
「工具」的所有Woocommerce產品。通過發帖獲取Wordpress數據
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'tools'
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
global $product;
echo '<br /><a href="' . get_permalink() . '">' . woocommerce_get_product_thumbnail() . ' ' . the_title() . ' ' . the_content() . ' ' . get_post_meta(get_the_ID() , '_regular_price', true) . ' ' . get_post_meta(get_the_ID() , '_sale_price', true) . '</a>';
endwhile;
wp_reset_query();
?>
這工作正常,並返回在category
「工具」中的所有產品。
但是我現在試圖通過其post id
獲得產品。所以,而不是由category
由id
。我想要獲得上述產品的所有數據,即標題,內容,圖像,銷售價格和正常價格。
我試了下面的代碼,但它沒有奏效。
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'ID' => '254'
);
$loop = new WP_Query($args);
while ($loop->have_posts()):
$loop->the_post();
global $product;
echo '<br /><a href="' . get_permalink() . '">' . woocommerce_get_product_thumbnail() . ' ' . the_title() . ' ' . the_content() . ' ' . get_post_meta(get_the_ID() , '_regular_price', true) . ' ' . get_post_meta(get_the_ID() , '_sale_price', true) . '</a>';
endwhile;
wp_reset_query();
?>
如何獲得關於產品的所有信息,因爲我在第一段代碼(按類別)做到了,但它的ID
呢?
因此,例如,在上面的代碼中,我試圖獲取產品上的數據,其帖子ID爲254.因此,有關該產品的信息。
我認爲我會在$args
陣列中尋找ID
而不是product_cat
。
請幫忙。
實際上使用的是''p'=> 254',它的工作方式與@Ohgodwhy上面的註釋中的建議一致,並且遵循您的示例 – Tester
酷。 Thx綠色複選標記。有很多方法可以做你想做的事情,但這是一個更簡單,相當簡單的方法,建立在你的例子之上。如果你知道帖子ID是「254」,你可以跳過所有其他的籃筐。該ID在WP系統中是唯一的。其他過濾器有助於保證該帖子具有「產品」質量。在使用中有兩個注意事項:1 - 在循環中或其他查詢周圍,這可能會變得不可靠(需要重置過濾器/查詢),2 - 我建議添加一個過濾爲「發佈」,所以你不會得到草稿/等泄漏。吉爾德運氣。 –