• Hello. I’m designing a theme and have a problem that I can’t find a proper solution to. I bet there is a simple solution.
    I want a custom template for displaying only attachments, sort of a gallery. So I figured Could use the template_redirect hook to load the ‘gallery.php’ template if the $_GET[‘gallery’] variable is set, but I don’t know how to get the proper posts from the database. I can call the ‘get_posts’ function and load all attachment posts into a variable and then use it to get thumbnails into the gallery template, but this make all template functions like the_post() & have_posts() unusable and I have to replicate those functions, as well as the paginating features which seem like an unnecessary overhead.

    I’ve also tried running this in the function called by template_redirect:

    $args = array(
    		'numberposts' => $number, 'offset' => $startat,
    		'category' => 0, 'orderby' => 'post_date',
    		'order' => 'DESC', 'include' => '',
    		'exclude' => '', 'meta_key' => '',
    		'meta_value' =>'', 'post_type' => 'attachment',
    		'suppress_filters' => true
    		);
    		$r = wp_parse_args($args, $defaults);
    
    		$wp_query->query($r);

    (Which is basically the same technique as the get_posts() function but used on the global $wp_query object instead of a temporary WP_Query. All variables in the array are defined.)
    This yield have_posts() = false.

    I’ve looked everywhere but haven’t found the preferred method for this. What am I missing here?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom templates and queries’ is closed to new replies.