• Hello

    Ive followed the directions in your codex of how to create a page of posts, but the posts from the category will not show up. is there a way that I can show posts from a specific category, and its sub-categories? Here is the code that I created for the posts

    <?php
    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }
    if ($category) {
      $cat = get_cat_ID($category);
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $post_per_page = 4; // -1 shows all posts
      $do_not_show_stickies = 1; // 0 to show stickies
      $args=array(
        'category__in' => array($cat),
        'orderby' => 'date',
        'order' => 'DESC',
        'paged' => $paged,
        'posts_per_page' => $post_per_page,
        'caller_get_posts' => $do_not_show_stickies
      );
      $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
      if( have_posts() ) :
    		while ($wp_query->have_posts("cat=1")) : $wp_query->the_post("cat=1"); ?>
    <span class="style1"><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php
    $short_title = the_title('','',false);
    $short_title = substr($short_title,0,44);
    echo $short_title;
    if($short_title_2!=$short_title) { echo "..."; }?></a></strong><br /></span>
    <span class="style1"><?php the_excerpt(); ?></span>
    <span class="style1">▪ <?php the_time('d M, Y','',''); ?> ▪ <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?><br /><br /></span>
    <?php endwhile; ?>
    
    <?php else : ?>
    <?php endif; 
    
    	$wp_query = $temp;  //reset back to original query
    
    }  // if ($category)
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • Okay, in the query you can show posts by categories by using that codings
    in index.php

    <?php global $wp_query;
    query_posts(
       array_merge(
          $wp_query->query,
          array('category__in' => array(1,2,3))

    you will have to change the numbers 1,2,3 to your categories one. if i put that code on my website mines are strangely 13,19,28,39

    it will make your categories becomes pages alike

    Hi,
    Which theme are you using?

    Is this a page and not categories.php or index.php loop, then it could be a template page?

    Last what value have you entered in the page custom field, the custom field should be named category and the value an ID of a category.

    Remove the “cat=1” in this line

    while ($wp_query->have_posts("cat=1")) : $wp_query->the_post("cat=1"); ?>

    Change to:

    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    HTH

    David

    Thread Starter SolidSnake2003

    (@solidsnake2003)

    I titled the page Home Page, and called the template file Front Page with the file name as home.php

    Ok this is not hanging together, home.php does not need a template name and cannot have custom fields, home.php will load first by priority (Template Heirarchy).

    The code you posted works by setting a custom field, where this bit of code is returning the category id from a custom field set on a page.

    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }

    If you know the category then you will need to hard code it after this block which could be removed, if the category was 112.

    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }
    //Set the category for the front page here!
    $category=112;

    And remove the “cat=1” code as above, and that should work.

    HTH

    David

    Not sure what theme you are using the code above is a little dated, so I have created another post with a twenty eleven download.

    Note: 'caller_get_posts' is depecated use 'ignore_sticky_posts'

    Regards

    David

    Thread Starter SolidSnake2003

    (@solidsnake2003)

    The code is not showing the posts, but is showing a link to to the page

    Post the whole pages code up to https://pastebin.com

    Bits of code do not give the full picture, the code becomes hard to read without all the code and the indentation is lost.

    The twenty eleven child theme in the link above works as you wanted it plus some other features, so I know we are close.

    We really need to see all the ‘home.php’ code, can you paste the code to pastebin and the link into a reply.

    Regards

    David

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Page of Posts: How to show posts in a specific category’ is closed to new replies.