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
- How to influence the information displayed on widget inside wp-admin
- Is there a hook or function I can use to display all theme files being used on a current page?
- Add action hook conditionally – only when home.php in use
- How to add classes to images based on their categories?
- current_cat_ancestor Alternatives
- Implementing DNS Prefetching with WordPress
- Help me to understand wp_header() and wp_footer() functions
- In WooCommerce I need to modify the thankyou.php page [closed]
- How can I add an extra WooCommerce hook
- Add body class of category parent
- Why get_header() or get_footer() does not run twice if called in the same php file?
- Remove Page Title from Static Frontpage
- How to override WordPress registration and insert an auto-generated username?
- Set default options for inserting media
- Change parent theme file function in child themes functions.php
- How to hook into the quick edit action?
- gform_after_submission content appears immediately after , not in post body [closed]
- How to auto-update child posts whenever the parent post is updated?
- How to set child post categories to parent post categories when updating parent post?
- WooCommerce add_action hook results in 500 error
- How to run a function once on every post in database?
- Make Categories a Dropdown Menu in Media Library
- how to change link of some wordpress pages
- Gravity Forms field entries into wp_query loop [closed]
- Post thumbnail relative link and HTML modify
- Proper way of using functions in action hook?
- Programmatically Create Category and sub Category
- Attach parent category template to all subcategories
- Accepted arguments value in hook functions
- Get current page_id before loop, in functions.php
- exclude a category from a search on a specific page
- How to properly add function called by action-hooked function to functions.php?
- Which action hook to use for function?
- Change size and crop medium_large images
- Custom HTML in specific category single page and its descendant categories
- Execute function after a post has been published
- Inserting a functions output after the content
- Modify a function without editing template
- Don’t delete a page if it holds users
- Problem only while using require_once() within functions.php
- Passing values from a widget to a function within a plugin
- Publish/Update post is changing image links from file url to post url
- Ordering posts by publish date not working?
- Replace a menu with widget or a custom template file programmatically
- Footer disappears when using the_content ( )
- Categories as main menu items and subcategories as nested lists
- Get User Login Data (date, time… )
- Registration Hooks don’t appear to be working
- Need help “sanitizing” a custom function that pulls category slug into body class
- Why does re-using this function not work? [closed]
- 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
- How to fix get_the_category function returning incorrect slug?
- How to edit the Tags within the image file URLs?
- (Woocommerce) Order by price when entering specific category
- Auto log in hook is requiring a page refresh
- Possible to hook into Media Library preview File column and use a custom image?
- How to rewrite wordpress search to work on specific category
- Can’t add_action to ‘save_post’ and get it to fire
- Exclude Category ID in function
- WordPress wraps span tags into p tags
- 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
- 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
- Perform function on publish AND save (not just save)
- Changing the text of Upload/Insert on Posts and Pages Screen
- 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
- Display link to category over featured image
- Does hook have an effect on increasing the page load?
- What is the earliest Hook a Script can use?
- Populate acf-field, depending on block name
- How Do I Unhook This Parent Theme Function?
- Remove the ‘category’ url for one category type
- Adding new Category does not refresh the backoffice
- How do i tweak my wp Post title base on category of the post
- Get Category in save_post Hook
- Calling hooks in functions
- 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
- True email confirmation for registration (keeping unvalidated users from user table)
- require_once() Causing categories and tag pages on dashboard to not refrsh
- How To Get WordPress Categories Last Update Date?
- get categories the post was in and just been removed from
- Init hook for header send
- How to get tags and categories?
- does acf_save_post cause endless loop?
- How to Insert A List of Posts in A Category Written by the Author into the Author Archive
- How can I append and prepend something to all post hyperlinks without using ::before or ::after? PHP hook solution?
- How do I add functionality to images?
- Create section wit form