I have a paid theme that has the following function manually hard-coded. It pulls the 8 latest posts and displays them:
global $do_not_duplicate;
global $post;
query_posts( array(
'posts_per_page' => '8',
'post__not_in' => $do_not_duplicate,
'ignore_sticky_posts' => 1
) );
I have looked at this page and tried to modify the code to show posts from the news category only (category number 1):
global $do_not_duplicate;
global $post;
// Attempt 1
query_posts( array(
'cat=1',
'posts_per_page' => '8',
'post__not_in' => $do_not_duplicate,
'ignore_sticky_posts' => 1
) );
// Attempt 2
query_posts( array(
'category=1',
'posts_per_page' => '8',
'post__not_in' => $do_not_duplicate,
'ignore_sticky_posts' => 1
) );
// Attempt 3
query_posts( array(
'category' => array(1),
'posts_per_page' => '8',
'post__not_in' => $do_not_duplicate,
'ignore_sticky_posts' => 1
) );
// Attempt 4
query_posts( array(
'cat=news',
'posts_per_page' => '8',
'post__not_in' => $do_not_duplicate,
'ignore_sticky_posts' => 1
) );
Nothing worked. The same list of 8 recent posts from all categories was returned.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The query_posts() function is almost wholly unnecessary and is discouraged in favor of WP_Query(). That being said we can look at the WP_Query() docs as they’re almost the same.
The cat parameter accepts either a comma separated string or integer. In this case we can use:
query_posts( array(
'cat' => 1,
'posts_per_page' => '8',
'post__not_in' => $do_not_duplicate,
'ignore_sticky_posts' => 1
) );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0