WordPress how to develop custom post type

Hi, In this WordPress custom post loop with arguments we are going to learn how to declare a custom post with argument  and use the custom post in frontend as a slider. Now start to make a custom post for slider step by step

Step 1. Open function.php file of your theme folder and write the array of label

//Declare Custom Post Type Slider:
$labels = array(
'name'               => ' Sliders',
'singular_name'      => ' Slider ',
'menu_name'          => ' Sliders',
'name_admin_bar'     => ' Slider ',
'add_new'            => 'Add Slider ',
'add_new_item'       => 'Add New Slider ',
'new_item'           => 'New Slider ',
'edit_item'          => 'Edit Slider ',
'view_item'          => 'View Slider ',
'all_items'          => 'All Sliders ',
'search_items'       => 'Search Sliders',
'parent_item_colon'  => 'Parent Sliders',
'not_found'          => 'No Sliders found.',
'not_found_in_trash' => 'No Sliders  found in Trash.'
);

We have declared all the label to be shown on admin panel

Step 2. Now make an array of argument with the labels

$args = array(
'labels'             => $labels,
'description'        => __( 'Description.', 'your-plugin-textdomain' ),
'public'             => true,
'publicly_queryable' => true,
'show_ui'            => true,
'show_in_menu'       => true,
'query_var'          => true,
'rewrite'            => array( 'slug' => 'sliders' ),
'capability_type'    => 'post',
'has_archive'        => true,
'hierarchical'       => false,
'menu_position'      => null,
'supports'           => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' )
);

Here the supports portion is to give permission of access and reright and slug is for url peramiter to show the single slider.

Step 3. Now register the post with the argument

register_post_type( 'custom_slider', $args );

here the custom_slider is the id of the slider by which we will get the slider from frontend and also make custom single post

 

Step 4. Now init a function and put the abve code inside the function.

Function 'custom_slider_init(){

//the above code inside the function

}

add_action( 'init', 'custom_slider_init' );

 

 

Here is the full code for declare a custom post for slider

function custom_slider_init(){
$labels = array(
'name'               => ' Sliders',
'singular_name'      => ' Slider ',
'menu_name'          => ' Sliders',
'name_admin_bar'     => ' Slider ',
'add_new'            => 'Add Slider ',
'add_new_item'       => 'Add New Slider ',
'new_item'           => 'New Slider ',
'edit_item'          => 'Edit Slider ',
'view_item'          => 'View Slider ',
'all_items'          => 'All Sliders ',
'search_items'       => 'Search Sliders',
'parent_item_colon'  => 'Parent Sliders',
'not_found'          => 'No Sliders found.',
'not_found_in_trash' => 'No Sliders  found in Trash.'
);
 
$args = array(
'labels'             => $labels,
'description'        => __( 'Description.', 'your-plugin-textdomain' ),
'public'             => true,
'publicly_queryable' => true,
'show_ui'            => true,
'show_in_menu'       => true,
'query_var'          => true,
'rewrite'            => array( 'slug' => 'sliders' ),
'capability_type'    => 'post',
'has_archive'        => true,
'hierarchical'       => false,
'menu_position'      => null,
'supports'           => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' )
);
 
register_post_type( 'custom_slider', $args );
}
 
add_action( 'init', 'custom_slider_init' );
 

 

Step 5. Now call the declared custom post from fronted in any arge  by using below code

The code of custom post loop

//the argument for the post
$args = array(  'post_type' => 'product'    );

//passing the argument through query post
query_posts( $args );
 
// if there is post go inside if loop
if ( have_posts() ):
//fetch the cumber of posts
while ( have_posts() ) :
the_post();
 
// Do stuff with the post content.
the_title();
the_permalink();
the_content();
the_excerpt();
echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo get_the_category_list();
endwhile;
else:
// Insert any content or load a template for no posts found.
endif;
// reset the query for the other posts have declared and to be declare
wp_reset_query();

In this post we have  learn how to declare a custom post with argument  and use the custom post in frontend as a slider

In next post we are going to learn how to create an index page for a wordpress theme

Thanks for Reading

Share The Post On -

Add a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.