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.

Hey Folks I have a page called Journal that I would like to filter all my posts from a specific post category.I created a function but am not sure how to do it. I tried get_post etc but it throws a format error and am also now sure what I should be ammending. I realize that I could do this in the main template tag but I would rather use a function if possible.

function JournalPage() {

if(is_page("Journal")) {

}

}
add_filter('the_content', 'JournalPage'); 
share|improve this question
    
Is there a particular reason you can't create a file to act as a template, why does this need to happen without the addition of a file? I'm simply curious as to why you're set on not creating a new file.. –  t31os Feb 4 '11 at 23:53
    
To add... the_content is a hook that occurs each time a post's content is output, hooking onto that would append or prepend your given content onto the output of "each" post/page... (and that's probably not what you're aiming - though it would occur once in this instance, that's not a good approach). I can suggest a suitable hook, but i'd rather understand your reasoning for not using an actual template file before i make that suggestion. :) –  t31os Feb 5 '11 at 0:00
    
So what is it now? Was my answer the sollution? Could you please mark one or vote up/down anything? Thanks. –  kaiser Feb 7 '11 at 11:45

1 Answer 1

up vote 2 down vote accepted

You need to query them: (for more info see inline comments)

/**
 * Template Name: Journal
 */    
// define( 'TEXTDOMAIN', 'your_textdomain_string' );  
    // To be sure, we reset the query, so nothing get's lost
        wp_reset_query();

        global $wp_query;
        $query_cat = new WP_Query( array(
             'orderby'      => 'date comment_count' // date = default, 2nd param = comment_count
            ,'order'        => 'ASC'
            ,'post_type'    => 'post'
            ,'post_status'  => 'publish'
            ,'category_name'=> 'slug_of_cat' // change value here
        ) );
        if ( $query_cat->have_posts() ) :
            while ( $query_cat->have_posts() ) : $query_cat->the_post();
                // check for password
                if ( post_password_required() ) :
                    the_content();
                elseif ( current_user_can('manage_options') ) : // see codex roles & caps for more info
                    // display some decent message here if restricting accessability to the post
                    // or place something like 
                    echo '<span class="whatever">'.sprintf(__(You can %1$s this page), TEXTDOMAIN), edit_post_link( __('edit', TEXTDOMAIN), '&nbsp;', ')', $post_id ).'</span>';
                    return;
                else :

                    // here goes your content

                endif;
            endwhile;

        else : // else; no posts
            _e( 'Nothing published so far.', TEXTDOMAIN );
        endif; // endif; have_posts();

        wp_rewind_posts(); // for a sec. query
share|improve this answer
1  
Thanks for your help! and this goes in the if statement then right? –  Driftwood Feb 4 '11 at 19:12
    
No, not really. I don't understand why you're making a function and trying to add a filter for the content. the_content function just retrieves and displays data that you added throught your post edit screen in admin UI to your database. You can't filter the query in there. The query is the always present database query about any information of the current request. What i showed you above is a whole template. You just have to add this to your theme folder and load the template for your "Journal" page. When you then call this page, you get the content @ "// here goes your content". –  kaiser Feb 4 '11 at 19:24
    
Note: The template will be avaible in a drop down on your page edit screen on the right side below the publish meta box (per default). You will need to add some mark-up to style how a post get's displayed @"//here goes the content". You should also add some sort of <div class="wrapper"> or else around the if ( have_posts() ) and close it after "no posts - endif; With a template it's the easiest and most maintainable sollution. ____ Oh, and pls don't forget to vote up and mark the sollution (check). Thanks. –  kaiser Feb 4 '11 at 19:26
1  
cool, thanks!!!!!! –  Driftwood Feb 7 '11 at 18:34
1  
10x @kaiser... was looking for that ;) –  Sagive SEO Jan 8 '12 at 23:13

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.