我已将自定义帖子的自定义分类法permalink结构设置为:
custom post type > custom taxonomy > post (来自this helpful Q&A)
现在我希望能够编写分类法。php根据当前的永久链接自动循环帖子,即:如果URL为example.com/writing/fiction/ 然后页面(taxonomy.php)将显示所有带有分类术语“虚构”的帖子,或者如果URL是example.com/writing/non-fiction 然后是分类学。php将显示所有带有分类术语“非虚构”的帖子。
目标是只需要1个分类法。php文件。
以下是当前代码(我希望将术语设置为自动/动态):
<?php
$posts = array (
\'post_type\' => \'writing\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'type\',
\'terms\' => \'fiction\', // looking to automate this, based on URL
),
),
);
$loop = new WP_Query( $posts );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<a href="<?php the_permalink();?>">
<?php the_title(\'<h2>\',\'</h2>\');?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
SO网友:codeview
使用它:
$queried_object = get_queried_object () ;
以及
\'terms\' => $queried_object->slug,
现在完整代码:
<?php
$queried_object = get_queried_object () ;
$posts = array (
\'post_type\' => \'writing\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'type\',
\'field\' => \'slug\',
\'terms\' => $queried_object->slug,
),
),
);
$loop = new WP_Query( $posts );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<a href="<?php the_permalink();?>">
<?php the_title(\'<h2>\',\'</h2>\');?>
</a>
<?php endwhile; wp_reset_postdata(); ?>