• Hi,

    Is it possible to make a static page and then have it showing posts from one specific category? (like when you follow a link like siteadress/?cat=xx) I have been messing about with query and get_post, but i can’t seem to find the proper use for them.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Yes. Create the page as normal, but make it a template with comments at the top like this:

    <?php
    /*
    Template name: special_page
     */
    ?>

    Then inside it, do this to get a category:

    if (have_posts()) {
      $my_query = new WP_Query('category_name=op-ed');
      while ($my_query->have_posts()) : $my_query->the_post();
        require("post.php"); // do all your posting code here
      endwhile;
    }

    Then for your static page, navigate downwards and look for the template, select the one named ‘special_page’ (as you named it at the top).

    Thread Starter rahbek

    (@rahbek)

    Thanks for the reply, but I still can’t figure out how to get it to work. I suppose I have to use one of the excisting files from my theme (page.php?) and then modify it as you described above? But does the code replace excisting code or does it just go before it? If I post the code, will you point me in the right direction?

    page.php:

    <?php get_header(); ?>
    <div id="content">
    	<div id="content-left">
    <?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    			<div class="box-left" id="post-<?php the_ID(); ?>">
    				<h3><a>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    				<?php the_content(); ?>
    			<div class="clear"></div></div>
    		<?php endwhile; ?>
    		<?php else : ?>
    		<h3>Not found!</h3>
    		<?php _e('Sorry, no posts matched your criteria.'); ?>
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    <?php endif; ?>
    	  </div><!-- end content-left -->
    	  <?php get_sidebar(); ?>
    	  <div class="clear"></div>
    </div><!-- end content -->
    <?php get_footer(); ?>

    Yes assuming that what you posted above is a copy of page.php (rename it to the template file, so let’s say you call it ‘special_page.php’). Don’t forget the comment bit at the top to name it.

    You would then have this as the code:

    <?php get_header(); ?>
    <div id="content">
     <div id="content-left">
      <?php if (have_posts()) :
             $my_query = new WP_Query('category_name=op-ed');
       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <div class="box-left" id="post-<?php the_ID(); ?>">
         <h3><a rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
         <?php the_content(); ?>
         <div class="clear"></div>
        </div>
       <?php endwhile; ?>
      <?php else : ?>
       <h3>Not found!</h3>
       <?php _e('Sorry, no posts matched your criteria.'); ?>
       <?php include (TEMPLATEPATH . "/searchform.php"); ?>
      <?php endif; ?>
     </div><!-- end content-left -->
     <?php get_sidebar(); ?>
     <div class="clear"></div>
    </div><!-- end content -->
    <?php get_footer(); ?>

    Then when you write a ‘page’ select the ‘template’ to use, picking the ‘special_page’ one that you just created.

    I’m struggling to do the same. Can you help me add this code to my page.php?
    Here is my page.php

    <?php get_header(); ?>
    
    	<div id="content">
    
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
          <?php
    // this grabs the image filename
    	$values = get_post_custom_values("pageimage");
    // this checks to see if an image file exists
    	if (isset($values[0])) {
    ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('url'); echo '/'; echo get_option('upload_path'); echo '/';
    // this is where the Page image gets printed
    	$values = get_post_custom_values("pageimage"); echo $values[0]; ?>" alt="pageimage" id="pagepic" /></a>
          <?php } ?>
    
    		<h2><?php the_title(); ?></h2>
    			<div class="entry">
    				 <?php the_content("<p class=\"serif\">" . __('Read the rest of this page', 'branfordmagazine') ." &raquo;</p>"); ?>
    
    				<?php wp_link_pages("<p><strong>" . __('Pages', 'branfordmagazine') . ":</strong>", '</p>', __('number','branfordmagazine')); ?>
    
    			</div>
    		</div>
    		<?php endwhile; endif; ?>
    	<?php edit_post_link('Edit', '<p>', '</p>'); ?>
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show posts on a page’ is closed to new replies.