According to the get_post_meta() function reference in the WordPress Codex, “If there is nothing to return the function will return an empty array unless $single has been set to true, in which case an empty string is returned.”
That means we can use ternary operators to check whether get_post_meta()
returns an empty string, and if so then we assign the static image instead. We’ll only need to modify the line where the $img
variable is defined:
$img =
( '' == get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )
?
get_template_directory_uri() . '/images/default-img.png' // updated example per comment
:
wp_get_attachment_image( get_post_meta( get_the_ID(), '_articles_image_id', 1 ), 'full', 1, get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )
;
Related Posts:
- Display current category title on category page
- How to get the custom header image’s alt text?
- PHP unit testing WordPress?
- WordPress GET parameters causing 404 Not Found
- is_page() not working from within a plugin
- How to create custom php script page
- Metabox repeating fields – radio buttons not saving correctly
- Create WordPress pages with PHP
- How do i hide the if the appears
- How to return the_excerpt (without echo)?
- How to handle a custom form in wordpress to submit to another page?
- Is it possible to generate a page without create in the admin?
- Replacing mysql_real_escape_string in WordPress theme
- TCPDF get_post_meta outside the loop
- How to redirect all 404 in a WordPress subdirectory to the index.php of subdirectory?
- Database “Migration” for Plugins?
- WooCommerce Link to Product Category
- Easiest way to show total number of subpages
- using woocommerce_template_single_add_to_cart in shop-loop – javascript issues [closed]
- ACF check if variable has http, if else return string
- Can’t load WP function into external function
- Woocommerce – Filter Variable Products by Variation Attributes and Variations Regular Price
- Loading header.php on specific page
- Adding a div at the bottom of a sidebar
- How can I set a default category for a custom post type in wordpress?
- Slider loading issue
- Sort posts by custom fields value using dropdown menu
- Cause of Blank Lines Being Added to WP FIles?
- how do I get a specific post from a post with a subcategory in WP
- PHP Fatal error: Out of memory in WordPress [closed]
- Theme developement – incative sidebars
- add_cap not working
- Weird 404 URL Problem – domain name being placed at end of urls
- Styling my own password protected page, how to deal with wrong password?
- Help solving t_echo php error while using echo do_shortcode
- WordPress Theme Modification Help
- Hook called before text widget save
- How to list commenters and days since last commented
- PHP Warning: strip_tags() expects parameter 1 to be string?
- wpdb prepare insert table doesn’t work
- How to modify the default feed via a function
- Automatically add images to a menu
- Custom Post type slider with thumbnail navigation
- Where can I find the inline-css of my theme header?
- Filter by field with array value in ACF on WP REST API
- how to add re-order to wp user panel of wordpress?
- How to add new CSS file to new PHP file
- custom plugin with upload files does not work
- WordPress SQL JOIN query
- Aspx/Rss feed – failed to open stream: Redirection limit reached
- Adding an Unlinked Space in a Custom Function
- WP_Query and help with the loop for magazine front page
- How do I call an external php non WordPress class into functions.php?
- Why does PHP called with Jquery AJAX not allow additional php code to be added?
- Infinite Loop in WordPress Installation
- send popup after wp_redirect()
- Validate search against a list of codes in wordpress
- Generating 10 000 WordPress posts using PHP and avoiding Server Timeout error
- A function to get the content of a page
- If Post Published Date or Modified Date is 1 Year or Older, Display Notice on Post Page
- Using the get_template_part method within an mu-plugin not possible?
- Exclude ipads and tablets form wp_is_mobile code
- Woocommerce Product page add on checkbox and price
- Where Do Image Dimensions Get Added To A Filename?
- How to set Nav as a default menu in wordpress
- WordPress How do I pass a variable from one add_action to another?
- Add my footer menu to the header
- WordPress child theme, creating a custom php template page
- Child and Parent Pages list of sub pages
- How to block access to a folder inside of wp-content for non logged in users?
- How to use get_template_part always relatively to the file I’m using it in?
- Include search tags and users in my search results system
- Complex PHP for json_encode > how to handle/output right?
- Show content by using tags
- Return only certain (html)-elements of the_content()
- Only Get A Certain Number of Posts From WP_Query
- Need to adjust condition to say if I’m pulling categories from a post, to pull only the first one
- Showing featured image of page’s child’s children page
- Even with PHP plugin get_posts not working in widget area
- How to call post title and post summary to other part of site?
- how to run a php code in widget?
- Permission error on plugin save
- Restricting Post Content Visibility with Custom Code – where to put it?
- Highlight main menu when on specific posts or pages
- Adding Gravity Form With if(is_page) Is Not Working
- Implement “No related posts for this entry” into the loop?
- wc_add_notice not working on cart page
- How to convert my comment.html to comment.php
- Twig+WordPress how to use array arguments inside a function?
- I would like to alert message while another user is editing or update data in wordpress admin panel, what should i do?
- It’s not showing full content for posts pages in wordpress “[…]” while customing some changes in the style.css file [closed]
- How do I convert a custom field to a php date format? [closed]
- WordPress online courses system
- How to align buttons properly [closed]
- index.php file keeps redirecting to a non-existent index.html file?
- How to pass parameter to another url?
- HTTP 500 Error after Creating Shortcode
- Displaying generated HTML within a WordPress site
- WordPress PHP8 Critical Error in class-wp-widget.php
- How to get the last date updated of postmeta?