1

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.

3 Answers 3

1

You can change your get_posts $args

Example:

$args = array(
    'orderby' => 'rand',
    'category_name' => 'your_category',
    'showposts' => 1
);
2
  • thanks for the reply giolliano sulit but still not working, would the first line be overiding the categorty selection? if ( false === ( $totd_trans_post_id = get_transient( 'totd_trans_post_id' ) ) ) { Commented Aug 1, 2017 at 18:29
  • Well you can try to run just the query in the else{ and see if that works. Commented Aug 1, 2017 at 23:02
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.

0

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();

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.