【问题标题】:Having trouble with wordpress custom taxonomy archive在使用 wordpress 自定义分类归档时遇到问题
【发布时间】:2014-02-28 22:55:36
【问题描述】:

我在使用自定义分类模板时遇到了一点问题。我继承了一个由其他人开发的网站,他们使用“类型”插件添加一些自定义分类法。

目标:

在 example-domain.com/people/harrison-ford 上拥有一个仅显示包含特定分类术语的帖子的存档模板

问题:

此代码引入了未选择分类的帖子。

这是我的完整代码:

<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
    $args = array(
        'post_type' => $post_type,
        'people' => 'harrison-ford',
        "$tax" => $tax_term->slug,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1,
            'orderby' => 'date',
            'order' => DESC
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) : ?>
        <h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
        <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

<-- display stuff -->

        <?php endwhile; // end of loop ?>
    <?php endif; // if have_posts()
    wp_reset_query();

}
?>

【问题讨论】:

    标签: php wordpress archive taxonomy


    【解决方案1】:

    你在这里期待什么? "$tax" 将变为 'people' =&gt;,这会将 'harrison-ford' 覆盖为 $tax_term-&gt;slug 的值。

    'people' => 'harrison-ford',
    "$tax" => $tax_term->slug,
    

    此外,我不知道任何名为 people 的自定义参数,我很确定您想要 tax_query

    'tax_query' => array(
        'taxonomy' => 'people',
        'terms' => array('harrison-ford', $tax_term->slug)
    )
    

    这将为您提供匹配harrison-ford$tax_term-&gt;slug 值在people 分类内的所有人的结果

    【讨论】:

    • 谢谢,删除第一行 $tax 行得通。我不知道为什么一开始就在那里。
    猜你喜欢
    • 1970-01-01
    • 2014-09-19
    • 2021-04-20
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多