Forums

Display a blog category on a page? (9 posts)

  1. ldexterldesign
    Member
    Posted 3 years ago #

    Hi guys,

    I realise this question must come up a lot, so excuse me. Surprisingly I'm struggling to find examples of this in action, and get a hook for accomplishing this.

    Basically I have want to duplicate SOME of the content from my index.php in a page. Pseudo code would look something like this:

    // this code would be placed in my page.php
    check page ID
    if page ID == 6 // faq/blog page
    then display all posts with cat ID == 5 && cat ID == 6 // faq and blog cat IDs respectively
    else display conventional page content

    Thanks guys,
    L

  2. ldexterldesign
    Member
    Posted 3 years ago #

    PS. I'm aware I'll probably be looking at query_posts here?

  3. stvwlf
    Member
    Posted 3 years ago #

    Hi

    This code goes before the wordpress loop in your page.php template. I think you are saying you want two categories displaying on one page. I am as an example assuming that the page slug for the page you want these displayed on is faq

    This code only affects that one page - all other pages will display normally.

    <?php
    $cats = '';
    if (is_page('faq')) {
      $cats='cat=5,6'
    }
    
    if ($cats) {
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       query_posts("$cats&amp;paged=$paged");
    } ?>

    Now the WP loop starts

    <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>

    I set this up to make it easy to add additional pages / categories

    See http://codex.wordpress.org/Template_Tags/query_posts for more details

  4. ldexterldesign
    Member
    Posted 3 years ago #

    @stvwlf - You're the man dude. That works a treat.

    I'm off to educate myself about query_posts() now then :]

    Thanks a lot,
    L

  5. ldexterldesign
    Member
    Posted 3 years ago #

    Is there a reason you've included paged in this? The following seems to work fine, I'm just curious:

    <?php
      $cats = '';
      // if page is faq/blog page
      if (is_page('6')) {
          $cats = 'cat=5,6';
      } // if page is case studies page
      elseif (is_page('7')) {
          $cats = 'cat=7';
      }
      // query appropriate category content
      query_posts($cat);
    ?>

    I've read this (http://codex.wordpress.org/Category_Templates#Text_Displaying_Only_on_First_Page_of_Archive); but my (old) problem didn't include an archive page. I interpret the benefit of what's being said behind the previous link is you can allow users to locate the post they want from the page titles faster, without the clutter of the body content, or teaser-text?

    Thanks,
    L

  6. ldexterldesign
    Member
    Posted 3 years ago #

    Correction:

    <?php
      $cats = '';
      // if page is faq/blog page
      if (is_page('6')) {
          $cats = 'cat=5,6';
      }
      // if page is case studies page
      elseif (is_page('7')) {
          $cats = 'cat=7';
      }
      // query appropriate category content
      if ($cats) {
          query_posts($cats);
      }
    ?>
  7. ldexterldesign
    Member
    Posted 3 years ago #

    Gives me control over how many posts are displayed (I'm sure there's a more efficient way of doing it):

    <?php
      $cats = '';
      // specify how many posts to show
      $noPosts = 2;
      // if page is faq/blog page
      if (is_page('6')) {
          $cats = 'cat=5,6';
    	  $cats = $cats . '&amp;' . 'posts_per_page=' . $noPosts;
      }
      // if page is case studies page
      elseif (is_page('7')) {
          $cats = 'cat=7';
    	  $cats = $cats . '&amp;' . $noPosts;
      }
      // query appropriate category content
      if ($cats) {
          query_posts($cats);
      }
    ?>
  8. stvwlf
    Member
    Posted 3 years ago #

    as far as your code

    if (is_page('6')) {
          $cats = 'cat=5,6';
    	  $cats = $cats . '&amp;' . 'posts_per_page=' . $noPosts;
      }
      // if page is case studies page
      elseif (is_page('7')) {
          $cats = 'cat=7';
    	  $cats = $cats . '&amp;' . $noPosts;
      }

    You are missing part of the code for page 7
    $cats = $cats . '&amp;' . $noPosts;
    should be either
    $cats = $cats . '&amp;' . 'posts_per_page=' . $noPosts;
    or if using the default # of pages per page
    $cats = $cats;

    You don't need the paged code until the # of posts you have to display exceeds the # of posts you are displaying on one page. Then you will find that without the paged code, when you click the 'prev posts' link that it displays the same posts that are on the first page, over and over.

  9. grapheezy91
    Member
    Posted 2 years ago #

    hi, i tried applying this code but it didnt work for me!
    heres my website

    http://www.wwww.wethebesttv.com/blog

    and i'm trying to put the blog category in the blog page so whenever i select the blog category i want it to post in the blog page...here is the blog page

    http://www.wethebesttv.com/blog/?page_id=10
    and here's my page.php

    <?php get_header(); ?>
    <style>
    html,body {
    background:#0D0D0D;
    color:#565050;
    }
    </style>
    <div id="page">
    
    	<div id="insidecopy">
        	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php if (is_page(blog)) {
          query_posts("cat=9");
       } ?>
    
        	<div class="copycontent"><a name="read"></a>
                <h2><?php the_title() ?></h2>
                <p><?php the_content(''); ?></p>
            </div>
            <?php endwhile; else: ?>
    		<?php endif; ?>
           <div id="altsidebar">
    		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Page Sidebar') ) : ?>
                <div class="altsideblock">
                    <h3>Configure Your Sidebar!</h3>
                    <p>To remove this message, configure your sidebar widgets throught the WordPress admin control panel.</p>
                </div>
            <?php endif; ?>
    
        </div></div>
    </div>
    
    <?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.