Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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
Php WordPress获取类别ID到数组_Php_Wordpress_Categories - Fatal编程技术网

Php WordPress获取类别ID到数组

Php WordPress获取类别ID到数组,php,wordpress,categories,Php,Wordpress,Categories,我在category.php中有这段代码,我想显示我所在类别的帖子,“$cat”以及引用id63的类别。我写了一些东西,但不想工作:/ <?php $cat->term_id; $my_query_args = array( 'posts_per_page' => 6, 'tax_query' => array( array( 'taxonomy' => 'category', 'fie

我在
category.php
中有这段代码,我想显示我所在类别的帖子,“
$cat
”以及引用
id
63的类别。我写了一些东西,但不想工作:/

<?php
$cat->term_id;
$my_query_args = array(
    'posts_per_page' => 6,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => array(63, $cat),
            'operator' => 'AND'
        )
    )
);
$my_query = new WP_Query($my_query_args);
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();

        ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    endwhile;
endif;
wp_reset_postdata();

?>

  • 有人能告诉我为什么不将当前显示的类别添加到
    $cat


    提前感谢您的帮助

    您的
    $cat
    变量可能是一个对象。尝试将
    'terms'=>数组(63,$cat),
    更改为
    'terms'=>数组(63,$cat->ID),
    'terms'=>数组(63,$cat->ID),
    您的
    $cat
    变量可能是一个对象。尝试将
    'terms'=>数组(63,$cat),
    更改为
    'terms'=>数组(63,$cat->ID),
    'terms'=>数组(63,$cat->ID),

    这将为您提供当前类别id。 现在将下面的查询放在category.php中wordpress循环开始之前

    query_posts( 'cat='.$cat_id);
    
    如果这是一个普通的类别,你不想让它太复杂,有太多的参数。这将通过检查当前类别id动态工作

    这将为您提供当前类别id。 现在将下面的查询放在category.php中wordpress循环开始之前

    query_posts( 'cat='.$cat_id);
    

    如果这是一个普通的类别,你不想让它太复杂,有太多的参数。这将通过检查您当前的类别id动态工作。

    您可能会在tac_查询中弄乱“field”参数

    'field' => 'term_id',
    

    字段-按术语选择分类法。可能的值为“术语库id”、“名称”、“slug”或“术语库分类库id”。默认值为“term\u id”。

    可能是您在tac\u查询中弄乱了“field”参数

    'field' => 'term_id',
    

    字段-按术语选择分类法。可能的值为“术语库id”、“名称”、“slug”或“术语库分类库id”。默认值为“term_id”。

    尝试将您的cat id或名称设置为cat=

    <?php
    $catquery = new WP_Query( 'cat=3&posts_per_page=10' );
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <ul>
    <li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
    <ul><li><?php the_content(); ?></li>
    </ul>
    </li>
    </ul>
    <?php endwhile; ?>
    
    
    

    尝试将您的cat id或名称设置为cat=

    <?php
    $catquery = new WP_Query( 'cat=3&posts_per_page=10' );
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <ul>
    <li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
    <ul><li><?php the_content(); ?></li>
    </ul>
    </li>
    </ul>
    <?php endwhile; ?>