Community Forums › Forums › Archived Forums › Design Tips and Tricks › Genesis Custom Loop
- This topic has 19 replies, 5 voices, and was last updated 10 years, 1 month ago by Summer.
-
AuthorPosts
-
July 16, 2014 at 6:12 am #114509emmtreParticipant
What's the correct syntax to use several category names ('blog' or 'quote') as argument?
remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'site_front_page' ); function site_front_page() { global $paged; global $query_args; $args = array( 'category_name' => 'blog', 'paged' => $paged, ); genesis_custom_loop( wp_parse_args( $query_args, $args ) ); } genesis();
July 16, 2014 at 6:23 am #114513Genesis DeveloperMembertry this way
'category_name' => array('blog','quote','video')
July 16, 2014 at 6:44 am #114516emmtreParticipantThanx but that didn't work. Here is the php error code for the second category. The first category works though.
Warning: urlencode() expects parameter 1 to be string, array given in /public_html/wp-includes/formatting.php on line 3690
July 16, 2014 at 8:54 am #114533marybaumParticipantCould you do a Boolean there?
'category_name' => 'blog' || 'quote' || 'video',
I'm asking as much as suggesting. I don't know if a Boolean counts as a string.
Another thing might be to declare a variable:
$catname = 'blog' || 'quote' || 'video';
then do
'category_name' => $catname,
I have no idea, without trying them myself, if those would work.
Guess I oughta Google argument syntax. 😉
Sharing the good news about the wonders of modern CSS and the split-step. Either one should get you moving fast. 😀
July 16, 2014 at 11:35 am #114550Brad DaltonParticipantDepends on how you use them http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
July 16, 2014 at 3:28 pm #114570emmtreParticipantBrad,
I't trying to use 'category_name' => 'blog', 'quote', as I have done in the past but now for a custom front-page template in a Genesis child theme but only posts with the first category with the slug 'blog' is displayed. The strange thing is that it seems to work with a page and the Genesis built-in blog template but not with my custom front-page template. And according to Bill Erickson the syntax is correct. Any ideas? Has something changed in WordPress or Genesis lately?
July 16, 2014 at 3:38 pm #114573emmtreParticipantThis is really strange. When I use the argument and the plus "+" operator for displaying posts that have both the 'blog' and 'quote' category all posts that have either the 'blog' or 'quote' category is displayed instead? There must be a logical error somewhere in the latest Genesis version?
remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'site_front_page' ); function site_front_page() { global $paged; global $query_args; $args = array( 'category_name' => 'blog' + 'quote', 'paged' => $paged, ); genesis_custom_loop( wp_parse_args( $query_args, $args ) ); } genesis();
July 16, 2014 at 4:09 pm #114576Brad DaltonParticipantHave you tried pre_get_posts to exclude categories? This is the most efficient way to alter the loop.
July 16, 2014 at 4:55 pm #114583SummerMemberAre you trying to display posts that are in either category or only in both categories?
The correct syntax for queries for posts in multiple categories should be:
'category__and' => array( 2, 6 )
You have to use the category ID, not the slug for this one.
from http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 17, 2014 at 12:59 am #114616emmtreParticipantI have done some more testing and can only conclude that the genesis_custom_loop doesn't work with the latest versions of Genesis and WordPress.
You should be able to add categories by adding them to the end of the parameter list (separated by commas) with 'cat' and 'category_name' as stated in the WordPress Codex.
When used with the built-in Genesis blog template and custom fields it does work as expected with several categories but not with a custom front-page.php or home.php template.
The 'cat' and 'category_name' only accept the first category in a list (separated by commas) due to some problem in WordPress with the format and syntax for integers and strings as argument.
From the php error code it seems like WordPress doesn't accept integers or strings anymore in the parameter list du to some internal format and sanitize functions in WordPress.
But 'category__and' and 'category__in' does work as expected with several categories since the parameter list is in an array with all the category id's separated by commas.
Another problem I also discovered with the genesis_custom_loop was that the sticky posts didn't work in the custom front-page.php template.
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
I have not used pre_get_posts to exclude categories before in a template but I will give it a try. Thanx for the suggestion and link.
July 17, 2014 at 8:54 am #114658SummerMemberI think your testing criteria may be flawed, because a number of folks here (including me) are still using genesis_custom_loop just fine. 🙂
Are you sure exactly what $query_args contains when being called in this function, before squishing it together with your new $args?
Just for grins, replace your custom loop call with this:
genesis_custom_loop ( $args );
and see what happens.also, 'cat' requires the category ID (a number), and 'category_name' takes the slug (a string), not the actual category name. If WordPress even casually mentioned that they would stop accepting those parameters, the Internet would have crashed from the number of people crying out simultaneously in rage & retribution 🙂
So if you want to use category_name, I think you need this:
category_name => 'array( 'blog' ,' quote' )
or
category_name=> array( 'blog'+'quote' )
I should go test that, shouldn't I?
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 17, 2014 at 10:59 am #114673emmtreParticipantSummer, yes you should definitely go testing yourself since you can't use an array with 'cat' or 'category_name'... Then you can try to add categories to 'cat' or 'category_name' just by adding them separated by commas as stated in the WordPress Codex. And I sure do know that 'cat' requires the category ID (number) and 'category_name' the slug (string). My testing environment is a freshly installed WordPress 3.9.1 and Genesis 2.1.2.
remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'site_front_page' ); function site_front_page() { global $paged; global $query_args; $args = array( 'category_name' => 'blog', 'quote', 'video', 'paged' => $paged, ); genesis_custom_loop( wp_parse_args( $query_args, $args ) ); } genesis();
July 17, 2014 at 11:26 am #114682SummerMemberYou have a syntax problem with the comma separated values, which is what I was trying to resolve.
The array parameters are always done in pairs, "variable => value," but since you have in $args a list of "variable => value, value, value," it cannot figure out what you want, so it takes the first pairing and dumps the rest.
You need to use category__in or category__and and the category IDs
This works:
function site_front_page() { global $paged; $args = array('category__in' => array (1, 41), 'paged' => $paged); genesis_custom_loop( $args ); }
I even tested it in a front-page.php template.
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After DarkJuly 17, 2014 at 11:45 am #114685Genesis DeveloperMemberI tried this code and it is working. i am getting all posts from three categories:
add_action( 'genesis_loop', 'site_front_page' ); function site_front_page() { global $paged; $args = array( 'cat' => '1,7,10', 'paged' => $paged ); genesis_custom_loop( $args ); }
July 17, 2014 at 11:46 am #114686emmtreParticipantYes but you should be able use comma separated values as stated in the WordPress Codex and it does work with the Genesis built-in blog template but not with the front-page template.
And you can use 'category__in' instead which accept an array of values but then you have to change all categories since this doesn't show any posts from sub-categories (children).
I was just qurious about how others have solved this issue since it will require a lot of manual work to change all categories for all posts.
And it look like genesis_custom_loop_args will accept comma separated values for' category_name' but they are stripped by some internal format and sanitize functions in WordPress.
I will find a solution with genesis_custom_loop since I need the front-page template to also be able to display widget areas with other post categories like 'news' on the front page
Thanx for the help!
July 17, 2014 at 11:49 am #114688emmtreParticipantThanx genwrok I have not tested with singel quotes outside the category IDs.
'cat' => '1,7,10',
July 17, 2014 at 11:49 am #114689Genesis DeveloperMemberI think that main issue is here :
global $query_args; and wp_parse_args( $query_args, $args ). You do not need to use the 'wp_parse_args' function. genesis_custom_loop is already using this function.
July 17, 2014 at 11:50 am #114690Genesis DeveloperMemberthose are my category IDs. I just tested on my dev site and it is working,
July 17, 2014 at 12:00 pm #114694emmtreParticipantAnd the winner is genwrock! You had to place the single quotes outside the comma separated values. Many thanx!
'category_name' => 'blog, quote, video',
July 17, 2014 at 1:06 pm #114711SummerMemberRegarding the commas/quotes, If it had been a snake, it woulda bit us 🙂
Also, you can have the custom loop and the widget spaces in your front-page.php... it's not an either/or thing.
Good luck!
WordPress / Genesis Site Design & Troubleshooting: A Touch of Summer | @SummerWebDesign
Slice of SciFi | Writers, After Dark -
AuthorPosts
- The topic ‘Genesis Custom Loop’ is closed to new replies.