Skip to content

Instantly share code, notes, and snippets.

@MichaelConte
Last active August 29, 2015 14:22
Show Gist options
  • Save MichaelConte/7322ffdb2ab76e5278b0 to your computer and use it in GitHub Desktop.
Save MichaelConte/7322ffdb2ab76e5278b0 to your computer and use it in GitHub Desktop.
Create Short Code for Custom Post Type with WordPress Custom Plugin
<?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">&larr;</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');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment