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?
- Display products from specific category in shop page
- WP_Query: query posts by ids from array?
- How to get order of posts?
- pre_get_posts with get_posts
- Executing Queries in tag.php
- Query Custom Meta Value with Increment
- Identify which loop you are hooking into; primary or secondary?
- Order by two meta keys
- Sort posts by custom taxonomy name
- Trying to list out child pages with WP_Query
- Exclude first 5 posts of specific categories in the main loop
- How to bring specific post to front of wordpress loop?
- meta_query on a date range using an array of values
- Why execute the_post()?
- Posts loop with pagination on a single post page
- WP_Query Order by Specific Post ID First
- How to change posts order on Category and Tag archives?
- WooCommerce Only OnSale Products Loop Snippet [closed]
- Apply CSS class to every second database record [closed]
- How to prevent writing duplicate loops?
- Using one WP_Query object within the loop of another WP_Query object
- WP_Query on custom field and order results
- how to merge a WPQuery array with a PHP array and use the Loop to Output the result
- Query posts by specific word on title
- Reset postdata to custom query in nested queries
- WP_Query condition affects posts_per_page count
- Custom Search Not Consistent with Results
- Display both standard posts in a specific category along with a custom post type in a single loop?
- Orderby is working with one query but not with other
- 1/3 of posts different class
- Two loops on archive page
- Loop Split two Columns
- How to order posts by title after they have already been sorted by category
- How to make a second query offset -2 from current post
- Running main loop 2 times with conditional statement
- WordPress | Date not always appear [duplicate]
- Array as ‘key’ in WP_Query
- Is_single() conditional tag returns null in query
- Wp_query with 2 meta keys and array of meta values
- Adding a html class based on post count
- Posts in loop all show the same author when there are many authors
- Multiple Rows in a using wp_query
- How to create custom query by keyword in post title?
- If have posts a week old, display, else display a different loop
- Create a loop on my pages with new “WP_Query”
- Create a page template for “top rated posts” but show full content and not just a list
- Daily drip of posts – based on user ‘startData’ – ordered oldest to newest
- How to add tax_query to $args with concatenation
- WP ForLoop to compare meta information of posts to determine what post to display
- How to split a post and intercalate elements from a loop
- How do I fit WP_Query arguments into a function?
- How to create a widget like this?
- Different Loop for tag pages?
- Add URL Rewrite Rule To WordPress
- Hiding carousel indicators if there’s only one post
- WordPress dynamic AJAX query
- 2 queries with counters
- Does query_posts have an effect on get_the_category?
- Second loop pagination changes URL, not content
- WP Query get only 1 post (sticky, not sticky etc)
- WP_Query not returning correct result with meta_query parameter
- Duplice post with standard WP loop – fixed by using query_posts() instead
- Show custom content when loop or wp query reaches specific time?
- Get categories within specific term
- Loop through array of pages
- Ajax buttons not working properly in WooCommerce when using wc_get_template_part
- the_post_thumbnail and multiple WP_Query
- I have 3 categories, i want to display on a loop the last 3 of every category
- WP_Query & shortcode : Return 3 articles from a category WordPress
- WP_Query doesn’t works inside loop
- Seach and categories not working when ignoring sticky posts in main loop
- View related categories in order of posts
- 2 Loops on one page – pagination of 2nd loop ignore array of excluded posts
- WP_query for posts with same post type AND same post category
- How to solve this script problem in theme directory?
- Show posts in category
- Both WP_Query and get_posts returning 1 post
- List ALL posts within last week from current category on category page
- Custom post type archive organized by category
- wp_query display posts based on day
- Sorting Posts Based On Meta Value Using AJAX
- No content found on page 2 of pagination with 1 post per page
- Converting an existing query_posts to WP_Query
- Loop being strainge
- Multiple loops on index page with sticky post and pagination
- Problem with WP_Query sort
- Get author meta data with no published posts in author.php?
- WP_Query and DES sort for Custom Taxonomy based upon a meta field?
- Help with showing text when something is entered in my search bar
- Print post data inside loop using theme content template
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Prevent WordPress loop from displaying similar post titles
- Custom WordPress loop, articles not in same order as Array