Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 想从类别slug获得帖子吗_Wordpress - Fatal编程技术网

Wordpress 想从类别slug获得帖子吗

Wordpress 想从类别slug获得帖子吗,wordpress,Wordpress,我正在做一个项目,在不同类别的slug中得到相同的结果。请帮帮我,我做错了什么 $act = $_POST['act']; $args = array( 'posts_per_page' => 100, 'offset' => 0, 'category' => $act, 'orderby' => 'post_date', 'order' => '

我正在做一个项目,在不同类别的slug中得到相同的结果。请帮帮我,我做错了什么

$act = $_POST['act'];
$args = array(
    'posts_per_page'   => 100,
    'offset'           => 0,
    'category'         => $act,
    'orderby'          => 'post_date',
    'order'            => 'DESC',
    'post_type'        => 'product',
    'taxnomy'          => 'product_cat',
    'post_status'      => 'publish'); 
$myposts = get_posts( $args );
global $product;
global $wpdb;               
foreach ($myposts as $key => $value) {
    $id = $value->ID;
echo '<li class="product type-product status-publish has-post-thumbnail first grid with-hover add-hover open-on-mobile with-border span3 featured shipping-taxable product-type-simple product-cat-accommodation-durban product-cat-accommodation-battlefields product-cat-battlefields product-cat-comfortable-accommodation-durban product-cat-comfortable-accommodation-battlefields product-cat-durban instock">';
echo '<div class="product-wrapper">';
echo '<a class="thumb" href="'.get_permalink( $id  ).'">';
$post_thumbnail_id = get_post_thumbnail_id($id);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
echo '<img src="'.$post_thumbnail_url.'" />';
echo '</a>';
echo '<h3>'.get_the_title( $id ).'</h3>';
echo '</div></li>';

}
$act=$\u POST['act'];
$args=数组(
“每页帖子数”=>100,
“偏移量”=>0,
“类别”=>$act,
'orderby'=>'post_date',
“订单”=>“描述”,
“post_类型”=>“产品”,
“税收”=>“产品类别”,
“发布状态”=>“发布”);
$myposts=get_posts($args);
全球$产品;
全球$wpdb;
foreach($myposts作为$key=>$value){
$id=$value->id;
回声级="产品类型产品状态发布已发布缩略图第一个网格带悬停添加悬停打开移动边界span3特色装运应税产品类型简单产品猫住宿德班产品猫住宿战场产品猫舒适住宿德班产品猫舒适住宿蝙蝠tlefields产品类别德班instock“>”;
回声';
回声';
echo“”。获取标题($id)。“”;
回音“”;
}

经过仔细考虑,我找到了问题的根源:

  • 类别需要是一个ID,并引用:
  • 注意:category参数需要是类别的ID,并且 不是类别名称

    注意:category参数可以是以逗号分隔的 类别,因为get_posts()函数传递“category” 参数作为“cat”直接输入到WP_查询中

    注意:category_name参数需要是字符串,在本例中, 类别名称

  • 分类法编写得不好,是“分类法”而不是“分类法”。此外,您可以从中删除它,因为它没有过滤任何内容
  • 
    
    这里$act的值是多少?@Bhumisah这只是一个类别的slug。我认为你的slug有问题,因为代码看起来很好。这个类别有孩子吗?用过静态slug。但是结果没有变化。嗨,谢谢你的回复。这里我想知道两件事。i)我想用slug过滤,而不是id。ii)公式适用于默认值不适用于woocommerce产品和类别的帖子和类别。
    <?php 
    $slug = "category-b";
    $args = array(
        'posts_per_page' => -1, 
        'tax_query' => array( 
            'relation' => 'AND', 
             array( 
                'taxonomy' => 'product_cat', 
                'field' => 'slug', 
                'terms' => $slug 
             ) 
        ), 
        'post_type' => 'product', 
        'orderby' => 'title', 
    ); 
    $the_query = new WP_Query( $args ); 
    
    foreach ($the_query->posts as $key => $value) {
         print_r($value->ID); 
    } 
    ?>