WordPress - Show posts from only 1 category on main page

7 replies
  • WEB DESIGN
  • |
Im using the theme NewsMagazine. It shows the latest posts on the main page but the problem is it shows posts from every category.

I only want it to display posts from one specific category. How do i do this?

Here's the code from the themes index.php file

<?php get_header(); ?> <div id="contentwrap"> - Display a random image

Someone from the WordPress forum gave me this code but when i view the site it doesn't show the posts from the category 'Latest Offers', instead it says;

Not Found
Sorry, but you are looking for something that isn't here.
#category #main #page #posts #show #wordpress
  • Profile picture of the author Player87
    Check out My Blog. Do you want different categories like I have done it on the Home Page.

    If so then I can help you.
    {{ DiscussionBoard.errors[3398219].message }}
    • Profile picture of the author cooler1
      Originally Posted by Player87 View Post

      Check out My Blog. Do you want different categories like I have done it on the Home Page.

      If so then I can help you.
      I already have different categories. What i want is for the posts which are shown on the main page to only be from one specific category.

      Currently it shows the posts from every category.
      Signature

      {{ DiscussionBoard.errors[3398277].message }}
      • Profile picture of the author Patrick
        <?php
        query_posts('cat=x');
        while (have_posts()) : the_post();
        the_content();
        endwhile;
        ?>

        This will work..

        in cat=x x will be the ID of the category which you can know by going to categories in the wp-admin, hover your mouse over a category and you will see the ID in the status bar.
        {{ DiscussionBoard.errors[3398310].message }}
        • Profile picture of the author cooler1
          Originally Posted by schwarzes View Post

          <?php
          query_posts('cat=x');
          while (have_posts()) : the_post();
          the_content();
          endwhile;
          ?>

          This will work..

          in cat=x x will be the ID of the category which you can know by going to categories in the wp-admin, hover your mouse over a category and you will see the ID in the status bar.
          I tried that but it comes up with Parse error: syntax error unexpected '<', when i visit the site.

          Here is the index.php file Have i put it in the wrong section?

          <?php get_header(); ?>
          <div id="contentwrap">
          <div class="inside">
          <?php if (get_option('swt_slider') == 'Hide') { ?>
          <?php { echo ''; } ?>
          <?php } else { include(TEMPLATEPATH . '/includes/slide.php'); } ?>
          <?php
          = (get_query_var('paged')) ? get_query_var('paged') : 1;
          = array(
          'category_name' => 'Latest Offers',
          'posts_per_page' => -1,
          'paged' =>
          );
          <?php
          query_posts('cat=1');
          while (have_posts()) : the_post();
          the_content();
          endwhile;
          ?>
          ++; // increment the counter
          if( % 3 != 0) {
          = 'leftp';
          } else {
          = 'rightp'; }
          ?>
          <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
          <h2 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
          <?php if ( function_exists( 'get_the_image' ) ) {
          get_the_image( array( 'custom_key' => array( 'post_thumbnail' ), 'default_size' => 'full', 'image_class' => 'aligncenter', 'width' => '190', 'height' => '112' ) ); }
          ?>
          <div class="meta">
          <?php the_time('F d, Y'); ?> |
          <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »', 'comm'); ?>
          </div>
          <div class="entry">
          <?php the_content(''); ?>
          </div>
          <p class="postmetadata"><a class="more-link" href="<?php the_permalink() ?>#more">Read More</a></p>
          </div>
          <?php if(++ % 3 == 0) : ?>
          <div class="clearp"></div>
          <?php endif; ?>
          <?php endwhile; ?>
          <div class="navigation">
          <?php
          include('includes/wp-pagenavi.php');
          if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
          ?>
          </div>
          <?php else : ?>
          <h2 class="center">Not Found</h2>
          <p class="center">Sorry, but you are looking for something that isn't here.</p>
          <?php get_search_form(); ?>
          <?php endif; ?>
          </div>
          </div>
          <?php get_sidebar(); ?>
          <?php get_footer(); ?>
          Signature

          {{ DiscussionBoard.errors[3398367].message }}
        • Profile picture of the author Istvan Horvath
          Originally Posted by schwarzes View Post

          This will work..
          Not sure about that

          You may want to take a closer look at the index.php the OP posted in a pastebin (see the first post) BEFORE you give away code advice...

          Looking at the code you may also notice there are Multiple Loops in action there (search the Codex for that!).
          BTW, your code ended the "while" to early... there is another one later in the index.php
          Signature

          {{ DiscussionBoard.errors[3398432].message }}
          • Profile picture of the author davidlieder
            On many themes, the way that posts display on the home page is controlled by widgets.

            You should check to see if the home page display of posts is controlled by a widget. Check your widgets page and see if there is a default widget to display the content on the main part of the front page. If that is the case, you can change the widget to another different widget that gives more "category exclusion" options.

            Editing the theme is usually not a good idea, in my opinion. You obviously are not a code person. If you must edit the code, then you should hire someone to do it who can really understand the code. One little dot out of place and you will have other problems later on. Actually, changing anything in the code could also break something else, even if you get the desired results.

            David Lieder
            Astral Universe Worldwide Media
            {{ DiscussionBoard.errors[3401747].message }}
  • Profile picture of the author wpfixeu
    I believe that you or anyone else in future should go with something like this code:
    Code:
            function my_category_queries(  ) {
              if (() && ()){
                 ('category_name', 'wperror');
                 ('post__not_in', get_option( 'sticky_posts' ) );
              }
            }
            add_action( 'pre_get_posts', 'my_category_queries' );
    Note: simply paste it in functions.php file and enjoy!

    If you want to understand the code better check this artice: Show posts from single category on main page
    {{ DiscussionBoard.errors[9153679].message }}

Trending Topics