Php 将自定义Post类型循环限制为1个结果

Php 将自定义Post类型循环限制为1个结果,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我有一个wordpress页面,我需要在页面底部加载一个自定义的帖子类型。有8类帖子类型,我让循环工作,以便它根据加载的页面从正确的类别中提取。问题是,我想把它限制在该类别的最新帖子。现在循环从一个类别中提取所有帖子,我只想要1篇。我不知道如何在数组中限制它。我试过posts、posts\u per\u page和num\u page,但似乎都不起作用。有人知道我失去的价值吗 $taxonomy=get_field('show_from'); $args

我有一个wordpress页面,我需要在页面底部加载一个自定义的帖子类型。有8类帖子类型,我让循环工作,以便它根据加载的页面从正确的类别中提取。问题是,我想把它限制在该类别的最新帖子。现在循环从一个类别中提取所有帖子,我只想要1篇。我不知道如何在数组中限制它。我试过posts、posts\u per\u page和num\u page,但似乎都不起作用。有人知道我失去的价值吗

$taxonomy=get_field('show_from');
                    $args = array(
                        'post_type' => 'project',
                        'tax_query' => array(
                                    array(
                                'taxonomy' => 'project-categories',
                                'field' => 'slug',
                                'posts_per_page' => 1,
                                'terms' => $taxonomy
                            ),
                        ) 
                    );


                $query = new WP_Query( $args ); ?>
            <?php if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();
                    $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>

                    <div class="cpt-feature" style="background: url('<?php echo $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
                    <?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>

                    <?php 

                    echo "<div class='cpt-overlay'>";
                    echo "<div class='project-information'>"; 
                    echo "<h3>";
                        the_title();
                    echo "</h3>";
                    echo "<p>";
                    echo get_field('intro_blurb');
                    echo "<p><a href='" . get_permalink() . "'>View Project</a></p>";
                    echo "</div><!--project-info-->";
                    echo "</div><!--cpt-overlay-->";
                    echo "</div><!--cpt-feature-->";
                    echo "</a>";
                }
            } else {
            }?>
$taxonomy=get_字段('show_from');
$args=数组(
“post_type”=>“project”,
“tax_query”=>数组(
排列(
“分类法”=>“项目类别”,
'字段'=>'段塞',
“每页帖子数”=>1,
“术语”=>$taxonomy
),
) 
);
$query=新的WP_查询($args);?>

这是一个很好的例子,说明了正确的缩进/格式化代码对诊断/排除问题的能力有着巨大的影响

问题其实很简单:
posts\u per\u page
参数位于错误的位置-它嵌套在
tax\u查询中,但需要位于“顶层”:


*大多数优秀的IDE,如PHPStorm,都提供了易于使用的工具来为您自动格式化代码-我强烈建议您找到并使用一个-我是PHPStorm的超级粉丝。

谢谢您的帮助。很高兴这是一个简单的修复。
$args = array(
    'post_type'      => 'project',
    // moved this argument out of the tax query
    'posts_per_page' => 1,
    'tax_query'      => array(
        array(
            'taxonomy' => 'project-categories',
            'field'    => 'slug',
            'terms'    => $taxonomy
        )
    )
);