简体   繁体   English

在很少的帖子中添加自定义帖子类型

[英]Add Custom Post Type Every Few Posts

I was hoping someone could help me with this problem. 我希望有人可以帮助我解决这个问题。 I am wanting to add a custom post type(testimony) to my WordPress loop and display one every few posts. 我想将自定义帖子类型(证词)添加到我的WordPress循环中,并每隔几条帖子显示一次。 I added the custom post type to the loop using the pre_get_posts action and they show up in the loop but I want to scatter just this post type through out the posts and not have them together. 我使用pre_get_posts操作将自定义帖子类型添加到循环中,并且它们显示在循环中,但是我只想将此帖子类型分散到整个帖子中,而不是将它们放在一起。 Is there a way to do this? 有没有办法做到这一点? Any help would be appreciated. 任何帮助,将不胜感激。

If I'm reading it correctly you've got one query that is getting both regular posts and your custom post type testimonials. 如果我阅读正确,那么您会得到一个查询,该查询同时收到常规帖子和自定义帖子类型的推荐。 So theoretically you could pull 10 results all of which would be posts or all of which would be testimonials, depending on your search criteria. 因此,根据您的搜索条件,理论上您可以提取10个结果,这些结果全部是帖子,或者全部是推荐书。

What you probably want to do is make two queries, one for posts and one for testimonials. 您可能想做的是进行两个查询,一个查询职位,一个查询推荐。 That will give you two arrays of post objects and then it's easy to loop through and display one type or the other depending on an incremented counter. 这将为您提供两个发布对象数组,然后很容易遍历并根据递增的计数器显示一种或另一种类型。

Very roughly, something like: 非常粗略地讲,类似:

$args = array('post_type'=>'post', 'posts_per_page'=>9, 'category_name'=>'news);
$posts = get_posts($args);

$args = array('post_type'=>'testimonials', 'posts_per_page'=>3);
$testimonials = get_posts($args);

/** see how many of the regular posts you got back */
$post_count = count($posts);
/** see how many testimonials you got back */
$testimonial_count = count($testimonials);
/** add them up to get the total result count */
$total_count = $post_count + $testimonial_count;

/** Loop through the total number of results */
for($i = 1; $i <= $total_count; $i++){

/** assuming you want to show one testimonial every third post */
if($i % 3 == 0){
  /** this means you're on the a third post, show a testimonial */
  setup_postdata($testimonials[$i]);
}
else{
  /** show a regular post */
  setup_postdata($posts[$i]);
}

/** and now handle the output */
?><h1><?php the_title();?></h1><?php

}

In this example it's pulling a total of 12 posts -- 9 posts and 3 testimonials -- and then showing a testimonial every third post. 在此示例中,它总共拉出了12个帖子-9个帖子和3个推荐书-然后每三个帖子显示一次推荐书。 It's assuming you actually have the correct number of each one. 假设您实际上每个人都有正确的号码。 If you only got back two testimonials you'd get an error, so one a production site you'd want to finish it off with some code in after the ternary operator to make sure that there is a matching testimonial and if not show a regular post, etc. But should get you going in the right direction. 如果您只获得了两个证明书,那么您将得到一个错误,因此,一个生产站点您想在三元运算符之后用一些代码完成它,以确保有一个匹配的证明书,如果没有显示常规的话发布等信息。但是应该让您朝正确的方向前进。

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

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