Advertisement
sayful

WP Custom Post Type

Jan 17th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php
  2.  
  3. // Register Custom Post Type
  4. function themename_custom_post_type() {
  5.  
  6.     $labels = array(
  7.         'name'                => _x( 'Portfolios', 'Post Type General Name', 'text_domain' ),
  8.         'singular_name'       => _x( 'Portfolio', 'Post Type Singular Name', 'text_domain' ),
  9.         'menu_name'           => __( 'Portfolios', 'text_domain' ),
  10.         'parent_item_colon'   => __( 'Parent Portfolio Item:', 'text_domain' ),
  11.         'all_items'           => __( 'All Portfolio Items', 'text_domain' ),
  12.         'view_item'           => __( 'View Portfolio Item', 'text_domain' ),
  13.         'add_new_item'        => __( 'Add New Portfolio Item', 'text_domain' ),
  14.         'add_new'             => __( 'Add New', 'text_domain' ),
  15.         'edit_item'           => __( 'Edit Portfolio Item', 'text_domain' ),
  16.         'update_item'         => __( 'Update Portfolio Item', 'text_domain' ),
  17.         'search_items'        => __( 'Search Portfolio Item', 'text_domain' ),
  18.         'not_found'           => __( 'Not found', 'text_domain' ),
  19.         'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
  20.     );
  21.     $rewrite = array(
  22.         'slug'                => 'portfolio',
  23.         'with_front'          => true,
  24.         'pages'               => true,
  25.         'feeds'               => true,
  26.     );
  27.     $args = array(
  28.         'label'               => __( 'portfolio', 'text_domain' ),
  29.         'description'         => __( 'Post Type Description', 'text_domain' ),
  30.         'labels'              => $labels,
  31.         'supports'            => array( 'title', 'editor', 'thumbnail', ),
  32.         'hierarchical'        => false,
  33.         'public'              => true,
  34.         'show_ui'             => true,
  35.         'show_in_menu'        => true,
  36.         'show_in_nav_menus'   => true,
  37.         'show_in_admin_bar'   => true,
  38.         'menu_position'       => 5,
  39.         'menu_icon'           => 'dashicons-slides',
  40.         'can_export'          => true,
  41.         'has_archive'         => true,
  42.         'exclude_from_search' => false,
  43.         'publicly_queryable'  => true,
  44.         'rewrite'             => $rewrite,
  45.         'capability_type'     => 'page',
  46.     );
  47.     register_post_type( 'portfolio', $args );
  48.  
  49. }
  50.  
  51. // Hook into the 'init' action
  52. add_action( 'init', 'themename_custom_post_type', 0 );
  53.  
  54. /***********************************************************************
  55.  To display posts from a specific post type.
  56. ***********************************************************************/
  57. $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 );
  58. $loop = new WP_Query( $args );
  59.  
  60. while ( $loop->have_posts() ) : $loop->the_post();
  61. //Your content go here
  62.     the_title();
  63.     echo '<div class="entry-content">';
  64.     the_content();
  65.     echo '</div>';
  66. //End of your content
  67. endwhile;
  68.  
  69. /***********************************************************************
  70.  Another way to display posts from a specific post type.
  71. ***********************************************************************/
  72.  
  73.     <?php
  74.     global $post;
  75.     $args = array( 'posts_per_page' => -1, 'post_type'=> 'portfolio');
  76.     $myposts = get_posts( $args );
  77.     foreach( $myposts as $post ) : setup_postdata($post); ?>
  78.    
  79.     <?php $slider_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'slider-thumb' ); ?>
  80.      
  81.     <li data-thumb="<?php echo $slider_image[0]; ?>">
  82.         <?php the_post_thumbnail('slider-larger'); ?>
  83.         <?php the_content(); ?>
  84.     </li>
  85.      
  86.     <?php endforeach; ?>
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement