Your code calls get_the_category()
but it doesn’t use the value returned.
// ...
if ( isset( $_POST['img-destacada'] ) ) {
$categories = get_the_category($post_id);
$img = '';
// $categories should contain either an array of WP_Term objects,
// or an empty array.
if ( ! empty( $categories ) ) {
// Here I've used the slug of the first item in the array to set the $img.
// You may have other logic you want to use.
$img = $categories[0]->slug . '.jpg';
}
if ( ! empty( $img ) ) {
// Checks to make sure there's an $img set.
update_post_meta($post_id, "img-destacada", $img);
}
}
// ...
References
Related Posts:
- Remove a category from a post when saving a new post
- Issues with title-tag and document_title_parts
- Display random categories on the front page (Finding and Editing Theme Functions)
- Import WordPress XML File from Within Functions.php
- Define custom Page Template without its own .php file
- Does hooking into the same action multiple times drain memory?
- How to influence the information displayed on widget inside wp-admin
- Extract image from content and set it as the featured image
- Is there a hook or function I can use to display all theme files being used on a current page?
- Why does re-using this function not work? [closed]
- Pass parameter to hooked function using custom page template
- Override the WordPress core function wp_referer_field
- How to return hook data when multiple parameters are present?
- Hook into a function without a hook?
- How to obtain the recent posts without their content in an efficient way?
- What is the text that appears beside the page titles in the list of pages in the WP admin?
- Setting a default text for excerpts of a particular category
- Running script based on Category
- How to fix get_the_category function returning incorrect slug?
- How to edit the Tags within the image file URLs?
- Is my code correct to enqueue CSS on a specific page?
- Return category name with & Ampersand doesnt work
- Can’t properly set the_title add_filter to show short_URL
- Control the srcset much more (not all sizes in it each time)
- How to add_filter html template to middle of content
- Filter yoast canonical add_action priority [closed]
- (Woocommerce) Order by price when entering specific category
- How do I hook into the container of wp_nav_menu?
- Remove links from the_content when using filters wp_trim_excerpt
- Is there a way to prevent a function/method from being called outside a specific hook?
- Auto log in hook is requiring a page refresh
- Possible to hook into Media Library preview File column and use a custom image?
- Function not working inside of a function in functions.php
- Access category within rss2_head hook?
- issue with if/elseif in_array inside foreach loop display only one post
- Randomizing wp_tag_cloud() in child theme
- Adding code before post title with the_title produces weird results
- Add back in child theme what the parent theme removed with remove_action
- add_filter to specific WooCommerce Category
- How to rewrite wordpress search to work on specific category
- Writing a function to detect an event
- Image loading function not working on archive.php template
- Can’t add_action to ‘save_post’ and get it to fire
- Exclude Category ID in function
- Problem with calling custom function in a foreach loop
- This code works, but the way I integrated it is breaking the media uploader. How can I integrate it properly?
- Specify multiple categories for custom post template – FATAL ERROR
- WordPress wraps span tags into p tags
- “All posts” in the category widget
- Changing where my author box is printed
- Is it possible to use add_filter in an included file in the child theme’s functions.php?
- Insert Content Before div#main from the functions.php File
- Add Adsense code between job listings – wp job manager plugin
- Get Attachment Category Name
- update_post_meta is Updating with two page id
- Replace admin header logo with an image
- Why I can’t load my JavaScripts using an hook inside my functions.php file?
- is_home() not working in functions.php file
- How to conditionally add a widget via a hook in functions.php
- Perform function on publish AND save (not just save)
- How to place a div inside a function that creates a div
- Changing the text of Upload/Insert on Posts and Pages Screen
- WordPress Categories: Function using custom SQL to return array of specific category IDs
- Filter for replacing the WP_query object for a given category
- How to add or delete default value theme options while activate and deactivate theme
- Display ACF category image on archive and single template files
- Get slug of current category in functions.php
- WordPress comment processing . Default unapproved comments detection before posting
- Warning at top of website & top menu gone
- Display a list of random terms from custom taxonomy with shortcode
- Change category display name function
- Display link to category over featured image
- Does hook have an effect on increasing the page load?
- Save_post – Warning: Cannot modify header information
- What is the earliest Hook a Script can use?
- Populate acf-field, depending on block name
- How to use WP conditional tag in a functions.php with OCEANWP Theme?
- custom error message for empty username and password using authenticate filter not working
- How Do I Unhook This Parent Theme Function?
- Remove the ‘category’ url for one category type
- Custom Function.PHP Code Not Working on One Site
- Adding new Category does not refresh the backoffice
- How do i tweak my wp Post title base on category of the post
- Get post_author email for Zapier Integration
- Get Category in save_post Hook
- Calling hooks in functions
- how to remove a tag in the_category function
- Menu and category highlighting for a single post
- i can’t use wordpress functions in ajax loaded php file
- save_post function keeps triggering: Warning: array_map(): Argument #2 should be an array… when restoring from trash
- How to add custom column into custom post, when field is select
- Graphic before title – Specific Category
- How can I stop the hook from breaking the login/logout function?
- save_post doesn’t correctly process function call with php class
- True email confirmation for registration (keeping unvalidated users from user table)
- WordPress hooks to call a function inside a construct
- require_once() Causing categories and tag pages on dashboard to not refrsh
- WordPress undefined function error on using add_action() in functions.php
- different body classes for each category
- How to echo the translated custom field?