我只想放弃上面的函数,因为代码中有几个bug,而且效率也不太高。我真的很惊讶它真的对你有用。
您最好的解决方案是编写一个完整的新函数。以下是我们想要做的事情以及我们将如何实现这一点
获取单个贴子页面上的当前贴子对象。$post
不可靠,因此我们将在此处使用主查询对象,并返回get_queried_object()
. 从这里,我们可以使用帖子ID和帖子类型来获取其他相关信息
查看当前帖子的条款wp_get_post_term()
. 我们将设置fields
参数仅获取术语ID。然后可以在我们的tax_query
添加一些验证以验证用户输入并清理输入
让我们把这一切都放在代码中(注意:这都是未经测试的,至少需要PHP 5.4+)
function get_related_posts( $taxonomy = \'\', $args = [] )
{
/*
* Before we do anything and waste unnecessary time and resources, first check if we are on a single post page
* If not, bail early and return false
*/
if ( !is_single() )
return false;
/*
* Check if we have a valid taxonomy and also if the taxonomy exists to avoid bugs further down.
* Return false if taxonomy is invalid or does not exist
*/
if ( !$taxonomy )
return false;
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !taxonomy_exists( $taxonomy ) )
return false;
/*
* We have made it to here, so we should start getting our stuff togther.
* Get the current post object to start of
*/
$current_post = get_queried_object();
/*
* Get the post terms, just the ids
*/
$terms = wp_get_post_terms( $current_post->ID, $taxonomy, [\'fields\' => \'ids\'] );
/*
* Lets only continue if we actually have post terms and if we don\'t have an WP_Error object. If not, return false
*/
if ( !$terms || is_wp_error( $terms ) )
return false;
/*
* Set the default query arguments
*/
$defaults = [
\'post_type\' => $current_post->post_type,
\'post__not_in\' => [$current_post->ID],
\'tax_query\' => [
[
\'taxonomy\' => $taxonomy,
\'terms\' => $terms,
\'include_children\' => false
],
],
];
/*
* Validate and merge the defaults with the user passed arguments
*/
if ( is_array( $args ) ) {
$args = wp_parse_args( $args, $defaults );
} else {
$args = $defaults;
}
/*
* Now we can query our related posts and return them
*/
$q = get_posts( $args );
return $q;
}
现在我们有了一个更好的功能,我们可以在单个帖子页面或内容模板部分中使用它,具体取决于您的具体用例。您可能已经注意到,我们的新功能
get_related_posts()
有两个参数,第一个参数接受单个分类法值,第二个参数是参数数组。此参数将传递给查询的参数,因此您可以传递可接受的任何有效参数数组
WP_Query
和
get_posts
在这里
示例:
您需要返回一篇文章,因此可以尝试以下操作:(请注意,不要在此处使用post类型参数或任何分类类型参数,您可能会得到意外的输出)
if ( function_exists( \'get_related_posts\' ) ) {
$related_posts = get_related_posts( \'my_taxonomy_name\', [\'posts_per_page\' => 1] );
if ( $related_posts ) {
foreach ( $related_posts as $post ) {
setup_postdata( $post );
// Use your template tags and html mark up as normal like
the_title();
the_content();
// etc etc
}
wp_reset_postdata();
}
}
根据评论,您的PHP版本似乎早于5.4版本,它不支持新的短数组语法(
[]
), 因此你得到了可怕的WSOD。为此,需要将新的数组语法更改为旧语法(
array()
).
您可以尝试以下操作:
function get_related_posts( $taxonomy = \'\', $args = array() )
{
/*
* Before we do anything and waste unnecessary time and resources, first check if we are on a single post page
* If not, bail early and return false
*/
if ( !is_single() )
return false;
/*
* Check if we have a valid taxonomy and also if the taxonomy exists to avoid bugs further down.
* Return false if taxonomy is invalid or does not exist
*/
if ( !$taxonomy )
return false;
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !taxonomy_exists( $taxonomy ) )
return false;
/*
* We have made it to here, so we should start getting our stuff togther.
* Get the current post object to start of
*/
$current_post = get_queried_object();
/*
* Get the post terms, just the ids
*/
$terms = wp_get_post_terms( $current_post->ID, $taxonomy, array( \'fields\' => \'ids\') );
/*
* Lets only continue if we actually have post terms and if we don\'t have an WP_Error object. If not, return false
*/
if ( !$terms || is_wp_error( $terms ) )
return false;
/*
* Set the default query arguments
*/
$defaults = array(
\'post_type\' => $current_post->post_type,
\'post__not_in\' => array( $current_post->ID),
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomy,
\'terms\' => $terms,
\'include_children\' => false
),
),
);
/*
* Validate and merge the defaults with the user passed arguments
*/
if ( is_array( $args ) ) {
$args = wp_parse_args( $args, $defaults );
} else {
$args = $defaults;
}
/*
* Now we can query our related posts and return them
*/
$q = get_posts( $args );
return $q;
}
然后要在模板中使用代码,请更改为
if ( function_exists( \'get_related_posts\' ) ) {
$related_posts = get_related_posts( \'my_taxonomy_name\', array( \'posts_per_page\' => 1) );
if ( $related_posts ) {
foreach ( $related_posts as $post ) {
setup_postdata( $post );
// Use your template tags and html mark up as normal like
the_title();
the_content();
// etc etc
}
wp_reset_postdata();
}
}