0

I'm trying to display posts from specific categories. And I believe I have the correct code:

<?php if ( in_category('11') ) { ?>
     <div class="post"> 
          <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
          <div class="entry">
               <?php the_content(); ?>
          </div>
     </div>
<?php } ?>

I know there are posts in that category, because if I use the querystring ?cat=11 I can see them. Only not here. Nothing is showing. What am I doing wrong?

Edit: As requested, I'm posting the results from <?php print_r( $GLOBALS['post'] ); ?>

WP_Post Object ( [ID] => 10 [post_author] => 1 [post_date] => 2013-03-08 12:46:36 [post_date_gmt] => 2013-03-08 12:46:36 [post_content] => This is the content from my static frontpage.php [post_title] => Forside [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => forside [to_ping] => [pinged] => [post_modified] => 2013-03-12 07:30:56 [post_modified_gmt] => 2013-03-12 07:30:56 [post_content_filtered] => [post_parent] => 0 [guid] => http://vsandbox.net/wordpress/?page_id=10 [menu_order] => 0 [post_type] => page [post_mime_type] => [comment_count] => 0 [filter] => raw )

On a sidenote, I noticed in a different thread that someone had problems with this when not using it in single.php. I'm not even sure what that means. I mean, each layout of the webdesign has to be put in it's own page template php-file right?

Edit2: Here is the full code of the frontpage.php file:

<?php
/*
Template Name: Forside
*/
?>
<?php get_header(); ?>

            <div id="content">

                <div id="inner-content" class="wrap clearfix">

                    <div id="main" class="twelvecol first clearfix" role="main">

                    <!-- START LOOP -->
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php print_r( $GLOBALS['post'] ); ?>
                    <section class="entry-content clearfix" itemprop="articleBody">
                        <?php the_content(); ?>
                    </section> <!-- end article section -->

            <p>Innlemmet 2011</p>
                        <?php if ( in_category('11') ) { ?>
                test
                                <div class="entry">
                                    <?php the_content(); ?>
                                </div>
            <?php } ?>

            <p>Innlemmet 2012</p>
                        <?php if ( in_category('12') ) { ?>
                            <div class="post"> 
                                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                <div class="entry">
                                    <?php the_content(); ?>
                                </div>
                            </div>
            <?php } ?>

            <p>Innlemmet 2013</p>
                        <?php if ( in_category('12') ) { ?>
                            <div class="post"> 
                                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                <div class="entry">
                                    <?php the_content(); ?>
                                </div>
                            </div>
            <?php } ?>

                        <!-- END LOOP -->
                        <?php endwhile; else: ?>
                        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
                        <?php endif; ?>

                        </div> <!-- end #main --> 
                </div> <!-- end #inner-content -->

            </div> <!-- end #content -->

<?php get_footer(); ?>
6
  • Where are you using this code? Is it within a post loop?
    – Dom
    Commented Mar 12, 2013 at 9:14
  • Yes it is. I've seen a few examples of this snippet, and some use { and } in the if statement. I'm not sure if that needs to be there or not but Commented Mar 12, 2013 at 9:21
  • 1
    Do a print_r( $GLOBALS['post'] ); in the line above this block. Then update your question with this info.
    – kaiser
    Commented Mar 12, 2013 at 9:30
  • Is it a page your are using this template?
    – Horttcore
    Commented Mar 12, 2013 at 9:58
  • Yes it is a page :) Commented Mar 12, 2013 at 13:21

2 Answers 2

1

Take a look at multiple loops for examples

And also look at the content.php file in the Twenty Fourteen theme

<?php

// The Query
$category_query = new WP_Query( 'category__in=11' );

// The Loop
if ( $category_query->have_posts() ) {
    echo '<ul>';
    while ( $category_query->have_posts() ) {
        $category_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {

}
/* Restore original Post Data */
wp_reset_postdata();

Don't forget you can use pre_get_posts to alter an existing loop and exclude categories from any loop.

-1

I assume that you are using this template on a page. So you forgot to query posts for the categories. the_post(); is reflecting to the page not posts.

<?php
$query = new WP_Query('cat=12');
if ($query->have_posts()) :
    ?><p>Innlemmet 2012</p><?php
    while ($query->have_posts()) : $query->the_post();
        the_title(); ?>
    endwhile;
endif;
?>

Read about the WP_Query class here (https://codex.wordpress.org/Class_Reference/WP_Query)

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.