Welcome to Day 2 of 3 in our series on How to Add Custom Styles to Divi’s Blog Post Formats. By default Divi comes with six blog post formats: Standard, Video, Audio, Quote, Gallery, and Link. In this series we teach you how to customize them with php and css.

Yesterday, I walked you through how to use Desktopserver to create a local install of WordPress. I also showed you how to set up a child theme. Both of these are a great foundation for any project you are working on and it is perfect for what we are going to be doing today and tomorrow.

Today we are going to be editing some PHP code inside Divi’s single.php file in order to get a completely unique layout for your blog post formats. If you aren’t already familiar with PHP, no worries. It’s not a lot and it’s not hard to do. I will walk you through exactly what code you need and where to put it.

After we have our single.php file edited, we will finally be ready for our last part of our series. Tomorrow is all about styling and finding out just how cool blog post formats can look on your blog.

Let’s get started.

What You Need For This Tutorial

Here is what you will need to follow along with today’s tutorial:

  • A code editor such as Atom, Sublime, Brackets or Notepad + + (whatever you prefer)
  • A Local WordPress Installation (latest version) using DesktopServer (unless you already have one)
  • A Divi Child Theme installed and active with three files added from yesterday’s post (style.css, functions.php, and single.php)

Customizing Divi’s Single.php Template

Adding a New Hero Section

Using your text editor, open your child theme’s single.php file. Make sure that you copied over all of the single.php code from the parent theme (Divi) with your file.

At the top of the file, locate the opening div with the id “main-content”.


Then enter the following just below the “main-content” opening div:


Here is what this code is doing to your single.php file.

First, a variable called “$featured_image_src” is assigned a value that “gets” the image source (which is the url to the image) for the featured image assigned to the post.

The next code is a combination of html and php that opens the new div with a custom class called “hero-section” and some inline styling to create two background images stacked on top of each other.

The first image is defined as a linear-gradient which serves as a dark overlay for the featured image.

  Linear-gradient (rgba(0,0,0,0.3), rgba(,0,0,0,0.3))  

And the second image (the featured image) is separated by a comma and, by default, stacks behind the first image.


Notice variable defined earlier is used to set the featured image as the background of the “hero section”.

Because the second image listed automatically stacks behind the first image, this combination creates the dark overlay over the hero section image.

Now, whenever you add a featured image to your post, a dark overlay will be automatically applied.

The next snippet of code adds the headline for your post inside your new hero-section div.


The final snippet of code adds the post meta content below the headline and within the hero-section div.


Here is what this new section and styling will allow us to accomplish in tomorrow’s post:

Taking Out What You Don’t Need

Since you have a new section for your featured image, post title, and post meta in place, you need to take out three sections of code that were being used to display your featured image, post title, and post meta by default.

You will need to take out the h1 tag that displays the post title:


You will need to take out the code that displays the post meta:


And you will need to take out the code and displays the featured image thumbnail:

  print_thumbnail( $thumb, //$thumbnail["use_timthumb"], $titletext, //$width, $height );  

Here is the entire code for the new single.php file. Feel free to copy and paste it if you get stuck.

Note: this is a scrolling box of code. You’ll need to select and drag to get it all.


'' && et_get_option('divi_integrate_singletop_enable') == 'on') echo(et_get_option('divi_integration_single_top')); ?>
', $first_video ); } else if ( ! in_array( $post_format, array( 'gallery', 'link', 'quote' ) ) && 'on' === et_get_option( 'divi_thumbnails', 'on' ) && '' !== $thumb ) { } else if ( 'gallery' === $post_format ) { et_pb_gallery_images(); } ?> %3$s
', esc_attr( $text_color_class ), $inline_style, et_pb_get_audio_player() ); break; case 'quote' : printf( '
', et_get_blockquote_in_content(), esc_attr( $text_color_class ), $inline_style ); break; case 'link' : printf( ' ', esc_url( et_get_link_url() ), esc_html( et_get_link_url() ), esc_attr( $text_color_class ), $inline_style ); break; } endif; ?>
'' ) ); ?>

That’s it for today!

Now the hardwork is all done and the framework is ready for us to add our styling to our post formats. Tomorrow I will be setting up and styling all 6 blog post formats using the single.php template we just created.

Please post your comments below and I will try my best to address any problems you may have. If possible, please make sure you have a fresh install of the latest version of WordPress and Divi to avoid any potential conflicts.

Until tomorrow!

The post How to Add Custom Templates and Design to Divi’s Blog Post Formats (Part 2 of 3) appeared first on Elegant Themes Blog.

Powered by WPeMatico

Submit a Comment

Your email address will not be published. Required fields are marked *