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?
- Changing a slider to a grid [closed]
- Custom Navigation build using wp_nav_menu and walker
- What can I use for small text when converting to WordPress?
- Using My Own Classes On Wp Unit Tests
- Echo all category names, apart from one
- Is it possible to visually group items on the theme customizer?
- Custom Blocks as part of a theme
- Customize how a WordPress theme looks like in the Theme Selector
- WooCommerce: multiple input field for multiple product variations
- Customizer – loading settings/controls/sections/panels based on a id/page id
- Where to place template files?
- Integrate WooCommerce theme with a WordPress theme [closed]
- how to get the post attachement image in full size?
- Widget Option is Missing
- 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?
- How do I get the trackback count of a post in wordpress without writing an SQL query?
- Extend WordPress Core Classes in OOP Theme?
- Load sidebar template just once to prevent multiple animations
- Default Text not showing from customizer
- Child Theme header1.php file not overwriting parent theme’s header1.php file
- Get the amount of posts on a given page
- Replace single_template filter with what for default posts?
- How to have content scroll over background
- Need help with adding templates (archives and sitemap) to WordPress child theme
- WordPress Template Part in iFrame
- Template for landing pages
- How to list articles by year based on url?
- Replace site title with logo when logo is uploaded in customizer using
- Unique design inside a specific category’s url
- How to transfer one domain to another domain without losing theme options (customizer)?
- How to seperate static phrases from template files in custom theme?
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- Change image size depending on page
- Broken theme, template is missing
- Is there a WordPress boolean for “theme_customizer_active()”?
- How to add menù section to my WordPress template?
- How can I prevent posts with no title showing up in my query?
- How can I use the WordPress Loop and Pagination in multiple instances but different scenarios throughout my site?
- How to make theme configurable
- Extending Twenty Eleven Theme
- Post and page content not displaying in search results
- Why still output /wp-content/themes/twentynineteen?
- How to clean up the theme for production?
- Previous and Next links not populating, have I missed something?
- Templates without a loop, best practice?
- searchform.php doesn’t work properly
- CSS added through customizer neglects the need of a child theme?
- Twenty Seventeen Pages Loop
- Assign a template to a custom post type when displayed by the main loop on the home page
- Customizer: get_preview_url() inside customize_save_after hook
- Custom Post type content within a slider
- Password protect a custom template
- add shortcode support in customizer
- Clearing dummy data before launch
- Templates dropdown not appearing using _s theme (underscores)
- Shortcode call not working in WP 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
- How does the loop know which post to view?
- Woocommerce Product attribute not imported with wordpress Importer [closed]
- New to WordPress – Read the Codex, Other Docs; Still Confused
- How to add custom page elements to the WYSIWYG editor?
- Using the media library for theme customization
- Confused by the behaviour of “front page” / “home” templates
- I need to develop a one-page design
- add jquery file if a certain page is included
- Retrieve WordPress’ the_content() with jQuery
- Exclude subcategory from wp_query
- Style first 3 posts differently with WP_Query [duplicate]
- How to create a WP theme that use BootStrap? [closed]
- Alternatives to handle customizer settings
- WP_Query not getting all posts, just tagged posts
- Update Specific Key Value in Complex `wp_options` object
- pass html code to worpdress
- Can’t change theme name
- Remove/Hide a Widget on a Custom Template
- How to link to the page displayed by home.php?
- first excerpt fine, subsequent post excerpts shift to the right instead of displaying vertically [closed]
- Pagination and multiple loops
- Under theme folder, what’s sequence of action for index.php, page.php, single.php?
- My contact form – I’ve changed the source code but the changes are not being applied
- How to custom category template based on category?
- How can I sort my loop based on meta data, using a form?
- Is there a template tag I can use to link to the archive page corresponding to the month that a post was published on?
- How to use multiple check-box values to work in a function and insert values in database
- List categories of a post hierarchically?
- How to add testimonials with an image?