Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Custom Post in Wordpress

    • 0
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 296
    Comment on it

    In wordpress you can create custom posts , Suppose in your WP site you want to create a Movie list then you can use this custom post type ,

    To create custom posts type you have to first invoke register_post_type() through init action, Lets suppose we want to add books type, write following code in your theme function.php

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
        register_post_type( 'books',
            array(
        'labels' => array('name' => __( 'Books' ),
        'singular_name' => __( 'Book' ),
        'add_new' => __( 'Add New ' ),
        'add_new_item' => __( 'Add New Book' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit Books' ),
        'new_item' => __( 'New Books' ),
        'view' => __( 'View Books' ),
        'view_item' => __( 'View Books' ),
        'search_items' => __( 'Search Books' ),
        'not_found' => __( 'No books found' ),
        'not_found_in_trash' => __( 'No books inTrash' ),
        'parent' => __( 'Parent Books' ),
                ),
            'public' => true,
            'has_archive' => true,
            )
        );
    }
    
    

    for menu item of you want to give custom image to it , then you can pass one more parameter in

    'menu_icon' => get_stylesheet_directory_uri() . '/images/books-logo.png',

    also you have menu_position to set menu position in Admin menu

    Now we will show custom posts using WP query

     $loop = new WP_Query( array( 'post_type' => 'books', 'posts_per_page' => 10 ) ); ?>
    
    while ( $loop->have_posts() ) : $loop->the_post(); 
        

    the_title( )

    the_content();
    endwhile;

    you can also check if your custom posts exists or not

    if ( is_post_type( 'books' ) )
        echo 'Books are there!';
    else
        echo 'Sorry!';
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: