admin管理员组文章数量:1128522
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] => /?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(); ?>
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(); ?>
Share
Improve this question
edited Dec 6, 2023 at 9:31
Jesse Nickles
7357 silver badges19 bronze badges
asked Mar 12, 2013 at 9:01
Kenny BonesKenny Bones
1011 silver badge2 bronze badges
6
|
Show 1 more comment
2 Answers
Reset to default 1Take 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.
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)
本文标签: categoriesDisplay posts from a specific category on frontpagephp template
版权声明:本文标题:categories - Display posts from a specific category on frontpage.php template? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736726845a1949772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{
and}
in the if statement. I'm not sure if that needs to be there or not but – Kenny Bones Commented Mar 12, 2013 at 9:21print_r( $GLOBALS['post'] );
in the line above this block. Then update your question with this info. – kaiser Commented Mar 12, 2013 at 9:30