Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

A bit of a noob PHP question coming up.

I am trying to create a generic archive style view for my custom post types. Ideally I want to create one generic template that can be used for all post-types.

Currently I use this as my have_posts query to bring in all posts of 'custom-post-type'

<?php
 $args=array(
   'post_type' => 'custom-post-type.',
   'post_status' => 'publish',
   'posts_per_page' => -1,
   'caller_get_posts'=> 1
 );
 $my_query = null;
 $my_query = new WP_Query($args);
 if( $my_query->have_posts() ) {
 while ($my_query->have_posts()) : $my_query->the_post(); 
?>

What I want to do is swap out custom-post-type with the slug of the page so that whenever this template is assigned to a page, it will look for a custom-post-type of that name & list them.

Alternatively if you know of a better way of doing it - great!

Cheers, George

share|improve this question
add comment

2 Answers 2

Try this:

$page = $wp_query->get_queried_object();
$slug = $page->post_name;

PS I think next WP release will get proper custom post archives or something around that...

share|improve this answer
    
Sorry, can't get this to work. –  George Wiscombe Oct 21 '10 at 9:06
    
Details? If you use this inside function don't forget global $wp_query;. –  Rarst Oct 21 '10 at 9:31
add comment
up vote 1 down vote accepted

As @Rarst mentioned in the previous comment this is going to be patched in WP3.1

The template hierarchy is "archive-customposttypename.php".

WordPress 3.1 template hierarchy

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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