0

I'm trying to get a random output in the taxonomy archive for my custom taxonomy "areas"

I have the taxonomy-areas.php in which I have a standard loop. This one displays the posts from 3 custom-post-types: Hotels Pubs Restaurants

As soon as I try to get them randomly with

<?php query_posts("orderby=rand"); ?>

it stops working, and starts to display standard posts as in index.php.

I've tryed to add parameters to query posts, but it degenerates the loop, that doesn't displays anymore the right archive for the taxonomy of course. How can I fix it? I would like to avoid to create a taxonomy-aree-$term.php for each and every single area I'm going to define in my custom-taxonomy...!

After some attempts I've tried with this one,

<?php query_posts("posts_per_page=24&orderby=rand&areas=north-east"); ?>

and tried to make it dynamic within the current taxonomy with this:

<?php query_posts("posts_per_page=24&orderby=rand&areas=$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->slug;"); ?>

But with a blank output.

Thanks and best regards.

1 Answer 1

2

I've finally found the solution thanks to a friend of mine:

exploiting

get_queried_object()

we can set the object name in the script as follows:

<?php
    $obj=$wp_query->get_queried_object();

    $args=array(
    'tax_query' => array(
        array(
            'taxonomy' => 'areas',
            'field' => 'slug',
            'terms' => $obj->name
            )
        ),
        'orderby' => 'rand'
    );

    query_posts($args);
?>

Hope this can help.

Enjoy! :)

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.