Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS FAO PHP gods, feeling very nice

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #40331
    DeTodo
    Member

    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!

    #112021
    DeTodo
    Member

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

    $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() ) { ?>

    (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.

    #112025
    Malazath
    Member

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

    #112030
    DeTodo
    Member

    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!

    #112093
    Malazath
    Member

    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

    #112116
    DeTodo
    Member

    So I managed to find a solution.

    I’m currently using…

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

    }
    ?>
    $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() ) { ?>

    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)
  • The forum ‘CSS’ is closed to new topics and replies.