Wordpress 显示类别 ID 的帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11287389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress displays posts of category ID
提问by Miker
Can't seem to find the right answer for what I thought would be trivial.
似乎无法为我认为微不足道的事情找到正确的答案。
I have some categories arranged like this...
我有一些这样排列的类别......
Parent Category 1
- Child Category 1
- Child Category 2
- Child Category 3
父类别 1
- 子类别 1
- 子类别 2
- 子类别 3
...and I have some posts that are in Child Category 2. I want my page to display all posts from the category I am currently in.
...我有一些属于子类别 2 的帖子。我希望我的页面显示我当前所在类别的所有帖子。
This is what I am doing right now:
这就是我现在正在做的事情:
<?php
query_posts('cat=2&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
As you can see I am having to manually specify the category (cat=2), but instead I want it to automatically detect the category I am already in and display the posts (that way if I'm in a different category it will display those posts).
如您所见,我必须手动指定类别 (cat=2),但我希望它自动检测我已经所在的类别并显示帖子(这样,如果我在不同的类别中,它将显示那些帖子)。
Any suggestions?
有什么建议?
Thanks in advance. (SO Community = Awesome Sauce).
提前致谢。(SO 社区 = 很棒的酱汁)。
回答by sandip patil
try below code:
试试下面的代码:
<?php
$current_cat_id = get_query_var('cat');
$showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
回答by Tyler Carter
If you are using category.php
, you can omit the query_posts
and it will auto fill in the posts for you.
如果您正在使用category.php
,则可以省略query_posts
,它会自动为您填写帖子。
回答by Hardik Patel
<?php
// Start the loop.
$categories = get_categories('child_of=1');//Parents category id
foreach ($categories as $cat) {
$option = '<a href="/category/archives/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;//parents sub category name
$option .= '</a>';
echo $option;
query_posts('cat=$cat->cat_ID&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; }?>
回答by Omprakash Patel
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'category' => 1 );
// 1 is a cat id.
//
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
回答by Engr Zardari
try
尝试
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'cat' => 1 );
回答by Aakash Sharma
Try this, this is better solution for this and you can also use it to show Related Post by a category id...
试试这个,这是更好的解决方案,您也可以使用它来按类别 ID 显示相关帖子...
Just call the function that mentioned below by using this line. Put this in your template or page.php/single.php file.
只需使用此行调用下面提到的函数。把它放在你的模板或 page.php/single.php 文件中。
Call by put this line: related_post_title('enter cat id here..');
通过放置此行调用: related_post_title('enter cat id here..');
Here is the Function and put this in function.php
file.
这是函数并将其放入function.php
文件中。
Related posts function:
相关帖子功能:
function related_post_title($cat_id){
$cat = $cat_id;
// Check if it is page only
if ( is_page() || is_single()) {
$args=array(
'cat' => $cat,
'order' => DESC,
'orderby' => rand,
'post__not_in' => array($post->ID),
'posts_per_page' => 9999,
'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo ' <ul> ';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
}
If any query please let me know, i will help you...
如果有任何疑问,请告诉我,我会帮助你...