This is how your code should look like I added some comments so you will understand its simple programming not really wordpress related.
$locations = get_the_terms($post->ID, 'location');
$locations = array_values($locations);
$total_locations = count($locations); // the total start from 1
for($i = 0; $i < $total_locations; $i++) {
echo $locations[$i]; // echo the location
if($i < $total_locations-2) { // so for comma you need to check if the for loop variable is - 2 because the loop start from 0
echo ', '; // echo comma
} elseif($i < $total_locations-1) { // and here is just -1 because you want to print it before the last one because this we use the less than sign in both of our conditions
echo ' and '; // echo and
}
}
Related Posts:
- How to show a hierarchical terms list?
- Need a simple but complete example of adding metabox to taxonomy
- Automatically Assign Parent Terms When A Child Term is Selected
- get term by id without taxonomy
- get a specific taxonomy term name
- wp_set_object_terms() Fails to Set Terms
- get_terms() How many is TOO many?
- get_terms with posts that have a different taxonomy with term x
- List all taxonomy terms / Show links if posts are attached, else names
- How to add terms to taxonomy with wp_insert_terms?
- Get_term_by only returns one item from array
- Is there any difference between post in child term and child & parent terms?
- Create and move terms for taxonomies
- How to get the last term from taxonomy?
- Count posts for each taxonomy term for each month
- List post from current taxonomy children
- What hooks/filters are there to alter selected terms on post save?
- Count argument in get_terms has no effect on ouput/doesn’t work
- How can I remove links from the function “get term list”?
- Get Term names from WP Term Object
- orphaned taxonomy terms remove by sql query
- functions to create term and child terms
- has_term not working
- taxonomy/category term in URL slug irrelevant for post?
- Filter taxonomy terms using multiple id in the edit-tags.php
- How to use a Term Meta Field as a link
- WordPress built-in method to extract array of term IDs?
- How do I get the current tag out of a taxonomy?
- Variable in Array Not Working
- Add current class to queried term on taxonomy term archive
- How to list 2 taxonomy terms for a post, based on their hierarchy
- get_terms problem : related articles
- Why “Warning: Invalid argument supplied for foreach()”
- Order wordpress taxonomy parent terms by their children count
- WordPress User Taxonomy Saved Values
- how to get the assigned child term of the term’s parent
- get_the_terms error
- Use get_terms to get post_tags but limit to a taxonomy
- Set attachment tags from attachment’s custom field data
- Set multiple Post Terms in same Taxonomy
- how to show only last child terms of a taxonomy
- Ordering Taxonomies / Changing term_id
- Show child terms of current term of the taxonomy
- Fetch and ACF image field from a custom taxonomy outside category/archive page? [closed]
- conditional statement based on terms
- Adding taxonomy terms based on custom field
- Multi Taxonomies same terms
- Make taxonomy terms appear in the order they were made
- Exclude posts with taxonomy term from appearing on home page query
- the_terms characters not limiting
- Hierarchical list of custom taxonomy terms
- Return Taxonomy Name for Each Term
- Get term from multiple taxonomy
- Modify output of wp_dropdown_categories to add term IDs to each option
- How to return newly added terms when using wp_set_object_terms
- Blogroll links sorted by category in a table
- How to relate a user object to taxonomy term?
- How to display taxonomy term custom meta (using wp_get_object_terms?)?
- SQL query joining on taxonomy tables not returning what I would expect
- Echo taxonomy name – second level
- Match and Merge Terms in Different Taxonomies
- If has_term, load other theme
- How can I query all post with currrent taxonomy term?
- Retrieve all term IDs of post
- Bug using wp_insert_term with switch_to_blog
- how to list parent terms only
- How to order a taxonomy’s terms numerically, from lowest to highest using get_terms
- update_term_meta not working
- Assign Multiple Values from Post Fields to Taxonomy Terms
- wp_insert_term doesn’t want to enter its data into custom taxonomy
- $_POST sometimes empty even though isn’t when using edited_[taxonomy] hook
- Can’t display multiple terms with get_the_terms
- What is the use case for sharing a term between multiple taxonomies?
- Order custom taxonomy hierarchy
- I want to get term by term_name without taxonomy
- I want to change the slugs of my terms dynamically
- How to show children terms even if they are empty
- echo a tax term in loop
- taxonomy_edit_form_fields output after term fields
- Create multidimensional array of taxonomies
- $product->ID in has_term() not working, but manually inputting the ID works, why?
- Can I have have terms with the same slug in the same taxonomy?
- how do you get one specific term from a shortcode attribute?
- Get taxonomy url, name, post count & image
- Taxonomy terms can’t find posts from archive.php or taxonomy.php
- taxonomy terms are not populating select tag options
- What is the earliest hook you can piggyback on to check terms/taxonomy for a collection view?
- Related Posts by taxonomy using Advance Custom Fields Plugin
- How to filter the terms to a special custom taxonomy?
- 2 Different Taxonomy with Hierarchical Term Relationship
- Non-Recursive get_term_children()
- Display yesterday’s last post’s Custom field from Unique terms of one Taxonomies
- Echo used hierarchical taxonomies parent name
- Passing values by form to create a query
- Order taxonomy terms by the frequency of use in the last 30 days
- Why can’t I exclude a term ID from either get_terms or WP_Term_query?
- Can One Taxonomies Terms be Ordered by A Seperate Taxonomy?
- How to display a term of taxonomy
- Unable to do WP_User_Query with meta filter
- get_terms of specific parent (including the parent itself)