4

I'm trying to get my news page to display content from one category only (number 3) but I cant seem to get it to work. Instead of just displaying posts from category 3 it displays posts from all category's.

Here is my code:

<?php get_header(); ?>

            <div class="content news_page">

                       <h1>Latest News</h1>     

                       <?php $args = array(
                            'post_type' => 'post' ,
                            'orderby' => 'date' ,
                            'order' => 'DESC' ,
                            'posts_per_page' => 6,
                            'category'         => '3',
                            'paged' => get_query_var('paged'),
                            'post_parent' => $parent
                       ); ?>
                       <?php query_posts($args); ?>




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

                                <div class="large-4 medium-4 small-12 columns">
                                    <div class="latest_news_cont">
                                    <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>

                                    <a href="<?php the_permalink() ?>"><h5><?php the_title(); ?></h5></a>
                                    <?php the_excerpt(); ?>
                                    <p style="text-align:center;"><a href="<?php the_permalink() ?>" class="readmore_news">Read more</a></p>
<br>
<div class="clear"></div>
                                       </div>
                                    </div>

                            <?php endwhile; ?>
                        <?php endif; ?>


<div class="clear"></div>

            </div><!--end of content-->



<div class="clear"></div>

<?php get_footer(); ?>
4
  • Exactly what doesn't work? Also, please do not us query_posts.
    – s_ha_dum
    Commented May 21, 2014 at 15:22
  • Hi, instead of displaying posts from a certain category it displays posts from all categorys Commented May 21, 2014 at 15:27
  • Is your news page your posts page set in your Reading Settings? Commented May 21, 2014 at 15:52
  • wordpress.stackexchange.com/a/290909/133699 Commented Mar 28, 2018 at 14:42

5 Answers 5

6

The argument isn't category, it is cat. Your query fails because you are using an argument that doesn't exist.

$args = array(
  'post_type' => 'post' ,
  'orderby' => 'date' ,
  'order' => 'DESC' ,
  'posts_per_page' => 6,
  'cat'         => '3',
  'paged' => get_query_var('paged'),
  'post_parent' => $parent
); 
$q = new WP_Query($args);
if ( $q->have_posts() ) { 
  while ( $q->have_posts() ) {
    $q->the_post();
    // your loop
  }
}

Notice that I have converted your query_posts() into a new WP_Query object. Do not use query_posts(), ever. Even the Codex states so.

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.

http://codex.wordpress.org/Function_Reference/query_posts

Also note that I removed unnecessary PHP opening and closing tags and formatted the code for better readability. That alternative control structure syntax is a formula for failure, in my experience.

7

Get First Five Posts With Specific Category

<?php
        // the query
        $the_query = new WP_Query(array(
            'category_name' => 'post_category_name',
            'post_status' => 'publish',
            'posts_per_page' => 5,
        ));
        ?>

        <?php if ($the_query->have_posts()) : ?>
            <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
                <?php the_category(); ?>
                <?php the_title(); ?>
                <?php the_excerpt(); ?>
                <?php the_post_thumbnail(); ?>
                <?php the_content(); ?>

            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>

        <?php else : ?>
            <p><?php __('No News'); ?></p>
        <?php endif; ?>
2

I would personally do this rather.

Instead of:

'category' => '3',

Replace it with this:

'category_name' => 'my-category-slug'

Obviously find the name of your category slug and replace 'my-category-slug'.

As mentioned by @s-ha-dum it would be better to not use this method and rather use WP_Query method. You can see it in the WordPress Codex here: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

1
  • Thanks. This gave me the solution to know that in "category_name" property you need to send the "slug" instead the name of the category (including spaces) Thanks! Commented May 31, 2021 at 16:19
1

Add this code in your functions file:

function wpsites_display_one_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'cat', '3' );
    }
}
add_action( 'pre_get_posts', 'wpsites_display_one_category' );

Change the is_home() conditional tag to match your news page or posts page loop if needed. Whatever you set in Settings > Reading.

0

You should change

'category'         => '3',

to in your code.

'cat' => '3',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.