我怎样才能在我的单曲中打圈呢。php,显示当前类别的帖子的单条,并遵循我的代码结构?问题来了,因为我找不到正确的代码。
我已尝试使用此代码
<?php
$categories = get_the_category($post->ID);
if ( $categories ) {
$category_ids = array();
foreach ( $categories as $individual_category ) {
$category_ids[] = $individual_category->term_id;
}
$args=array(
\'category__in\' => $category_ids,
\'post__not_in\' => array($post->ID),
\'showposts\'=>3, // Number of related posts that will be shown.
\'ignore_sticky_posts\'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo \'<h3>Related Articles</h3>\'; // You can edit this
echo \'<ul>\';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<!-- do stuff -->
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo \'</ul>\';
}
}
?>
但很明显,这只会给我ID号为3的类别,我需要它与正在查看的单个帖子相同。
谢谢
-EDIT-
这是我的单曲全集。PHP代码
<div class="container">
<div id="breadcrumbs">
<?php if (function_exists(\'nav_breadcrumb\')) nav_breadcrumb(); ?>
</div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="single">
<div class="columns is-centered">
<div class="column is-two-fifths is-content">
<?php the_post_thumbnail(); ?>
<div class="title">
<?php the_title(); ?>
</div>
</div>
</div>
</div>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<hr/>
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach ($categories as $individual_category) {
$category_ids[] = $individual_category->term_id;
}
$args = array(
\'category__in\' => $category_ids,
\'post__not_in\' => array($post->ID),
\'showposts\' => 3, // Number of related posts that will be shown.
\'ignore_sticky_posts\' => 1
);
$my_query = new wp_query($args);
if ($my_query->have_posts()) {
echo \'<h3>Related Articles</h3>\'; // You can edit this
echo \'<ul>\';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<!-- do stuff -->
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo \'</ul>\';
}
}
?></div>
SO网友:Jacob Peattie
我建议如下:
$categories = get_the_category();
$category_ids = wp_list_pluck( $categories, \'term_id\' );
$args = [
\'numberposts\' => 4,
\'category__in\' => $category_ids,
];
$related_posts = get_posts( $args );
$related_posts = wp_list_filter( $related_posts, [ \'ID\' => get_queried_oject_id() ], \'NOT\' );
if ( count( $related_posts > 3 ) ) {
array_pop( $related_posts );
}
global $post;
foreach ( $related_posts as $post ) {
setup_postdata( $post );
// Output posts.
}
wp_reset_postdata();
请注意有关代码的以下几点:
我使用wp_list_pluck()
在单行中获取类别ID我不使用post__not_in
. 这已经知道了performance issues. 相反,我查询的帖子比我们需要的多出1篇,这样,如果当前帖子出现在结果中,我们可以稍后删除它。这将比使用post__not_in
.我使用get_posts()
而不是WP_Query
. 这是因为它对辅助查询(例如设置ignore_sticky_posts
和no_found_rows
到true
.一旦我们有了结果,我就使用wp_list_filter()
删除当前帖子(如果它出现在结果中)。我通过使用get_queried_oject_id()
获取当前帖子的ID。如果它没有出现在结果中,那么我使用array_pop()
要删除最后一个帖子,请返回到3个帖子
SO网友:Bhupen
如果您试图按类别获取自定义分类法的“相关帖子”,请参阅下面给出的代码:
$categories = get_the_terms($post->ID, \'your-taxonomy-name\');
if ($categories) {
$category_ids = array();
foreach ($categories as $individual_category) {
$category_ids[] = $individual_category->term_id;
}
$args = array(
\'post_type\' => \'your-post-type\',
\'post__not_in\' => array($post->ID),
\'showposts\' => 3, // Number of related posts that will be shown.
\'ignore_sticky_posts\' => 1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'your-taxonomy-name\',
\'field\' => \'id\',
\'terms\' => $category_ids,
\'operator\'=> \'IN\'
)
),
);
$my_query = new wp_query($args);
用有效数据替换“您的分类名称”和“您的帖子类型”。