我的一个客户端有自定义的帖子类型,他们使用插件而不是在函数中定义它们。php。(因此,YARPP的解决方案行不通)。在此自定义帖子类型中,它们有各种类别。每个帖子都属于一个或多个类别。在每个帖子之后,他们希望显示所有相同类别的5篇帖子(可以是一个或多个类别)。
所以,我需要做的是设置一种方法来动态查找一篇文章所属的所有类别(我已经这样做了&在我解决这个问题之前,请立即将其设置为回显),然后查询以查找属于完全相同类别的5篇文章(要么属于所有类别,要么不应显示)。
这是我想到的代码,但我不知道如何“组合它们”,因此它不仅可以获取术语(类别),还可以强制查询使用这些术语来过滤5个“相关”程序。
//get the post\'s terms (troubleshooting - can be removed once figured out!
$category_terms = wp_get_object_terms($post->ID, \'category\');
if(!empty($category_terms)){
if(!is_wp_error( $category_terms )){
echo \'Terms <ul>\';
foreach($category_terms as $term){
echo \'<li><a href="\'.get_term_link($term->slug, \'category\').\'">\'.$term->name.\'</a></li>\';
}
echo \'</ul>\';
}
}
//get post terms done
// get the custom post type\'s taxonomy terms
$custom_taxterms = wp_get_object_terms( $post->ID,
\'category\', array(\'fields\' => \'ids\') );
// arguments
$args = array(
\'post_type\' => \'program_listings\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 5, // you may edit this number
\'orderby\' => \'rand\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'id\',
\'terms\' => $custom_taxterms
)
),
\'post__not_in\' => array ($post->ID),
);
$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
echo \'<h2>Other Programs in this category</h2><ul>\';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li style="margin-left:10px;list-style:none;"><a href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li>
<?php
endwhile;
echo \'</ul>\';
endif;
// Reset Post Data
wp_reset_postdata();
因此,获取帖子条款(&P);回显它们(完成后不会是回显-我这样做是为了确保它确实得到了类别/术语)效果很好。它显示帖子所在的每个“类别”。但是,第二位显示来自任何类别的帖子,而不是仅在所有类别中的帖子。
基本上,我需要将第一个和;第二部分,以便查询仅返回当前帖子所有类别中的帖子结果。如果某个帖子不在所有相同的类别中(与当前帖子的所有类别完全匹配),则不应返回该帖子。(例如,如果某个帖子属于父级“北美”类别,但不属于该帖子所属的任何其他类别,则不应在结果中返回该帖子)。
SO网友:Sublime Design
这是所需的代码,以便仅显示与同一子类别(因此与所有其他类别匹配)匹配的帖子(属于特定自定义帖子类型):
<!-- other posts -->
<?php
//this gets the lowest hierarchical child term for current post
$categories = get_the_category($post->ID);
foreach($categories as $category) :
$children = get_categories( array (\'parent\' => $category->term_id ));
$has_children = count($children);
if ( $has_children == 0 ) {
$current_child = $category->name;
}
endforeach;
//this starts the query for the program listings custom post types
$post_type = \'program_listings\';
$tax = \'category\';
$tax_terms = wp_get_object_terms($post->ID, \'category\');
if ($tax_terms == $category_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
\'post_type\' => $post_type,
"$tax" => $tax_term->slug,
\'post_status\' => \'publish\',
\'operator\' => \'AND\',
\'posts_per_page\' => 5,//limits the listing to 5
\'caller_get_posts\'=> 1,
\'post__not_in\' => array ($post->ID),
\'orderby\' => \'ID\', //orders by ID, you could also tell it to order by: ‘author’ ‘title’‘name’‘date’ or ‘rand’
\'order\' => \'ASC\', //tells it the display order should be ascending, DSC would indicate a descending order
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() && $tax_term->name == $current_child)//now only display categories the current post is in
{
echo \'<h2>Other programs in this category</h2><ul>\';
while ($my_query->have_posts() && $tax_term->name==$current_child ) : $my_query->the_post(); ?>
<li style="margin-left:15px;list-style:none;"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
}
wp_reset_query();
}
}
echo \'</ul>\';
?>
<!-- end other posts -->