Grow your CSS skills. Land your dream job.

FAO PHP gods, feeling very nice

  • # October 16, 2012 at 5:04 pm

    Hello gods,

    I want to dynamically display random posts of the same category the post being looked at is in.

    So for example:

    You’re looking at a post about cats in the pet’s category.

    I want to also display a random selection of the other posts in that same category, ie: dogs, horses, hamsters, etc.

    However it seems possible that this code should also be clever enough to display random posts if this was a locations category, showing me new york, paris, london, etc.

    I really don’t know where I would even begin with this.

    I’m using body class to allow me to style other elements according to their category. Not able to get my head around how this would work using that method.

    I hope i’ve made sense &

    Thank for you all your help as always!

    # October 16, 2012 at 7:44 pm

    Just to have something for you to quickly edit, and perhaps make myself clearer. I found this somewhere..

    < ?php
    $args=array(
    ‘cat’ => 3,
    ‘orderby’ => ‘comment_count’,
    ‘order’ => ‘DESC’,
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => 6,
    ‘caller_get_posts’=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) { ?>

    < ?php }

    wp_reset_query(); ?>

    (sorry can’t get the code tags to work)

    It works but I need it so that rather than me defining the category value, the post being looked at will automatically determine this instead.

    Is this possible!??

    I hope this makes sense and thank you for your time.

    # October 16, 2012 at 9:12 pm

    I want to just double check before I state anything; you are looking to get the categories ID? Or just the category name?

    # October 16, 2012 at 11:54 pm

    Thanks for your time..

    Whichever I need so that I can display thumbnails and excerpts of that particular category currently being looked at.

    It’s all for a simple “more posts” widget in the sidebar. I just need those more posts suggestions to be of the same category as the post currently being looked at. If it helps I’ve made it so that the body class is given a name of this same category also.

    I hope this has made sense, and thanks once again!

    # October 17, 2012 at 11:32 am

    Have you tried using the wordpress function:

    get_the_term_list( $post->ID, ‘taxonomy-here’)

    http://codex.wordpress.org/Function_Reference/get_the_term_list

    # October 17, 2012 at 12:08 pm

    So I managed to find a solution.

    I’m currently using…

    < ?php
    foreach((get_the_category()) as $category) {
    $postcat= $category->cat_ID;
    $catname =$category->cat_name;

    }
    ?>
    < ?php
    $args=array(
    ‘cat’ => $postcat,
    ‘orderby’ => ‘comment_count’,
    ‘order’ => ‘DESC’,
    ‘post_type’ => ‘post’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => 3,
    ‘caller_get_posts’=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) { ?>

    < ?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

    Just need a bit of help making it so that it shows random posts. Right now I get the same 3 posts show. I would like for these to be randomised on a every reload.

    Anyone?

    Thanks.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".