简体   繁体   English

自定义帖子类型的帖子数量不受限制

[英]Unlimited posts on custom post type

I've got a custom post type I need to set to 'display all posts'. 我有一个自定义帖子类型,需要设置为“显示所有帖子”。 In Reading I've set it to 10 due to only wanting 10 posts on the blog page. 在阅读中,我将其设置为10,因为在博客页面上只需要10个帖子。 How do I set the maximum posts on the CPT page? 如何在CPT页面上设置最大帖子数? I've found this code... 我找到了此代码...

$args = array('post_type' => 'portfolio',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'portfolio',
'field' => 'slug',
)
)
)

$query = new WP_Query($args)

I'm new to php so I'm unsure where I add this - functions.php in the 'register CPT' or in the loop on the archive page? 我是php的新手,所以不确定在“ register CPT”或存档页面上的循环中将其添加到何处-functions.php? The loop is quite complicated as I'm pulling in 3 taxonomies and setting values each time. 循环非常复杂,因为我每次都要引入3种分类法并设置值。

<?php $i = 0; ?>
                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <?php 
                    $i++;
                    $term_list1 = wp_get_post_terms($post->ID, 'discipline', array("fields" => "ids"));
                    $term_list2 = wp_get_post_terms($post->ID, 'type', array("fields" => "ids")); 
                    $term_list3 = wp_get_post_terms($post->ID, 'sector', array("fields" => "ids")); 
                     ?>

                <li class="item" data-id="id-<?php echo $i; ?>" data-type='<?php foreach ($term_list1 as $value) {echo $value." ";} ?><?php foreach ($term_list2 as $value) {echo $value." ";} ?><?php foreach ($term_list3 as $value) {echo $value." ";} ?>'>
                    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>

                </li>

                <?php endwhile; else : ?>
                <?php endif; ?>

Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

I figured it out, I used this: 我想通了,我用了这个:

<?php $args = array( 'post_type'=>'portfolio', 'posts_per_page'=>100); 
                    $portfolio = new WP_Query( $args ); while( $portfolio->have_posts() ) : $portfolio->the_post(); ?>

You need to edit your template, and pass those arguments to the query_posts function BEFORE your loop, then reset the query with wp_reset_query after: 您需要编辑模板,然后在循环之前将这些参数传递给query_posts函数,然后在以下情况下使用wp_reset_query重置查询:

<?php
query_posts( array(
    'post_type' => 'portfolio',
    'posts_per_page' => -1 )
);
$i = 0;
if (have_posts()) : while (have_posts()) :

    the_post();

    $i++;
    $term_list1 = wp_get_post_terms($post->ID, 'discipline', array("fields" => "ids"));
    $term_list2 = wp_get_post_terms($post->ID, 'type', array("fields" => "ids"));
    $term_list3 = wp_get_post_terms($post->ID, 'sector', array("fields" => "ids"));
    ?>

<li class="item" data-id="id-<?php echo $i; ?>" data-type='<?php echo implode(' ',$term_list1 ) . ' ' . implode(' ',$term_list2 ) . ' ' . implode(' ',$term_list3 ); ?>'>
    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>

</li>

<?php
endwhile; endif;
wp_reset_query();
?>

I also tided up your data-type generation with the use of implode rather than foreach loops 我还使用implode而不是foreach循环来整理您的数据类型生成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM