admin管理员组文章数量:1420078
How can I put a loop in my single.php that shows the posts of the current category of the single and following the code structure that I have? The question comes, because I can not find the correct code.
I have tried with this code
<?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>';
}
}
?>
But as is obvious, this would only give me the category with ID number 3, and I need it to be the same as the single post that is being viewed.
Thanks
-EDIT-
This is my full single.php code
<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>
How can I put a loop in my single.php that shows the posts of the current category of the single and following the code structure that I have? The question comes, because I can not find the correct code.
I have tried with this code
<?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>';
}
}
?>
But as is obvious, this would only give me the category with ID number 3, and I need it to be the same as the single post that is being viewed.
Thanks
-EDIT-
This is my full single.php code
<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>
Share
Improve this question
edited Jul 12, 2019 at 7:05
Dario B.
asked Jul 12, 2019 at 6:34
Dario B.Dario B.
15510 bronze badges
9
|
Show 4 more comments
2 Answers
Reset to default 1I suggest the following:
$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();
Note the following points about the code:
- I use
wp_list_pluck()
to get the category IDs in a single line. - I don't use
post__not_in
. This has known performance issues. Instead I query for 1 more post than we need so that we can remove the current post later if it appears in the results. This will be faster than usingpost__not_in
. - I use
get_posts()
instead ofWP_Query
. This is because it has more appropriate defaults for secondary queries (such as settingignore_sticky_posts
andno_found_rows
totrue
. - Once we have results I use
wp_list_filter()
to remove the current post, if it appears in the results. I do this by usingget_queried_oject_id()
to get the ID of the current post. If it doesn't appear in the results then I usearray_pop()
to remove the last post to get back down to 3 posts.
If you are trying to get "Related Posts" by category for custom taxonomy then see the code given below:
$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);
Replace "your-taxonomy-name" and "your-post-type" with valid data.
本文标签: categoriesloop in singlephp of the same category
版权声明:本文标题:categories - loop in single.php of the same category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745327624a2653665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
shows the posts of the current category of the single
. It was about displaying the title of posts from any category that is assigned to the currently displayed post? – nmr Commented Jul 12, 2019 at 7:05