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.

I created a custom post type "landing_page" to hold content I want to display on the top of category archive pages.

So for each category, I have one landing_page entry tagged with that category. What do I have to add to the category archive.php template to get it to show that category's (or custom taxonomy term's) landing_page content?

query_posts(
    array( 'posts_per_page' => 1,
    'post_type' => landing_page,
    'category' => [[???]] ));

     while (have_posts()) : the_post();
    the_content();
endwhile;
?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

I'd suggest adding something like this to the top of the theme file or wherever you want this content to appear in the archive, category or whatever..

// Check if it's a category or taxonomy archive
if( is_category() || is_tax() ) {
    // Grab the queried data, slug, tax, etc..
    $queried = $wp_query->get_queried_object();
    // Check taxonomy and slug are set
    if( isset( $queried->taxonomy ) && isset( $queried->slug ) ) {
        // Look for a landing page post type with a slug that matches the current queried slug
        $landing_page = get_posts( 'name=' . $queried->slug . '&post_type=landing_page&posts_per_page=1&nopaging=1' );
        // If the result wasn't empty
        if( !empty( $landing_page ) ) {
            // Output the title and content using the same filters WP uses in the loop
            echo apply_filters( 'the_title', get_the_title( $landing_page->ID ) );
            echo apply_filters( 'the_content', get_the_content( $landing_page->ID ) );
        }
    }
}

This will should do what you want without interupting the main category query for the archive.

Hope that helps.

share|improve this answer
    
Looks good... but get_posts will return an array, so I think you might have to refer to $landing_page[0]->ID to get the title and content. –  goldenapples Apr 7 '11 at 23:18
    
I might use new WP_Query() instead of get_posts(). I have yet to fully understand the differential efficiency/best practices surrounding WP_Query/get_posts/query_posts (even with explanatory diagrams like this: wordpress.stackexchange.com/questions/1753/…)—but my understanding is that the others involve WP_Query, so using WP_Query itself is more direct. –  supertrue Jun 27 '11 at 16:46

You can get the current category with

and since you are using archive.php and not category.php check if its a category first so something like this:

if (is_category()){
query_posts(
    array( 'posts_per_page' => 1,
    'post_type' => 'landing_page',
    'category' =>  get_query_var('cat'));

     while (have_posts()) : the_post();
        the_content();
     endwhile;

}
share|improve this answer
    
Missing quotes around your post type value.. ;) –  t31os Mar 8 '11 at 14:18
    
thanks i edited my answer –  Bainternet Mar 8 '11 at 14:20

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.