Monday, 9 March 2020

How to create custom post type in WordPress without using Plugins

Function to be written on function.php for custom post type
--------------------------------------------------------------------------------

function gallery_post_type() {
$labels = array(
'name'                => _x( 'gallery', 'Post Type General Name', 'acsweb' ),
'singular_name'       => _x( 'gallery', 'Post Type Singular Name', 'acsweb' ),
'menu_name'           => __( 'gallery', 'acsweb' ),
'parent_item_colon'   => __( 'Parent gallery', 'acsweb' ),
'all_items'           => __( 'All gallery', 'acsweb' ),
'view_item'           => __( 'View gallery', 'acsweb' ),
'add_new_item'        => __( 'Add New gallery', 'acsweb' ),
'add_new'             => __( 'Add New', 'acsweb' ),
'edit_item'           => __( 'Edit gallery', 'acsweb' ),
'update_item'         => __( 'Update gallery', 'acsweb' ),
'search_items'        => __( 'Search gallery', 'acsweb' ),
'not_found'           => __( 'Not Found', 'acsweb' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'acsweb' ),
);
$args = array(
'label'               => __( 'gallery', 'acsweb' ),
'description'         => __( 'gallery news and reviews', 'acsweb' ),
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', ),
'taxonomies'          => array( 'genres' ),
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,

'menu_position'       => 5,
'menu_icon'           => 'dashicons-format-image',
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'capability_type'     => 'page',
'taxonomies'          => array( 'category' ),
);
register_post_type( 'gallery', $args );
}
add_action( 'init', 'gallery_post_type', 0 );







Fetching custom post type in template pages
------------------------------------------------------------

 <?php
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'service',
'paged' => $paged,
'orderby' =>'ID',
'order' =>'asc',
'posts_per_page' => 9)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
                <div class="row align-items-center" style="padding-bottom:100px;">
                   
                    <div class="col-lg-4 col-md-12">
                        <div class="about-image">                         
                            <img src="<?php echo the_post_thumbnail_url(); ?>" alt="Image Title">
                        </div>
                    </div>

                    <div class="col-lg-8 col-md-12">
                        <div class="about-area-content">
                            <span></span>
                            <h3><?php echo get_the_title(); ?></h3>
                            <?php echo get_the_content(); ?>
                 
                           
               </div>
                <?php endwhile; ?>
               



6 comments:

  1. thanks, very clear and easy to implement.

    ReplyDelete
  2. I really appreciate your effort. I am learning alot of things from you. keep it up. I learn and implement on my site Amal Info TV. Please check and let me know if any problem.

    ReplyDelete