Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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 WP查询自定义分类法,但不包括当前术语_Php_Wordpress - Fatal编程技术网

Php WP查询自定义分类法,但不包括当前术语

Php WP查询自定义分类法,但不包括当前术语,php,wordpress,Php,Wordpress,我正在设置一个wp_查询,以列出自定义分类法中的所有术语。但是,我想在下面的代码中加一个字母,以排除当前的分类术语 例如,在“term1”的archive.php页面上,我希望将其排除在外,这意味着我只剩下“term2”、“term3”等 <? $args = array( 'taxonomy' => 'topic', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' =&g

我正在设置一个wp_查询,以列出自定义分类法中的所有术语。但是,我想在下面的代码中加一个字母,以排除当前的分类术语

例如,在“term1”的archive.php页面上,我希望将其排除在外,这意味着我只剩下“term2”、“term3”等

<? 
$args = array(
    'taxonomy' => 'topic',
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => '-1'
);
?>
<? $tax_menu_items = get_categories( $args );
    foreach ( $tax_menu_items as $tax_menu_item ):?>
        <li>
            <a href="<? echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
                <? echo $tax_menu_item->name; ?>
            </a>
        </li>
    <? endforeach; ?>


  • 试试这个,它确实取决于您在哪里使用它

    // Get the category ID
    $catID = get_query_var('topic');
    // Now run the query excluding the current taxonomy
    $args = array(
        'exclude' => $catID,
        'taxonomy' => 'topic',
        'orderby' => 'title',
        'order' => 'ASC',
        'posts_per_page' => '-1'
    );
    

    然后是上面剩下的代码。有关get_categories()的详细信息,请查看exclude参数,这绝对是您所需要的-它只是确保排除正确的类别ID。

    从理论上讲,如果您查看全局$post对象,该对象应该是当前的分类法,那么您可以从中获得要为参数排除的ID。如果你需要进一步的解释,请告诉我:)你可以作为参考。嗨@SimonPollard,谢谢你的建议。你能帮我调整一下上面的代码吗?我对wp_查询相当陌生…不幸的是,我正在使用archive.php页面上的代码,这意味着您已经在通过该查询显示的选项之一的页面上了?嗨,Simon。同样不幸的是,我应该澄清一下,我在这里使用的是自定义分类法,所以“cat”的术语可能不正确?可能是的。。。在get\u query\u var位中尝试“topic”,以防万一。理想情况下,我们只是想找到那个神奇的ID,然后一切都会成功——不,没有运气。我们不想排除“主题”Topic是自定义分类法的名称,而我的查询显示的是该分类法中的术语。get_query_var(“Topic”)应返回您正在查看的主题的当前ID-出于兴趣,您是如何创建自定义分类法的?需要确保它是可公开查询的