Revision: 69196
Updated Code
at May 5, 2015 21:41 by nbpurohit
Updated Code
<?php /* Plugin Name: My Shortcode Plugin Description: display custom post type using short code. Version: 1.0 Author: Author Name */ // register custom post type to work with add_action('init', 'webr_faq_post_type'); function webr_faq_post_type() { register_post_type('webr_faq', array( 'label' => 'Faq', 'description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'map_meta_cap' => true, 'hierarchical' => false, 'rewrite' => array('slug' => 'faq', 'with_front' => true), 'query_var' => true, 'supports' => array('title','editor','excerpt','thumbnail','author','page-attributes'), 'labels' => array ( 'name' => 'Faq', 'singular_name' => 'Faq', 'menu_name' => 'Faq', 'add_new' => 'Add Faq', 'add_new_item' => 'Add New Faq', 'edit' => 'Edit', 'edit_item' => 'Edit Faq', 'new_item' => 'New Faq', 'view' => 'View Faq', 'view_item' => 'View Faq', 'search_items' => 'Search Faq', 'not_found' => 'No Faq Found', 'not_found_in_trash' => 'No Faq Found in Trash', 'parent' => 'Parent Faq', ) ) ); } add_action('init', 'webr_faq_post_type_taxonomy'); function webr_faq_post_type_taxonomy() { register_taxonomy( 'webr_faq_categories',array ( 0 => 'webr_faq', ), array( 'hierarchical' => true, 'label' => 'FAQ Categories', 'show_ui' => true, 'query_var' => true, 'show_admin_column' => true, 'labels' => array ( 'search_items' => 'Faq Category', 'popular_items' => '', 'all_items' => '', 'parent_item' => '', 'parent_item_colon' => '', 'edit_item' => '', 'update_item' => '', 'add_new_item' => 'Add New Faq Category', 'new_item_name' => 'New Faq Category', 'separate_items_with_commas' => '', 'add_or_remove_items' => '', 'choose_from_most_used' => '', ) ) ); } function cpt_content_func($atts){ extract( shortcode_atts( array( 'slug' => null, 'post_type'=>null, ), $atts ) ); $args = array( 'name' => $slug, 'post_type' => $post_type, 'posts_per_page' => 2 ); $posts= array(); $posts = get_posts( $args ); //echo "<pre>"; //print_r($posts); $content=''; $post_nav=array(); foreach ($posts as $post) : setup_postdata($post); $post_nav[] += $post->ID; $url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID) ); $content .= '<h2>'.$post->post_title.'</h1>'; if(!empty($url)){ $content .= '</br><img src='.$url[0].'>'; } $content .= '</br></br>'.$post->post_content; $content .= '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">←</span> Older posts' ) ) . '</div>'; endforeach; $current = array_search( get_the_ID(), $post_nav ); $prevID = $posts[$current-1]; $nextID = $posts[$current+1]; $content .= '<div class="navigation">'; if ( !empty( $prevID ) ): $content .= '<div class="alignleft">'; $content .= '<a href='.get_permalink( $prevID ).' title='.get_the_title( $prevID ).'>Previous</a>'; $content .= '</div>'; endif; if ( !empty( $nextID ) ): $content .= '<div class="alignright">'; $content .= '<a href='.get_permalink( $nextID ).' title='.get_the_title( $nextID ).'>Next</a>'; $content .= '</div>'; endif; $content .= '</div>'; //navigation return $content.wp_reset_postdata(); } add_shortcode('rep','cpt_content_func'); ?>
Revision: 69195
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 1, 2015 23:20 by nbpurohit
Initial Code
<?php /* Plugin Name: My Shortcode Plugin Description: display custom post type using short code. Version: 1.0 Author: Nilay Purohit */ // register custom post type to work with add_action('init', 'webr_faq_post_type'); function webr_faq_post_type() { register_post_type('webr_faq', array( 'label' => 'Faq', 'description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'map_meta_cap' => true, 'hierarchical' => false, 'rewrite' => array('slug' => 'faq', 'with_front' => true), 'query_var' => true, 'supports' => array('title','editor','excerpt','thumbnail','author','page-attributes'), 'labels' => array ( 'name' => 'Faq', 'singular_name' => 'Faq', 'menu_name' => 'Faq', 'add_new' => 'Add Faq', 'add_new_item' => 'Add New Faq', 'edit' => 'Edit', 'edit_item' => 'Edit Faq', 'new_item' => 'New Faq', 'view' => 'View Faq', 'view_item' => 'View Faq', 'search_items' => 'Search Faq', 'not_found' => 'No Faq Found', 'not_found_in_trash' => 'No Faq Found in Trash', 'parent' => 'Parent Faq', ) ) ); } add_action('init', 'webr_faq_post_type_taxonomy'); function webr_faq_post_type_taxonomy() { register_taxonomy( 'webr_faq_categories',array ( 0 => 'webr_faq', ), array( 'hierarchical' => true, 'label' => 'FAQ Categories', 'show_ui' => true, 'query_var' => true, 'show_admin_column' => true, 'labels' => array ( 'search_items' => 'Faq Category', 'popular_items' => '', 'all_items' => '', 'parent_item' => '', 'parent_item_colon' => '', 'edit_item' => '', 'update_item' => '', 'add_new_item' => 'Add New Faq Category', 'new_item_name' => 'New Faq Category', 'separate_items_with_commas' => '', 'add_or_remove_items' => '', 'choose_from_most_used' => '', ) ) ); } function cpt_content_func($atts){ extract( shortcode_atts( array( 'slug' => null, 'post_type'=>null, ), $atts ) ); $args = array( 'name' => $slug, 'post_type' => $post_type, 'posts_per_page' => 2 ); $posts= array(); $posts = get_posts( $args ); //echo "<pre>"; //print_r($posts); $content=''; $post_nav=array(); foreach ($posts as $post) : setup_postdata($post); $post_nav[] += $post->ID; $url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID) ); $content .= '<h2>'.$post->post_title.'</h1>'; if(!empty($url)){ $content .= '</br><img src='.$url[0].'>'; } $content .= '</br></br>'.$post->post_content; $content .= '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">←</span> Older posts' ) ) . '</div>'; endforeach; $current = array_search( get_the_ID(), $post_nav ); $prevID = $posts[$current-1]; $nextID = $posts[$current+1]; $content .= '<div class="navigation">'; if ( !empty( $prevID ) ): $content .= '<div class="alignleft">'; $content .= '<a href='.get_permalink( $prevID ).' title='.get_the_title( $prevID ).'>Previous</a>'; $content .= '</div>'; endif; if ( !empty( $nextID ) ): $content .= '<div class="alignright">'; $content .= '<a href='.get_permalink( $nextID ).' title='.get_the_title( $nextID ).'>Next</a>'; $content .= '</div>'; endif; $content .= '</div>'; //navigation return $content.wp_reset_postdata(); } add_shortcode('rep','cpt_content_func'); ?>
Initial URL
Initial Description
here i create custom plugin to display custom post type by adding short code to any page and also create custom post type for FAQ with name webr_faq. add following short code [rep post_type='webr_faq'] to your page and replace with custom post type name.
Initial Title
Wordpress custom plugin to create shortcode for custom post type.
Initial Tags
Initial Language
PHP