相关分类不显示任何帖子

时间:2011-08-22 作者:adolfozen

不显示任何结果

<?php

$query = array(
    \'tax_query\' => array(

        \'relation\' => \'AND\',

        array (
            \'taxonomy\' => \'location\',
            \'field\' => \'slug\',
            \'terms\' => array( \'california\', \'\' ),
        ),

        array(
            \'taxonomy\' => \'genre\',
            \'field\' => \'slug\',
            \'terms\' => array( \'comedy\', \'\' ),
        )
    )
);


$query = new WP_Query();


?> 
未显示任何结果:(未显示帖子,需要帮助,谢谢

3 个回复
SO网友:tollmanz

我不确定您的其余代码是什么样子,但您正在混淆您的$query 变量,并且您不发送$query 到的新实例WP_Query. 此外,如果只使用一个术语,则无需向“terms”参数发送数组。试试这个。

<?php
// Set the args
$args = array(
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array (
            \'taxonomy\' => \'location\',
            \'field\' => \'slug\',
            \'terms\' => \'california\',
        ),
        array(
            \'taxonomy\' => \'genre\',
            \'field\' => \'slug\',
            \'terms\' => \'comedy\',
        )
    )
);

// Make a new instance of WP_Query
$query = new WP_Query();

// Make the query
$query->query($args);

// Test for posts
if($query->have_posts())
{
    // Loop through posts
    while($query->have_posts())
    {
        // Set up the post
        $query->the_post();
        global $post;
    }
}
?>

SO网友:Chris

首先,你用错了。

$query = new WP_Query(array(
     \'post_type\' => \'post,\'
     \'post_status\' => \'publish\',
     \'tax_query\' => array(
        \'taxonomy\' => \'genre\',
        \'field\' => \'slug\',
        \'terms\' => array( \'comedy\', \'\' ),
    )
));
需要将数组提供给WP\\u查询对象。此外,我很确定至少需要指定一种post类型才能正常工作。

SO网友:adolfozen

<?php
// Set the args
$args = array(
    \'tax_query\' => array(
        \'post_type\' => \'show\',
        \'relation\' => \'AND\',
        array (
            \'taxonomy\' => \'location\',
            \'field\' => \'slug\',
            \'terms\' => \'california\',
        ),
        array(
            \'taxonomy\' => \'genre\',
            \'field\' => \'slug\',
            \'terms\' => \'comedy\',
        )
    )
);




 // Make a new instance of WP_Query
$query = new WP_Query($args);

// Make the query
$query->query(\'showposts=5\');

// Test for posts
if($query->have_posts())
{
    // Loop through posts
    while($query->have_posts()) 
    {
    echo \'<div>\';
  the_title();
    echo \'</div>\';

        // Set up the post
       $query->the_post();


        global $post;


    }
}



?>
但问题是它显示的帖子也来自帖子

结果:有点像《喜剧》(Carifornia,喜剧)的帖子,而其他帖子则是从1类帖子到2类帖子

结束

相关推荐

Taxonomy Dropdown Question

我需要替换show\\u option\\u all(显示所有分类法),使用:显示三个分类法(ID 14、15、16)并将其标记为“all”的命令基本上,当有人选择“all”时,我只想显示上面三种分类法的结果。注意:我不能从搜索结果中排除分类法,因为它会影响其他搜索表单。请帮忙!我当前的代码是:http://pastebin.com/NJi2LQhY谢谢保罗