我刚刚习惯了WP查询,希望能在这方面得到一些帮助。
我已经创建了一个自定义分类法(主题),现在想在我的头版上显示包含其中一个分类法的最新帖子,作为一篇最具特色的帖子。
现在我似乎真的不知道如何让它正确过滤查询,也许有人可以纠正我:
<?php
$args = array(
\'tax_query\' => array(
array(
\'posts_per_page\' => 1,
\'taxonomy\' => \'theme\',
\'field\' => \'slug\',
\'terms\' => array (\'text-image\', \'just-text\', \'just-image\')
)
)
);
$query = new WP_Query( $args );
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
非常感谢您的帮助,谢谢!
编辑:这里是完成的代码,以防其他人需要它;
<?php
$args = array(
\'post_type\' => \'post\', // it\'s default, you can skip it
\'posts_per_page\' => \'1\',
\'order_by\' => \'date\', // it\'s also default
\'order\' => \'DESC\', // it\'s also default
\'tax_query\' => array(
array(
\'taxonomy\' => \'nameoftaxonomy\',
\'field\' => \'slug\',
\'terms\' => array (\'whatever1\', \'whatever2\', \'whatever3\')
)
)
);
$query = new WP_Query( $args );
?>
<?php if (have_posts()) : while( $query->have_posts() ) : $query->the_post(); ?>
谢谢你的帮助!
SO网友:Max Yudin
你的WP_Query
论点是错误的。posts_per_page
不属于tax_query
. 以下应起作用:
$args = array(
\'post_type\' => \'post\', // it\'s default, you can skip it
\'posts_per_page\' => \'1\',
\'order_by\' => \'date\', // it\'s also default
\'order\' => \'DESC\', // it\'s also default
\'tax_query\' => array(
array(
\'taxonomy\' => \'theme\',
\'field\' => \'slug\',
\'terms\' => array (\'text-image\', \'just-text\', \'just-image\')
)
)
);