如何在loop-start.php

时间:2019-07-24 12:47:16

标签: wordpress woocommerce categories shortcode

我对WooCommerce短代码进行了Ajax调用,如下所示:

global $wpdb;
// get_the_data
$term_id = $_POST['term_id'];
do_shortcode [products category='$term_id'];

可以正常工作,但是我需要能够确定loop-start.php中属于哪个类别,我已经重写了该类别以根据类别显示不同的内容。

我尝试了多种方法来在loop-start.php中获取类别

global $wpdb;
global $wp_query;

$tableTitle = $wpdb->get_results(
    "SELECT slug FROM `wp_terms` WHERE name = '$page_title'"
);
$page_title = $tableTitle[0]->slug;
echo "<h2>page_title is ".$page_title."</h2>";

$cat = $wp_query->get_queried_object();
echo "CAT IS:".print_r($cat,true); // the category needed.

echo "<h2>query_var = ". get_query_var('cat') ."</h2>";

我尝试过的每种方法都不会返回任何结果。如何获取该短代码的生成类别或其他类别的指示符?

1 个答案:

答案 0 :(得分:0)

我想我明白了。我可以使用$ _POST ['term_id']

$tableTitle = $wpdb->get_results(
    "SELECT slug FROM `wp_terms` WHERE term_id = '$term_id'");
$page_title = $tableTitle[0]->slug;
echo "<h2>page_title is ".$page_title."</h2>";