WordPress.org

Forums

[resolved] Calling category ID from The Loop (2 posts)

  1. scott5150
    Member
    Posted 1 year ago #

    I'm trying to determine how to directly reference a category in my loop. I created a new template page based off of the Blog template. This will be to display only posts from the category "latest-news."

    I think I found out how to do it, but the code appears to be from earlier versions of wordpress.

    Here's what I tried.

    <?php query_posts('category_name=world-dryer-events'); ?>
            <?php if ( have_posts() ) :
                while ( have_posts() ) : the_post();
                    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
                        $args = array('
                            offset'=> 0,
                            'paged'=>$paged,
                            'posts_per_page' => get_option('posts_per_page'),
                            'post_type' => 'post'
                            );
                        $all_posts = new WP_Query($args);
                        while($all_posts->have_posts()) : $all_posts->the_post(); ?>
    
                        <?php
                        /* Include the Post-Format-specific template for the content.
                         * If you want to overload this in a child theme then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        $format = get_post_format();
                        if( false === $format )  $format = 'standard';
                        get_template_part( 'postformats/'.$format, 'medium' );
    
                        ?>
    
                        <?php endwhile;
                endwhile; ?>

    The part I'm mainly concerned about is the first bit where I attempt to filter the posts down to posts from category "latest-news":

    <?php query_posts('category_name=world-dryer-events'); ?>

    I've also seen it done like these:

    'category=world-dryer-events'

    'cat=6'

    Anybody know how to make that first line filter only posts from the category I mentioned?

    Thanks.

  2. scott5150
    Member
    Posted 1 year ago #

    On this one, it wasn't necessary to use that first line (query_posts) at all. You can tell wordpress to do what I need from within array() object. In the following code, the line:

    'post_type' => 'post'

    Was changed to:

    'category_name' => 'world-dryer-events'

    Here's the whole loop revised:

    <?php if ( have_posts() ) :
                while ( have_posts() ) : the_post();
                    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
                        $args = array('
                            offset'=> 0,
                            'paged'=>$paged,
                            'posts_per_page' => get_option('posts_per_page'),
                            'post_type' => 'post'
                            );
                        $all_posts = new WP_Query($args);
                        while($all_posts->have_posts()) : $all_posts->the_post(); ?>
    
                        <?php
                        /* Include the Post-Format-specific template for the content.
                         * If you want to overload this in a child theme then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        $format = get_post_format();
                        if( false === $format )  $format = 'standard';
                        get_template_part( 'postformats/'.$format, 'medium' );
    
                        ?>
    
                        <?php endwhile;
                endwhile; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags