i am using page.php only which is default template for pages in wordpress, my header footer and sidebar are same on all pages but main area content is changing which i want wordpress to pull for each specific page. for example if page is about us ..it should show posts of about us category and so on...
i want to know if i can achieve this with conditional statements or i have to make separate templates for each page.
i tried something like ...
<?php if (page_id ==11 ):
// post loop to display all fetch all post of category id 4
$page_query = new WP_Query('post_type=post&cat=4'); ?>
<?php while ($page_query->have_posts()) :
$page_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
} ?>
<?php the_content(); ?>
<?php endwhile; endif;
wp_reset_postdata();
?>
this is my loop on page.php but it doesn't seem to work!
wow... not sure if this helps, but is much neater and cleaner than what you have currently
<?php
if (page_id == 11){
$page_query = new WP_Query('post_type=post&cat=4');
while ($page_query->have_posts()){
$page_query->the_post();
echo "<a href=/"". the_permalink() ."/" rel=/"bookmark/"><".the_title()."</a>";
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
the_content();
}
}
wp_reset_postdata();
?>
Still, you're unclear of what does not work, so it's hard to tell what isn't working...
I see this has never been resolved and I had nothing to do so I hope this will help someone in the near future.
If you want to show posts that are in relation to the page "about us" you should name that categorie the name of the page. By doing so you you can change to code above to the following:
global $post;
$pagename = get_the_title($post->ID);
$args = array(
'post_type' => 'post',
'category_name' => $pagename,
);
$page_query = new WP_Query($args);
while ( $page_query->have_posts() ) : $page_query->the_post();?>
<a href="<?php the_permalink(); ?>" rel="bookmark">
<?php the_title()?>
</a>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
the_content();
endwhile;