get_the_title()
returns the post (product) title including all HTML, whitespaces and symbols not allowed in URLs, etc. Use post slug instead.
Also, you don’t need to use any actions.
function custom_hero_image( $post_id ) {
$product = get_post( $post_id );
$slug = $product->post_name;
if ( is_product( $post_id ) ) {
$html="<img class="jarallax-img" src="". get_template_directory_uri() .'/inc/assets/img/shop/hero/'.$slug.'.jpg">';
} else {
$html="<p>No results found.</p>";
}
return $html;
}
And call the function within a template file:
<div id="hero">
<?php
// get the post ID outside the Loop
$post_id = get_queried_object_id();
// print the <img>
echo custom_hero_image( $post_id );
?>
</div>
Related Posts:
- Woocommerce add to simple product attribute programmatically [closed]
- Creating loop within functions.php
- Set quantity of woocommerce product on page visit [closed]
- How can I remove the main description text box in a Woocommerce product editor page? I only use the “Product short description” text box [closed]
- When sale price is 0.00 show only regular price
- Parent category as WOOCommerce Categories widget title
- Woocommerce custom calculation function
- Copy and Modified WooCommerce function is_coupon_valid [closed]
- Add multiple custom fields to the general settings page
- How to display custom field in woocommerce orders in admin panel?
- Is it possible to remove the “standard” post format?
- Issue with get_theme_mod returning a blank value instead of the saved value
- How to remove row-actions from pages table?
- Modify search function in WordPress (TwentyTwelve)
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- How to fix a theme with page.php Default Template that accidentally deleted?
- How to Acheive the custom woocommerce category template
- How do I change parameters without changing the core
- Problem with extract() with custom shortcode
- Remove product description from product archive page
- Hide some items from Screen options in dashboard for products
- add sidebar area to header of child theme
- Display WooCommerce subscriptions for user [closed]
- Increment price for Woocommerce Minicart [closed]
- Change woocommerce products weight varation based its product category or tags? [closed]
- adding custom classes for tables
- Pass Shortcode Attribute to footer Script
- Woocommerce checkout field maxlength, make input number field only (postcode)
- PHP files included in functions.php don’t seem to work
- change in form-checkout.php by using code in functions.php
- How Can I Expand the WordPress Customize window without any Plugin?
- wp_dropdown_categories and custom attribute
- Automatically wrap multiple images in div
- Ajax not working properly
- Include default functions and methods
- How can I modify all existing tags while keeping the urls themselves?
- Remove the product category module block from the Woocommerce product page
- Unable to login using username
- different style sheet for just one page template
- Functions when woocommerce isn’t installed
- Condition OR for current user ID
- Where do I store custom functions that call custom page template files?
- Wp-query Order By problem
- replace text in Sort Box
- login_headertitle is deprecated since version 5.2.0
- add referrer to woo
- Add attribute only to first image of every post via functions.php
- Different favicon on different pages
- The_content and Preg_replace in loaded Iframe [closed]
- Auto scroll to id on page load on all pages except home page. Only do this on mobile
- Adding product to WooCommerce cart [closed]
- two search forms on the same page
- WooCommerce -> wc_enqueue_js not working [closed]
- Use existing image sizes for WooCommerce
- Insert a field with PREG_REPLACE – strange behaviour
- How to echo Widget Title in Custom Frontend-Template Box
- how to display a page before the home page
- loginout function customization
- Function to check if custom page is used by page?
- implementing a centralized content “show-do-not-show” toggle?
- How would I make a function to dynamically determine if user is logged to change navigation menu text?
- How to display this meta data (an array) in form of a function (created with a custom write panel)?
- WordPress – using sessions?
- Customise Grouped Product display in Woocommerce with custom column
- Woocommerce Variable Product Dropdown for Custom Shop Template
- How to hide content on mobile in the functions.php file – woocommerce
- How to remove font awesome from loading in wp theme
- Header menu aligned right on all pages except for single-post page [closed]
- Hide ‘add to cart’ when certain value in dropdown is selected
- Adding discount functionality to the cart
- Remove text after a dot and a colon in Woocommerce product title
- Woocommerce product price change
- Help with with my function for wordpress
- Remove clickable Link of WordPress Site Logo from Woocommerce Single Product page
- Problems adding a new field to product in cart
- Extend WordPress Audio Widget to accept HLS (.m3u8) URLs
- Shortcode to insert default text and change one word throughout it?
- How Can I Concatenate A String With One Of My Custom Field Value Before Saving The Post?
- Variable function names
- Woocommerce – Switching Price for Category
- add product thumbnail to checkout page only and include variation name
- WordPress Ajaxifying not working properly
- WordPress function with attribute pass to template
- Move product attributes after summary on single product page
- Will dequeueing in child theme functions.php file prevent Google Fonts from loading?
- Which method is more correct for removing WooCommerce Extensions menu item?
- Adding A New Widget to WordPress Disables the Existing Widgets
- Help! Need a different logo on my main page from my other pages
- Display page number on custom page title function
- How to replace words in my posts and saved new words
- How do I integrate my Child Theme into a Custom Template?
- Shortcode to eliminate and replace with
- Insert menu into theme location depending on user logged in/out status
- Add functionality to post editor
- Why does modifying my functions.php file always result in a server error
- Remove snippets of JS from core
- reusing code in function and running it with loop
- Explode Array from Repeatable Custom Field
- WordPress Custom wp mail template return full template
- post_row_actions filter from parent theme not executing in child theme