Blog

How Display Custom Post Type in WordPress Divi Theme

Divi theme is a good theme for WordPress developers with some design sense. Using pre-build modules we are capable of creating any type of wordpress websites with different type of sections and designs. In this post we are discussing how to use Custom Post Type in Divi theme.

But the issues is that this theme don’t have option to display content from post type. We are using post type of create special set of content thats is not related to any other pages or posts.  So to display a content from post type we need write its function to display the post type in WordPress templates. In Divi theme we can not code php inside the text modules.

So to do this we have two method one is create a sidebar and add that sidebar to the section we need display the section. And inside that side bar we can use PHP Widget plugin. Using this plugin you can write php code inside the side bar.

The second method is create a short code in your theme function.php and use it. To prevent the issues of file rewiring while we update divi we have to use Child Theme concept. Or you need make the function inside a plugin and use that to create the short code. Here i give you a example for such a function.

function recent_appartment_function() {
  global $post;

    $html = "";

    $my_query = new WP_Query( array(
         'post_type' => 'appartment',
         'posts_per_page' => 3
    ));

    if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

        $html .= "<div class='appartment_entry'>";
        $html .= "<div class='appapertment_price'>" .get_field('apartments_price') . "</div>";
        $html .= "<div class='appapertment_image'>" .get_the_post_thumbnail() . "</div>";
        $html .= "<h2 class='appartment_title'>" . get_the_title() . " </h2>";
        $html .= "<div class='appartment_features'>" . get_field('apartments_features') . " </div>";
        $html .= "</div>";
       endwhile;
     wp_reset_postdata();
 endif;

    return $html;
}

function register_shortcodes(){
   add_shortcode('ApartmentList', 'recent_appartment_function');
}
add_action( 'init', 'register_shortcodes');

So we have create a function to display post type Apartment.

Then we can use the short code ApartmentList to display the function you can use this short code inside a Divi text module.

Leave a Reply


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