/ Published in: PHP
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.
add following short code [rep post_type='webr_faq'] to your page and replace with custom post type name.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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() { 'label' => 'Faq', 'description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'map_meta_cap' => true, 'hierarchical' => false, 'query_var' => true, '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() { 0 => 'webr_faq', ), 'label' => 'FAQ Categories', 'show_ui' => true, 'query_var' => true, 'show_admin_column' => true, '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){ 'slug' => null, 'post_type'=>null, ), $atts ) ); 'name' => $slug, 'post_type' => $post_type, 'posts_per_page' => 2 ); $posts = get_posts( $args ); //echo "<pre>"; //print_r($posts); $content=''; 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>'; $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; $prevID = $posts[$current-1]; $nextID = $posts[$current+1]; $content .= '<div class="navigation">'; $content .= '<div class="alignleft">'; $content .= '<a href='.get_permalink( $prevID ).' title='.get_the_title( $prevID ).'>Previous</a>'; $content .= '</div>'; endif; $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'); ?>