the permalink and post title won’t be available until after you’ve run the query. the following should return the last 3 posts from the designated category.
the same post is being displayed 3 times because it doesn’t look like you’re setting the permalink or post title from within “the loop”
if (!function_exists('get_posts_menus')):
function get_posts_menus($category_name, $count)
{
$args = array(
'category_name' => $category_name,
'posts_per_page' => $count
);
$the_menu = new WP_Query($args);
while ($the_menu->have_posts()) {
$post_class = get_the_id();
$permalink = get_the_permalink();
$title = get_the_title();
$the_menu->the_post();
echo "<li class=\"post-id-$post_class\"><a href=\"$permalink\" alt=\"$title\">$title</a></li>";
}
wp_reset_postdata();
}
endif;
Related Posts:
- how to use pre_gets_posts to exclude one queried ID from homepage loop
- relation OR instead of AND – Filtered term ID’s in loop
- Custom query for custom post type not getting correct post ID
- Get post Number with local loop and template
- Problem with custom WP_Query and underlying pagination/posts_per_page
- Calling a function with WP_Query only ever brings the first result
- How to write a query-function as a query-shortcode?
- Only display post if published in last 24 hours?
- Need to convert string of term ids into comma separated integers for use in an arguments array
- how to access query string in wordpress?
- WordPress | Date not always appear [duplicate]
- Custom taxonomy.php not working
- Array as ‘key’ in WP_Query
- How to remove the most recent post from $the_query
- different for loop if screen resolution < 1000px
- WP_Query loop else statement not executing
- Change query from cat id to slug or name?
- How do I get my custom query to work with search results after the first page?
- Using Wp_Query without the loop?
- Is_single() conditional tag returns null in query
- How do I rewrite this loop as a new WP_Query style-loop?
- Transient api Caches confused
- Issue with front page navigation after upgrading to 3.4
- Include current post into loop
- Merge two search functions for custom post type
- Custom WP_Query id
- Best choice for multiple loop in page?
- get_template_part causes 500 error
- WP Query – order posts by meta field first and then order the rest
- Loop increase number
- Make a SQL query with wpdb in WordPress
- How to display the featured image for each post?
- Assign custom parameter to each post in query
- How to provide meta_key array to wp_query?
- Wp_query with 2 meta keys and array of meta values
- Adding a html class based on post count
- Check for the main query from the template
- Trying to retrieve random post, getting a page
- Error with function in functions.php?
- Posts in loop all show the same author when there are many authors
- Exclude recently updated post from custom WP_Query using multiple loops
- get_posts output always same post
- Multiple Rows in a using wp_query
- How to use WP_Query() on single.php?
- sort and display posts by custom field (wp-query and the loop))
- How to create custom query by keyword in post title?
- difference between querying database and using the loop
- If have posts a week old, display, else display a different loop
- WP_Query – Adding “offset” posts to the end of the loop
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Conditional operator OR not working with custom fields
- Loop increase in while loop not working
- Create a loop on my pages with new “WP_Query”
- Get posts that do not have the same tags as current
- posts archive page – closing WP_Query loop correctly [closed]
- Create a page template for “top rated posts” but show full content and not just a list
- has_excerpt() not working inside wp_query
- wp_query is showing posts from other categories
- Daily drip of posts – based on user ‘startData’ – ordered oldest to newest
- How to add tax_query to $args with concatenation
- Should I reset $wp_query?
- Meta query compare for ID’s greater than specific ID
- Exclude authors IDs from WP_Query
- Exclude a Woocommerce product from WP_Query
- WP ForLoop to compare meta information of posts to determine what post to display
- Loading two different AJAX requests on two different pages
- AJAX Breaking Offset Argument In WP Query
- WP_QUERY wrong ammount of posts
- Getting the post terms ‘wp_get_post_terms’ per post when within the functions.php file
- How to add sort order to incremented and paginated category loop
- How to load a script code only in posts?
- How does WP generate the default $query in WP_Query based on the URL?
- Continue or break the while loop
- How to filter query loop block with a search string from the query parameters
- inserting content of 1 Post to in another with a template hierarchy
- how to get category`s slug in WP_Query loop?
- Loop posts based on permalink term
- Save (and exclude) posts from loop to use in another loop on page
- Multiple meta_key ordering with pre_get_posts
- Get all posts with empty meta_value
- How to split a post and intercalate elements from a loop
- How to remove only the latest sticky post from the loop
- Expecting statement error from php loop using ACF plugin
- Loop 3 posts column wise and continue the loop into same columns
- How I can repeat 2 HTML templates in a WordPress Query?
- PHP – Loop custom post type categories within jQuery Tabs
- Check if a post has term inside loop
- How do I fit WP_Query arguments into a function?
- Create a notification for post field
- How to create a widget like this?
- How do I sort this custom list of sticky posts
- WP_Query with one category in args shows other categories
- Query only displays one page_id
- How to Order By Two Custom Fields?
- Display specific page (that is child) content on parent page
- Different Loop for tag pages?
- Add URL Rewrite Rule To WordPress
- Ordering posts by custom field and grouped by month
- Pagination for custom loop on custom page template is not displaying anything