很快就完成了,这至少应该是一个开始。这里的基本概念是将所有帖子都标记为主要人物(在本例中,Tom Cruise,您必须自己设置该变量。一旦有了这些变量,就可以遍历它们,然后foreach
电影(高射炮、热带雷霆、不可能完成的任务等),获取其他actors
为了那部电影。从那里,您生成一个数组,其中包含参与者slug(尽管您可以在对象中使用id或其他任何东西,实际上,适合您的需要)作为键,关联性(仅爬行出一个级别)作为值。然后你做一个arsort
以获得最相关的顶部,这将允许您for( $i=0; $i<5; $i++)
然后做一些无聊的HTML之类的东西。
$wp_query = new WP_Query;
$args = array(
// post basics
\'post_type\' => \'Movies\', // check capitalization, make sure this matches your post type slug
\'post_status\' => \'publish\', // you may not need this line.
\'posts_per_page\' => 10, // set this yourself, 10 is a placeholder
// taxonomy
\'tax_query\' => array(
array(
\'taxonomy\' => \'actors\', // slug for desired tag goes here
\'field\' => \'slug\',
\'terms\' => \'Tom Cruise\', // should work without a slug, try it both ways...and use a variable, don\'t hardcode
)
)
);
$results = $wp_query->query( $args );
$related = array();
if( count( $results ) > 0 ) {
foreach( $results as $result ) {
$new_related = get_the_terms( $result->ID, \'actors\' ); //get terms for this post
if( is_array( $new_related ) ) {
foreach( $new_related as $v ) {
$name = $v->name;
if( array_key_exists( $name, $related ) )
$related[$name]++; // add to total if it\'s already there
else
$related[$name] = 1; // initialize if it does not
}
}
}
arsort( $related, SORT_NUMERIC );
}