1

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

2 Answers 2

1

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

2
  • Sorry, can't get this to work. Commented Oct 21, 2010 at 9:06
  • Details? If you use this inside function don't forget global $wp_query;.
    – Rarst
    Commented Oct 21, 2010 at 9:31
1

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

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.