if it is the “Featured Image” of your post, you can use:
<?php
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) { the_post_thumbnail(); }
?>
check: the_post_thumbnail function
else if you want to get the first image inside your post, you can use somthing like that:
function get_first_post_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
//Defines a default image
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
}
Then place <?php echo get_first_post_image(); ?>
Related Posts:
- Change loop order via form or link (jquery, not URL)
- Setting hero/splash section as a blog post
- What is the best way to handle multiple calls to get_template_directory_uri() and similar functions?
- Displaying recent post excerpts on static front page
- Move default page templates to sub directory
- How to add custom meta box when you have a custom page template file
- Create theme for mobile phones and tablets only?
- Not Found when using activity stream as front page with BuddyPress
- Best practice to create required pages
- How customizable is a self-hosted WordPress blog compared to a Blogger blog?
- Meaning of “if ( is_home() && ! is_front_page() )” snippet?
- How to insert a logo in the header?
- Get ‘page’ number with infinite scroll
- Page template in two level deep folder
- Why doesn’t /2013/01/ properly return January’s archives in archive.php?
- Best practices – Should I create a child theme vs. customizing a framework theme? (such as HTML5 reset)
- How to disable 3.1 “Admin Bar” via script for the admin user?
- Problems with Layout inherit in theme.json
- How the WordPress sidebar works
- Should we localize custom-made themes / plugins?
- Extend walker – navigation, adding data attribute to a tag
- How can I remove the Static Front Page option from the Customizer
- Is there any need to use both wp_reset_postdata and wp_reset_query together?
- query posts in wordpress
- How can i customize the comment list
- the_content() in single-{post-type}.php problem
- How to handle theme customization and sass variables
- wp_enqueue_scripts not called on search page?
- wp_head() not inserting the default stylesheet style.css
- First completely customized theme, where should I start?
- Theme Customizer not loading
- Best approach to create sites with Modular Content? [closed]
- Finding Page Template and Displaying Content
- How do I update WooCommerce template files in my theme? [closed]
- What’s the best action to use when you want to do something only once per theme setup?
- Customizer API and add_panel(). Panel doesn’t show
- How to correctly add JQuery in a WP theme?
- Why is the first query affecting the second query, even after wp_reset_query() and wp_reset_postdata(), but not on the second page?
- Check if password protected post is visible
- Creating multiple hooks for theme
- Add theme templates for child categories into the template hierarchy
- Where can I access my custom page template?
- How do I create a custom partial / template?
- Change the template of multiple multisite pages at the same time
- How to reorganize the items returned by wp_list_comments()?
- Load child template based on parent
- How do I Add images uploaded in the post to a default custom field
- How to add a button which saves the post then executes a function
- Limitations when modifying wp_title with a filter
- How to edit theme functions file to modify pagination?
- Sort wp_query by sum of two meta fields
- Q: How to pull data from custom table to populate zustomizer setting/control select options
- Using My Own Classes On Wp Unit Tests
- Integrate WooCommerce theme with a WordPress theme [closed]
- How do you use WordPress for a website that’s not in a blog format?
- Is there any tool to find lines of codes responsible to generate front-end HTML elements?
- Extend WordPress Core Classes in OOP Theme?
- Replace site title with logo when logo is uploaded in customizer using
- searchform.php doesn’t work properly
- Customizer: get_preview_url() inside customize_save_after hook
- Custom Post type content within a slider
- Password protect a custom template
- How to call a widget in a loop at a specific iteration value?
- Load different single.php templates according to post layout selection
- Right procedure when you develop a WP Website for a client
- New to WordPress – Read the Codex, Other Docs; Still Confused
- How to add custom page elements to the WYSIWYG editor?
- Confused by the behaviour of “front page” / “home” templates
- Style first 3 posts differently with WP_Query [duplicate]
- How to link to the page displayed by home.php?
- Pagination and multiple loops
- My contact form – I’ve changed the source code but the changes are not being applied
- How to custom category template based on category?
- get_the_terms() returning wrong results inside of loop
- Show only pages you are author of
- White screen of death on index.php page 3 and above?
- WordPress theme files Organization
- Infinite loop when nesting have_posts()
- my ajax wont sent the data, please correct my code
- Different templates for each category level
- Problem with Displaying Custom Theme Page’s Content
- What is the best practice workflow for new website overhaul and keeping current SEO ranking? [closed]
- Child theme in separate WordPress Install
- Add a customize control depending wich radio setting is checked
- How to show children pages as array
- How to share posts (and plugins) between existing site and new, separate dev/test installation?
- How to prevent XSS attack in wordpress theme?
- Conditional loop based on current page
- WordPress 3.5 + Foundation 5 not working on IE9
- Tags interfering with next_post_link();
- Simple loop with shortcode rendering problem
- How get the 10 most viewed pages (not post)
- Theme Option’s Save Button is not working
- Checking conditionals
- have_posts() return false on single post
- How to defeat “Blog pages show at most __ posts” setting in the loop?
- Permalinks problem with custom theme
- How to split the site’s layout without damaging this layout? [closed]
- Multiple content areas with Gutenberg – Transparent areas mid content
- What is the point of using archive.php instead of index.php?