Genesis loop for one category in home

Community Forums Forums General Discussion Genesis loop for one category in home

This topic is: not resolved

Tagged: 

This topic contains 8 replies, has 2 voices, and was last updated by  marcello.vicidomini 7 months, 1 week ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #95173

    marcello.vicidomini
    Member
    Post count: 17

    Hi all,
    i’m trying to display a loop for just one category in the homepage. I’ve written a custom loop which is driving me a little bit crazy, since it seems to accept everything I define in the array except ‘cat’ (I’m sure i’m using the correct category ID, and I also tried with different IDs). So in spite of having

    'cat' => 29

    defined in the loop, all the categories keep appearing in the homepage :(

    What am I doing wrong?

    This is the code I have in my home.php file:

    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_loop', 'child_grid_loop_homepage' );
    function child_grid_loop_homepage() {
    	if ( function_exists( 'genesis_grid_loop' ) ) {
    		genesis_grid_loop( array(
    			'features' => 0,
    			'feature_image_size' => 0,
    			'feature_image_class' => 'alignleft post-image',
    			'feature_content_limit' => 200,
    			'grid_image_size' => 'homepage-featured-image',
    			'grid_image_class' => 'alignleft post-image',
    			'grid_content_limit' => 150,
    			'more' => __( '[Continue reading...]', 'genesis' ),
    			//* The next line is ignored and all the categories keep being included in the loop. Why?
    			'cat' => 29,
    			'post_per_page' => 6
    		) );
    	} else {
    		genesis_standard_loop();
    	}
    }

    Thanks a lot for your help! :D

    #95205

    braddalton
    Participant
    Post count: 10253
    #95256

    marcello.vicidomini
    Member
    Post count: 17

    Hello braddalton,

    thanks a lot for your answer!
    Don’t you think that pre_get_posts would be impractical? It looks like each time you add a new category you have to edit the code. Or maybe I’m not getting something? Isn’t there a way to include one category rather then excluding all the other ones?

    Thanks again for your help :D

    #95260

    braddalton
    Participant
    Post count: 10253

    Yes, you can see the examples on the Codex which you can include in the code.


    #95644

    marcello.vicidomini
    Member
    Post count: 17

    Hello Brad,
    I think I solved it, here’s the code:

    if ( function_exists( 'genesis_grid_loop' ) ) {
    
    	//* Here's the line I added. I used the slug instead of the id, but if works with cat_ID too
    	query_posts('category_name=blog');
    
    	genesis_grid_loop( array(
    		'features' => 0,
    		'feature_image_size' => 0,
    		'feature_image_class' => 'alignleft post-image',
    		'feature_content_limit' => 200,
    		'grid_image_size' => 'homepage-featured-image',
    		'grid_image_class' => 'alignleft post-image',
    		'grid_content_limit' => 0,
    	) );
    } else {
    	genesis_standard_loop();
    }
    #95651

    braddalton
    Participant
    Post count: 10253
    #95653

    marcello.vicidomini
    Member
    Post count: 17

    Ok, that was clear :D

    Should I use WP_Query instead?

    #95655

    braddalton
    Participant
    Post count: 10253

    Why don’t you simply display your category archive page on the front page?


    #95656

    marcello.vicidomini
    Member
    Post count: 17

    Because in the front page I have to display other elements too that are not from the category (an image slider), and then I have a different page showing the archive for the category.
    So I thought to have the following structure:
    * home.php: with a sidebar to include the slider as a widget, and a custom loop to show the last 6 posts from “Blog” category
    * A link in the menu pointing to the “Blog” category archive page.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.