admin管理员组文章数量:1287230
I'm very new to word press having only installed it on the weekend, I'm fine with HTML & CSS but PHP & Wordpress is alien to me.
I'm trying to display a post from a specific category that changes every day, i've searched everywhere and found the below code whihc works to a point.
I just cant seem to get it to choose from a category number?
Any help or explanation would be much appreciated.
<?php
if ( false === ( $totd_trans_post_id = get_transient( 'totd_trans_post_id' ) ) ) {
$args = array('numberposts' => 1, 'orderby' => 'rand');
$totd = get_posts($args);
$midnight = strtotime('midnight +1 day');
$timenow = time();
$timetillmidnight = $midnight - $timenow;
echo $midnight;
echo ",".$timenow;
set_transient('totd_trans_post_id', $totd[0]->ID, $timetillmidnight);
} else {
$args = array('post__in' => array($totd_trans_post_id));
$totd = get_posts($args);
}
foreach( $totd as $post ) : setup_postdata($post); ?>
<div>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
</div>
<?php endforeach; ?>
EDIT Thanks for the suggestions but nothing seems to be working with my existing code, it still continues to pick a completey random post from all categories.
I'm very new to word press having only installed it on the weekend, I'm fine with HTML & CSS but PHP & Wordpress is alien to me.
I'm trying to display a post from a specific category that changes every day, i've searched everywhere and found the below code whihc works to a point.
I just cant seem to get it to choose from a category number?
Any help or explanation would be much appreciated.
<?php
if ( false === ( $totd_trans_post_id = get_transient( 'totd_trans_post_id' ) ) ) {
$args = array('numberposts' => 1, 'orderby' => 'rand');
$totd = get_posts($args);
$midnight = strtotime('midnight +1 day');
$timenow = time();
$timetillmidnight = $midnight - $timenow;
echo $midnight;
echo ",".$timenow;
set_transient('totd_trans_post_id', $totd[0]->ID, $timetillmidnight);
} else {
$args = array('post__in' => array($totd_trans_post_id));
$totd = get_posts($args);
}
foreach( $totd as $post ) : setup_postdata($post); ?>
<div>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
</div>
<?php endforeach; ?>
EDIT Thanks for the suggestions but nothing seems to be working with my existing code, it still continues to pick a completey random post from all categories.
Share Improve this question edited Aug 1, 2017 at 19:50 Howdy_McGee♦ 20.9k24 gold badges91 silver badges177 bronze badges asked Aug 1, 2017 at 6:13 BuzzinBillyBuzzinBilly 212 bronze badges3 Answers
Reset to default 1You can change your get_posts $args
Example:
$args = array(
'orderby' => 'rand',
'category_name' => 'your_category',
'showposts' => 1
);
Just use WP_Query() to generate your custom query, category parameters.
<?php
$category_query_args = array(
'orderby' => 'rand',
'category_name' => 'your_category'
);
$category_query = new WP_Query( $category_query_args );
?>
Note: you could also pass the category slug to the query, via category_name, instead of cat.
And just forward your loop.
<?php
if ( $category_query->have_posts() ) : while $category_query->have_posts() : $category_query->the_post();
// Loop output goes here
endwhile; endif;
?>
Hope This will help you.
You can use this following code snippet in any theme file or you can also create a new page template. Before using this code, don’t forget to replace category IDs that you want to fetch post from.
<?php
// display random post from specific categories
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => '2, 6, 17, 38', // category IDs
'orderby' => 'rand',
'posts_per_page' => '1', // get 1 post only
'ignore_sticky_posts' => 1,
);
$my_query = new WP_Query( $args );
// the loop
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
// display article
get_template_part( 'content', 'featured' );
endwhile;
endif;
wp_reset_postdata();
?>
本文标签: categoriesSelect a random post from a specific category
版权声明:本文标题:categories - Select a random post from a specific category? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741300403a2371059.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论