Modify your first code chunk thusly:
$names = array();
$CatID = array( 1, 5, 3,);
foreach ( $CatID as $ID ) {
$names[] = get_the_category_by_ID( $ID );
// uncomment the next line to get the name tied to the ID
// $names[$ID] = get_the_category_by_ID( $ID );
}
var_dump( $names );
See PHP’s array()
reference for more info on using arrays.
Why your code isn’t working
In your first example, you’re looping through your $CatID
array, assigning the return from get_the_category_by_ID()
to $names
. Each time through the loop, you’re overwriting the $names
variable, which is why only 3
— the last one in your list — is sticking.
In your second example, you’re swinging too far the other way — you’re trying to get the value of $ID[$x]
, but $ID
isn’t an array, so you’re handing an invalid value (probably null
) to get_the_category_by_ID()
. That’s why it’s returning a passel of WP_Error
objects.
Related Posts:
- How to array only one key from another array
- Get categories names as an array to use it in theme settings
- How to get the last category name of a child category?
- Output category list inside array
- Finding and removing duplicates within WP Arrays
- Using multiple variables to assign categories to an array
- How to filter specific element of an array in wordpress/php?
- wp_get_archive for category returning different URLs on different but similar sites
- !in_array doesnt recognize category
- Display current category title on category page
- Inject post (from specific category) between posts in Loop
- Metabox repeating fields – radio buttons not saving correctly
- How can merge two arrays values in one array and save in database
- Allow authors to post only in categories they create in WordPress
- List all categories but exclude current post category on single post page
- Compare two meta key values against each other inside the get_posts array?
- WP_User_Query order by meta_key that is an array
- Edit category output
- Can an array be used as a meta_query value?
- List of posts by day of the week
- Custom Loop through category menu to include sub categories
- Different background-image by category
- Get posts with at least one category in common with current post?
- Woocommerce – remove product from category
- Get category name from custom loop and echo it once
- ‘orderby’ => ‘rand’ alternative for better performance?
- Create shortcodes within foreach loop (using array)
- hover image appears below placeholder instead of overlayed
- get current product name in functions.php
- Filter Select results based on selection
- How to exclude category ID from Looper in WordPress
- Add a specific part of current category page url to shortcode
- how do I get a specific post from a post with a subcategory in WP
- WP grandchild categories in nested ul li
- Reprinting tags with all attributes
- How to pick the default selected value in wordpress dropdown?
- Get page that displays all children of taxonomy parent
- Checking array against author id in loop
- How to show only subcategories in parent category not parent category?
- Dilemma of Populating all the categories in a drop down list
- Grouping posts by a custom meta value
- JSON REST API WordPress only showing first 10 categories
- Get all categories post is not in
- Get latest post from all categories except one
- Error while setting role
- Rewrite with pagination /foo/page/2/ to posts of a given category, page 2
- In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?
- Loop over Array and get the distinct ids
- Remove empty terms from array, sort alphabetically, update back to repeating field
- Only show first image in foreach loop
- How to get current post category details inside “loop”?
- Post not populating for custom post type based on category selection
- Filter by field with array value in ACF on WP REST API
- Removing sidebar on category pages
- Only load certain artists on this page
- add the value of a variable returned in a while loop [closed]
- Compare current post Category in select menu
- Custom functions for string data calculations
- Get the list of post categories
- Remove Custom Post Category In WordPress Slug Using .Htaccess
- Woocommerce. Get a list of products in the current category on the archive page and product page
- How to show categories and date on posts
- Problem with inserting multiple images in gallery of each WooCommerce product programmatically
- Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
- Allow two posts (from different categories) to have the same slug
- Get main parent category for post (WordPress/Advanced Custom Fields)
- Category Page Displaying all Pages
- Trying to show the category of a post in the post display
- Link to index.php from home.php?
- ACF – Get ID of relationship field in flexible content
- Hide a specific category title from displaying on site
- Replace an array (with identical values) with another array in the_content
- Return to the beginning of the results of a for loop
- Combine multiple queries, array_unique returns nothing
- Populate Posts based on category selected using AJAX that means without page refresh
- random woocommerce categories are not showing when count enabled?
- How to Retrieve All Category Images on Front-End
- get_users when from meta key that has serialized values
- How to replace hard-coded list of custom taxonomy terms based on custom field query [closed]
- Cant get unique_array() work on get_the_category() foreach loop
- Get a list of tags present in a paged front page
- get_category display only 1
- Add custom filter to register data in array
- Page returning ID from array, how to return the correct values for post in acf wordpress
- Auto remove empty values in array
- Display a post based on its metabox selection
- Unserializeing multiple column values that are stored in one database results variable
- Make an array listing custom taxonomy
- Get category id for a custom category and display it in a class
- Is there a way to randomize and connect a background and header image?
- dynamically filter by category via sub-menu
- get_permalink returning first letter
- Get unique post in parent category
- Incomplete term slugs output from a foreach loop with get_the_terms
- How to print array of specific item
- How to insert the first letter in uppercase [closed]
- Grabbing value of input field inside of array
- Custom WordPress Function – Adding items from Foreach Loop into an array and Updating Field based on array of IDs (ACF + WooCommerce)
- How to limit number of posts in the certain category and exclude the oldest post automatically
- PHP – Converting elements to actual values