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 have a custom template which should only show posts of specific categories. What do i need to change inside this code, to get this done?

if ( ! function_exists( 'attitude_theloop_for_template_blog_full_content_one' ) ) :
/**
 * Fuction to show the content of page template full content display.
 */
function attitude_theloop_for_template_blog_full_content_one() {
    global $post;

    global $wp_query, $paged;
    if( get_query_var( 'paged' ) ) {
        $paged = get_query_var( 'paged' );
    }
    elseif( get_query_var( 'page' ) ) {
        $paged = get_query_var( 'page' );
    }
    else {
        $paged = 1;
    }
    $blog_query = new WP_Query( array( 'post_type' => 'post', 'paged' => $paged, 'cat' => 1 ) );
    $temp_query = $wp_query;
    $wp_query = null;
    $wp_query = $blog_query; 

    global $more;    // Declare global $more (before the loop).

    if( $blog_query->have_posts() ) {
        while( $blog_query->have_posts() ) {
            $blog_query->the_post();

            do_action( 'attitude_before_post' );
?>
    <section id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <article>

            <?php do_action( 'attitude_before_post_header' ); ?>

            <header class="entry-header">
                <h2 class="entry-title">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute();?>"><?php the_title(); ?></a>
                </h2><!-- .entry-title -->
            </header>

            <?php do_action( 'attitude_after_post_header' ); ?>

            <?php do_action( 'attitude_before_post_content' ); ?>

            <div class="entry-content clearfix">
                <?php
                    $more = 0;       // Set (inside the loop) to display content above the more tag.

                    the_content( __( 'Read more', 'attitude' ) );

                    wp_link_pages( array( 
                        'before'            => '<div style="clear: both;"></div><div class="pagination clearfix">'.__( 'Pages:', 'attitude' ),
                        'after'             => '</div>',
                        'link_before'       => '<span>',
                        'link_after'        => '</span>',
                        'pagelink'          => '%',
                        'echo'              => 1 
               ) );
                 ?>
            </div>

            <?php do_action( 'attitude_after_post_content' ); ?>

            <?php do_action( 'attitude_before_post_meta' ); ?>

            <div class="entry-meta-bar clearfix">                       
                <div class="entry-meta">
                    <span class="by-author"><?php _e( 'By', 'attitude' ); ?> <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php the_author(); ?></a></span> |
                    <span class="date"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( get_the_time() ); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a></span> |
                    <?php if( has_category() ) { ?>
                    <span class="category"><?php the_category(', '); ?></span> |
                <?php } ?> 
                    <?php if ( comments_open() ) { ?>
                    <span class="comments"><?php comments_popup_link( __( 'No Comments', 'attitude' ), __( '1 Comment', 'attitude' ), __( '% Comments', 'attitude' ), '', __( 'Comments Off', 'attitude' ) ); ?></span> |
                <?php } ?>                              
                </div><!-- .entry-meta -->
            </div>

            <?php do_action( 'attitude_after_post_meta' ); ?>

        </article>
    </section>
<?php
            do_action( 'attitude_after_post' );

        }
        if ( function_exists('wp_pagenavi' ) ) { 
            wp_pagenavi();
        }
        else {
            if ( $wp_query->max_num_pages > 1 ) {
            ?>
                <ul class="default-wp-page clearfix">
                    <li class="previous"><?php next_posts_link( __( '&laquo; Previous', 'attitude' ), $wp_query->max_num_pages ); ?></li>
                    <li class="next"><?php previous_posts_link( __( 'Next &raquo;', 'attitude' ), $wp_query->max_num_pages ); ?></li>
                </ul>
                <?php 
            }
        }
    }
    else {
        ?>
        <h1 class="entry-title"><?php _e( 'No Posts Found.', 'attitude' ); ?></h1>
      <?php
   }
   $wp_query = $temp_query;
        wp_reset_postdata();
}
endif;

this code is from the file content-extensions.php in the library/structure folder, im using wordpress 3.6.1 with attitude-free from theme-horse.

share|improve this question
    
Are you hacking a theme directly? –  s_ha_dum Sep 18 '13 at 13:26
    
i guess, if you like to call it so, yes... afaik, there is no other option in this theme. –  Raymond Osterbrink Sep 18 '13 at 14:37
    
There is always (unless a theme is truly badly written) an option. It is called a "Child Theme". –  s_ha_dum Sep 18 '13 at 14:41
    
this is not necesary, also i dont know, how this should make my problem any less problematicly –  Raymond Osterbrink Sep 18 '13 at 14:50
    
See my answer. There are two good solutions. –  s_ha_dum Sep 18 '13 at 14:54
add comment

2 Answers

You can do that by using category parameter in the wp_query

change the line which looks like this

$blog_query = new WP_Query( array( 'post_type' => 'post', 'paged' => $paged ) );

to

$blog_query = new WP_Query( array( 'post_type' => 'post', 'paged' => $paged, 'cat' => 4 ) );

// Here '4' is the id of category you want to show, whereas you can also use the category name

For details see this link: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

share|improve this answer
    
sadly this changes nothing... i still get posts of all categories –  Raymond Osterbrink Sep 18 '13 at 13:33
    
sorrry, but that should work .. can you please paste the change you made to the code? –  Maruti Mohanty Sep 18 '13 at 13:57
    
made the change, also tried 'cat' = 1 (which broke my site) and 'cat=1' and 'cat=-1,-2,-3,-4' like shown on the link, but no reaction. –  Raymond Osterbrink Sep 18 '13 at 14:02
    
Are you sure that this function is being called? Are you sure that the template file that calls this function is being loaded? –  Chip Bennett Sep 18 '13 at 15:36
    
@Raymond if you are comfortable with the child theme go for it, it is a better way to do it but if you are no way planning to update your theme in future you can edit the theme files directly(but I would prefer child theme). coming back to the way you are writing it seems you are using 'cat' = 1, here the syntax is wrong it should be 'cat' => 1 –  Maruti Mohanty Sep 19 '13 at 5:31
show 1 more comment

@MarutiMohanty has plausible code, but I would suggest to you that you don't edit the theme file directly. Create a Child Theme and define attitude_theloop_for_template_blog_full_content_one in that child's functions.php with the alterations necessary. Your theme made that function pluggable so it can be replaced.

Alternately, create MU-Plugin. Create a folder at wp-content/mu-plugins, and put a .php file in there containing your code to replace that function.

Untested but either should work, I think, (possibly needing some tweaking) and are much better than hacking a third party theme.

share|improve this answer
    
Child theme passed my mind before writing the solution but Raymond has asked precisely what he could do in the above code, so i suggested this but child theming is the more right way to edit. –  Maruti Mohanty Sep 19 '13 at 5:26
1  
@MarutiMohanty : Be aware of the XY problem –  s_ha_dum Sep 19 '13 at 6:12
    
Thanks I will take care of this in future :) –  Maruti Mohanty Sep 19 '13 at 6:23
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.