Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create custom posts in wordpress theme

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 492
    Comment on it

    Default posts option is not sufficient for customized themes in WordPress. Sometimes users need to create and use their own post type with desired names apart from traditional blog posts.


         The following PHP code will register a new user defined custom post time ( say "testimonial" ) at WordPress admin area. Hence, one can insert edit or update their custom post types .


         Note : Plz places the following function/code  in your theme's function.php.

         

    <?php
          
            /*  Create function to register new post type named "testimonials".  
                Set labels to display for custom post type area like title label, singular and plural name display on admin area, label to edit and add post etc. */
    
                Create array to pass slug name for posts and to support optional elements like featured image, comments, excerpt etc. */
    
    
            function my_customPost() {
                $labels = array(
                    'name'               => _x( 'Testimonials', 'post type general name', 'my-plugin-textdomain' ),
                    'singular_name'      => _x( 'Testimonial', 'post type singular name', 'my-plugin-textdomain' ),
                    'menu_name'          => _x( 'Testimonials', 'admin menu', 'my-plugin-textdomain' ),
                    'name_admin_bar'     => _x( 'Testimonial', 'add new on admin bar', 'my-plugin-textdomain' ),
                    'add_new'            => _x( 'Add New', 'testimonial', 'my-plugin-textdomain' ),
                    'add_new_item'       => __( 'Add New Testimonial', 'my-plugin-textdomain' ),
                    'new_item'           => __( 'New Testimonial', 'my-plugin-textdomain' ),
                    'edit_item'          => __( 'Edit Testimonial', 'my-plugin-textdomain' ),
                    'view_item'          => __( 'View Testimonial', 'my-plugin-textdomain' ),
                    'all_items'          => __( 'All Testimonials', 'my-plugin-textdomain' ),
                    'search_items'       => __( 'Search Testimonials', 'my-plugin-textdomain' ),
                    'parent_item_colon'  => __( 'Parent Testimonials:', 'my-plugin-textdomain' ),
                    'not_found'          => __( 'No testimonials found.', 'my-plugin-textdomain' ),
                    'not_found_in_trash' => __( 'No testimonials found in Trash.', 'my-plugin-textdomain' )
                );
    
                $cust_argmnts = array(
                    'labels'             => $labels,
                            'description'        => __( 'Description.', 'my-plugin-textdomain' ),
                    'public'             => true,
                    'rewrite'            => array( 'slug' => 'testimonial' ),
                    'capability_type'    => 'post',
                    'hierarchical'       => false,
                    'show_in_menu'       => true,
                    'query_var'          => true,
                    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
                );
    
                register_post_type( 'testimonial', $cust_argmnts );    /* Register new post type with name "testimonial" for wordpress default posts it is "post" */
            }
    
           
    
           /* Call function to create custom post type on "init" hook. */
    
            add_action( 'init', 'my_customPost' );  
    
        ?>

     

    After creating posts from WordPress admin, there is a need to use them at any template page or theme's front-end.
        
        The following code will display all the user defined custom posts titles with links at the front-end which are published from WordPress admin.

        Note : Plz places the following code in your theme's template file where you want to display the all the custom posts.

           

      <?php
            $post_type_name = array(
                'post_type'        => 'testimonial',
            );
            $result_array = get_posts( $post_type_name );
    
            foreach ( $result_array as $post ) : setup_postdata( $post ); ?>
                <li>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </li>
            <?php endforeach;
            wp_reset_postdata(); ?>

     

 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: