简体   繁体   English

如何将自定义帖子类型查询的帖子数限制为3?

[英]How do I limit the Number of Posts to 3 for a Custom Post Type Query?

I am trying to display results of a custom post type on my main wordpress page. 我试图在我的主WordPress页面上显示自定义帖子类型的结果。

This is my code so far: 到目前为止这是我的代码:

                <?php
wp_reset_query();
$args=array(
  'post_type' => 'rooms',
  'post_status' => 'publish',
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {  ?>
    <TABLE border="1" cellspacing="50">
    <TR>FEATURED ACTIVE LISTINGS</tr>
  <?php
  while ($my_query->have_posts()) : $my_query->the_post(); 
    $my_custom_fields = get_fields(get_the_ID());
    if(isset($my_custom_fields['availability']) && $my_custom_fields['availability'] == 'Available'):
?>

    <tr><td><?php echo the_post_thumbnail('thumbnail');?>
    <br>UNIT <?php echo the_field('unit_number'); ?> <?php echo the_field('bedrooms'); ?>BEDS/<?php echo the_field('bathrooms'); ?>BA
    <br>$<?php echo the_field('price'); ?></td>
    </tr>
  <?php 
endif;
endwhile; ?>
  </TABLE>
<?php }
wp_reset_query();
?>

This works. 这有效。 However, If I try to add 'posts_per_page' => 3, into the array of args. 但是,如果我尝试将'posts_per_page' => 3,添加到args数组中。 It does not display any results at all. 它根本不显示任何结果。 What am I doing wrong, or is there an alternative way to achieve this same result? 我做错了什么,或者有另一种方法来实现同样的结果?

If it is relevant, The plugins I am using are Advanced Custom Fields and Custom Post Types. 如果它是相关的,我使用的插件是高级自定义字段和自定义帖子类型。

Thanks in advance! 提前致谢!

SOLVED: 解决了:

I have actually figured it out myself, and thought I would share how I solved it. 我实际上已经弄明白了,并且认为我会分享我是如何解决它的。

'posts_per_page' => 3 was working but it would only show the last 3 posts of that type that was NOT being filtered by if(isset($my_custom_fields['availability']) && $my_custom_fields['availability'] == 'Available'): 'posts_per_page'=> 3正在运行,但它只显示该类型的最后3个帖子未被if(isset($ my_custom_fields ['availability'])&& $ my_custom_fields ['availability'] =='过滤“):

In order to Limit posts that were being filtered by this field, I added my own counter and set it to be at max of 3. I changed the above line to if(isset($my_custom_fields['availability']) && $my_custom_fields['availability'] == 'Sold' && ($postcount < 3)): and added $postcount++; 为了限制由此字段过滤的帖子,我添加了自己的计数器,并将其设置为最大值为3.我将上面的行更改为if(isset($ my_custom_fields ['availability'])&& $ my_custom_fields [ 'availability'] =='Sold'&&($ postcount <3)):并添加$ postcount ++; inside the loop. 在循环内。

Thanks again for your help. 再次感谢你的帮助。 I hope this helps whoever else. 我希望这可以帮助其他任何人。

The solution you found is only going to work under certain conditions. 您找到的解决方案只能在某些条件下工作。 If there aren't three posts with availability set to "Available" out of the set you retrieve (probably the first ten), you're not going to have enough. 如果没有三个帖子的可用性设置为“可用”,那么您检索的集合(可能是前十个),您就没有足够的资源。 You can do a custom query instead that specifies a custom field name and value, as described in the WP_Query documentation : 您可以执行自定义查询,指定自定义字段名称和值,如WP_Query文档中所述

$args=array(
   'post_type' => 'rooms',
   'post_status' => 'publish',
   'meta_key' => 'availability',
   'meta_value' => 'Available',
   'posts_per_page' => 3,
   'ignore_sticky_posts'=> 1
); 
$my_query = new WP_Query($args);

In functions.php you should rather do the following: 在functions.php中,您应该执行以下操作:

// posts per page based on content type
function themename_custom_posts_per_page($query)
{
    switch ( $query->query_vars['post_type'] )
    {
        case 'content_type_name':  // Post Type named 'content_type_name'
            $query->query_vars['posts_per_page'] = 3; //display all is -1
            break;

    }
    return $query;
}
if( !is_admin() )
{
    add_filter( 'pre_get_posts', 'themename_custom_posts_per_page' );
}

Source of this answer 这个答案的来源

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

相关问题 如何将自定义帖子类型WP查询中的帖子分成3组,然后将这些3组分成2组? - How do I separate the posts in a custom post type WP query into groups of 3, and then separate those groups of 3 into sets of 2? 如何查询自定义帖子类型和一个类别的帖子列表? - How do I query a list of posts of a custom post type and one category? 在Wordpress自定义帖子类型查询中限制带有附件的帖子 - Limit posts with attachment inside Wordpress custom post type query 如何在WordPress中自定义帖子类型的自定义类别的帖子循环? - How do I loop posts for a custom category for a custom post type in WordPress? 如何限制基于自定义元返回的帖子? - How do I limit the posts returned based on custom meta? 如何从侧边栏中的单页自定义帖子类型获取帖子以显示在页面中? - How do I get the posts from single page custom post type from a sidebar to display in a page? 如何删除自定义帖子类型管理屏幕中出现的所有Wordpress帖子? - How do I remove all Wordpress Posts from appearing in my Custom Post Type admin screen? 帖子不会显示在自定义帖子类型中 - Posts do not get shown in custom post type 每个帖子类型限制6个帖子 - Limit 6 posts per post type 如何在wordpress自定义帖子上设置帖子数 - How to set number of posts on wordpress custom post
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM