Last active
November 18, 2019 07:32
-
-
Save hmowais/e4e90361b93fcf7a67dcbb30aeda5fcd to your computer and use it in GitHub Desktop.
show CPT as Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Show CPT Courses */ | |
// require_once WP_CONTENT_DIR . '/inc/postType.php'; | |
require get_stylesheet_directory() . '/inc/postType.php'; | |
//$course = new JW_Post_Type("course"); | |
//Create Shortcode news_flk | |
// Shortcode: [course-category cname="development-report"] | |
function courses_shortcode($atts) { | |
ob_start(); | |
// define attributes and their defaults | |
global $post; | |
extract( shortcode_atts( array ( | |
'order' => 'DESC', | |
'orderby' => 'ID', | |
'limit' => 6, | |
), $atts ) ); | |
// define query parameters based on attributes | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
$options = array( | |
'post_type' => 'course', | |
'order' => $order, | |
'orderby' => $orderby, | |
'posts_per_page' => $limit, | |
'paged' => $paged, | |
); | |
$temp = $books_loop; | |
$books_loop = null; | |
$books_loop = new WP_Query( $options ); | |
$i=0; | |
?> | |
<div class="single-news-posts container"> | |
<div class="row"> | |
<?php | |
if ( $books_loop->have_posts() ) { | |
while ( $books_loop->have_posts() ) : $books_loop->the_post(); | |
$i++; | |
global $post; | |
?> | |
<div class="single_course col-md-4 col-sm-6 col-xs-12"> | |
<div class="course_thumbnail"> | |
<?php | |
// if ( has_post_thumbnail() ) { ?> | |
<!--<a href="<?php // the_permalink();?>"><?php //the_post_thumbnail();?></a>--> | |
<?php //} else { ?> | |
<?php | |
$format = get_post_format( $post_id ); | |
if($format == 'link') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_pdf.png'; ?>"> | |
</a> | |
<?php } elseif($format == 'video') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_video.png'; ?>"> | |
</a> | |
<?php }elseif($format == 'audio') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_audio.png'; ?>"> | |
</a> | |
<?php }else { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_doc.png'; ?>"> | |
</a> | |
<?php } | |
?> | |
</div> | |
<div class="caption container"> | |
<div class="course_meta row"> | |
<div class="meta_info col-md-6"><?php | |
global $post; | |
$terms = get_the_terms( $post->ID, 'coursescategory' ); | |
//$nterms = get_the_terms( $post->ID, 'product_tag' ); | |
foreach ($terms as $term ) { | |
$store_id = $term->term_id; | |
echo $product_cat_name = $term->name; | |
break; | |
} | |
?></div> | |
<div class="date col-md-6"> | |
<!--<?php the_date('F j, Y'); ?> --> | |
<?php echo get_the_date(); ?> | |
</div> | |
</div> | |
<h4 class="heading"><a href="<?php the_permalink();?>"><?php the_title();?></a></h4> | |
<div class="course_exerpt"><?php echo wp_trim_words( get_the_content(), 12 ); ?></div> | |
<a class="mybtn" href="<?php the_permalink();?>">Learn More</a> | |
</div> | |
</div> | |
<?php endwhile; ?> | |
<div class="pagination"> | |
<?php | |
$total_pages = $books_loop->max_num_pages; | |
if ($total_pages > 1){ | |
$current_page = max(1, get_query_var('paged')); | |
echo paginate_links(array( | |
'base' => get_pagenum_link(1) . '%_%', | |
'format' => '/page/%#%', | |
'current' => $current_page, | |
'total' => $total_pages, | |
'prev_text' => __('« prev'), | |
'next_text' => __('next »'), | |
)); | |
} | |
?> | |
</div> | |
<?php } ?> | |
</div> | |
</div> | |
<?php | |
wp_reset_postdata(); | |
$myvariable = ob_get_clean(); | |
return $myvariable; | |
} | |
add_shortcode( 'courses', 'courses_shortcode' ); | |
/* another shortcode */ | |
// Shortcode: [course-category] | |
/* Show Courses CPT Specific Category content as Shortcode */ | |
function courses_category_shortcode($atts) { | |
ob_start(); | |
// define attributes and their defaults | |
global $post; | |
extract( shortcode_atts( array ( | |
'order' => 'DESC', | |
'orderby' => 'ID', | |
'limit' => 6, | |
'cname' => '', | |
), $atts ) ); | |
// define query parameters based on attributes | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
if($cname != ''){ | |
$options = array( | |
'post_type' => 'course', | |
'order' => $order, | |
'orderby' => $orderby, | |
'posts_per_page' => $limit, | |
'paged' => $paged, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'coursescategory', | |
'field' => 'slug', | |
'terms' => $cname, | |
), | |
), | |
); | |
}else{ | |
$options = array( | |
'post_type' => 'course', | |
'order' => $order, | |
'orderby' => $orderby, | |
'posts_per_page' => $limit, | |
'paged' => $paged, | |
); | |
} | |
$temp = $books_loop; | |
$books_loop = null; | |
$books_loop = new WP_Query( $options ); | |
$i=0; | |
?> | |
<div class="single-news-posts container"> | |
<div class="row equal-height"> | |
<?php | |
if ( $books_loop->have_posts() ) { | |
while ( $books_loop->have_posts() ) : $books_loop->the_post(); | |
$i++; | |
global $post; | |
?> | |
<div class="single_course col-md-4 col-sm-6 col-xs-12"> | |
<div class="course_thumbnail"> | |
<?php | |
// if ( has_post_thumbnail() ) { ?> | |
<!--<a href="<?php // the_permalink();?>"><?php //the_post_thumbnail();?></a>--> | |
<?php //} else { ?> | |
<?php | |
$format = get_post_format( $post_id ); | |
if($format == 'link') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_pdf.png'; ?>"> | |
</a> | |
<?php } elseif($format == 'video') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_video.png'; ?>"> | |
</a> | |
<?php }elseif($format == 'audio') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_audio.png'; ?>"> | |
</a> | |
<?php }else { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_doc.png'; ?>"> | |
</a> | |
<?php } | |
?> | |
</div> | |
<div class="caption container"> | |
<div class="course_meta row"> | |
<div class="meta_info col-md-6"><?php | |
global $post; | |
$terms = get_the_terms( $post->ID, 'coursescategory' ); | |
//$nterms = get_the_terms( $post->ID, 'product_tag' ); | |
foreach ($terms as $term ) { | |
$store_id = $term->term_id; | |
echo $product_cat_name = $term->name; | |
break; | |
} | |
?></div> | |
<div class="date col-md-6"> | |
<!--<?php the_date('F j, Y'); ?> --> | |
<?php echo get_the_date(); ?> | |
</div> | |
</div> | |
<h4 class="heading"><a href="<?php the_permalink();?>"><?php the_title();?></a></h4> | |
<div class="course_exerpt"><?php echo wp_trim_words( get_the_content(), 12 ); ?></div> | |
<a class="mybtn" href="<?php the_permalink();?>">Learn More</a> | |
</div> | |
</div> | |
<?php endwhile; ?> | |
<div class="pagination"> | |
<?php | |
$total_pages = $books_loop->max_num_pages; | |
if ($total_pages > 1){ | |
$current_page = max(1, get_query_var('paged')); | |
echo paginate_links(array( | |
'base' => get_pagenum_link(1) . '%_%', | |
'format' => '/page/%#%', | |
'current' => $current_page, | |
'total' => $total_pages, | |
'prev_text' => __('« prev'), | |
'next_text' => __('next »'), | |
)); | |
} | |
?> | |
</div> | |
<?php } ?> | |
</div> | |
</div> | |
<?php | |
/* Restore original Post Data */ | |
wp_reset_postdata(); | |
$myvariable = ob_get_clean(); | |
return $myvariable; | |
} | |
add_shortcode( 'course-category', 'courses_category_shortcode' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* custom post type */ | |
// Register Custom Post Type Courses | |
function create_courses_cpt() { | |
$labels = array( | |
'name' => _x( 'Course', 'Post Type General Name', 'mid' ), | |
'singular_name' => _x( 'Course', 'Post Type Singular Name', 'mid' ), | |
'menu_name' => _x( 'Courses', 'Admin Menu text', 'mid' ), | |
'name_admin_bar' => _x( 'Course', 'Add New on Toolbar', 'mid' ), | |
'archives' => __( 'Course Archives', 'mid' ), | |
'attributes' => __( 'Course Attributes', 'mid' ), | |
'parent_item_colon' => __( 'Parent Courses:', 'mid' ), | |
'all_items' => __( 'All Courses', 'mid' ), | |
'add_new_item' => __( 'Add New Course', 'mid' ), | |
'add_new' => __( 'Add New', 'mid' ), | |
'new_item' => __( 'New Course', 'mid' ), | |
'edit_item' => __( 'Edit Course', 'mid' ), | |
'update_item' => __( 'Update Course', 'mid' ), | |
'view_item' => __( 'View Course', 'mid' ), | |
'view_items' => __( 'View Courses', 'mid' ), | |
'search_items' => __( 'Search Courses', 'mid' ), | |
'not_found' => __( 'Not found', 'mid' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'mid' ), | |
'featured_image' => __( 'Featured Image', 'mid' ), | |
'set_featured_image' => __( 'Set featured image', 'mid' ), | |
'remove_featured_image' => __( 'Remove featured image', 'mid' ), | |
'use_featured_image' => __( 'Use as featured image', 'mid' ), | |
'insert_into_item' => __( 'Insert into Courses', 'mid' ), | |
'uploaded_to_this_item' => __( 'Uploaded to this Courses', 'mid' ), | |
'items_list' => __( 'Courses list', 'mid' ), | |
'items_list_navigation' => __( 'Courses list navigation', 'mid' ), | |
'filter_items_list' => __( 'Filter Courses list', 'mid' ), | |
); | |
$args = array( | |
'label' => __( 'Courses', 'mid' ), | |
'description' => __( '', 'mid' ), | |
'labels' => $labels, | |
'menu_icon' => 'dashicons-book', | |
'supports' => array('title', 'editor', 'post-formats', 'thumbnail'), | |
// 'taxonomies' => array('surveyspecialty','surveylocated'), | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => true, | |
'has_archive' => true, | |
'hierarchical' => false, | |
'exclude_from_search' => false, | |
'show_in_rest' => true, | |
'publicly_queryable' => true, | |
'capability_type' => 'post', | |
); | |
register_post_type( 'course', $args ); | |
flush_rewrite_rules(true); | |
} | |
add_action( 'init', 'create_courses_cpt', 0 ); | |
//add_action( 'init', 'create_survey_services' ); | |
// Register Taxonomy Courses Category | |
function create_coursescategory_tax() { | |
$labels = array( | |
'name' => _x( 'Courses Category', 'taxonomy general name', 'mid' ), | |
'singular_name' => _x( 'Courses Category', 'taxonomy singular name', 'mid' ), | |
'search_items' => __( 'Search Courses Category', 'mid' ), | |
'all_items' => __( 'All Courses Category', 'mid' ), | |
'parent_item' => __( 'Parent Courses Category', 'mid' ), | |
'parent_item_colon' => __( 'Parent Courses Category:', 'mid' ), | |
'edit_item' => __( 'Edit Courses Category', 'mid' ), | |
'update_item' => __( 'Update Courses Category', 'mid' ), | |
'add_new_item' => __( 'Add New Courses Category', 'mid' ), | |
'new_item_name' => __( 'New Courses Category', 'mid' ), | |
'menu_name' => __( 'Courses Category', 'mid' ), | |
); | |
$args = array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
); | |
register_taxonomy( 'coursescategory', array('course'), $args ); | |
} | |
add_action( 'init', 'create_coursescategory_tax' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Create single-course.php file to show single CPT page */ | |
<?php | |
/** | |
* The template for displaying all single posts | |
* | |
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post | |
* | |
* @package WordPress | |
* @subpackage Twenty_Nineteen | |
* @since 1.0.0 | |
*/ | |
?> | |
<?php get_header(); ?> | |
<?php if (have_posts()) : ?> | |
<?php while (have_posts()) : the_post(); ?> | |
<?php get_template_part( 'title' ); ?> | |
<?php get_template_part( 'slider' ); ?> | |
<?php | |
// if ( $books_loop->have_posts() ) { | |
// while ( $books_loop->have_posts() ) : $books_loop->the_post(); | |
// $i++; | |
// global $post; | |
?> | |
<article class="container_inner default_template_holder"> | |
<section class="full_course container-fluid"> | |
<div class="row"> | |
<div class="post_image col-md-7 col-sm-6 col-xs-12"> | |
<?php | |
if ( has_post_thumbnail() ) { ?> | |
<a href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a> | |
<?php } ?> | |
</div> | |
<div class="caption col-md-5 col-sm-6 col-xs-12"> | |
<div class="course_meta"> | |
<div class="selected_course_format"> | |
<?php | |
$format = get_post_format( $post_id ); | |
if($format == 'link') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_pdf.png'; ?>"> | |
</a> | |
<?php } elseif($format == 'video') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_video.png'; ?>"> | |
</a> | |
<?php }elseif($format == 'audio') { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_audio.png'; ?>"> | |
</a> | |
<?php }else { ?> | |
<a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_doc.png'; ?>"> | |
</a> | |
<?php } | |
?> | |
</div> | |
</div> | |
<br> | |
<h2 class="heading"><a href="<?php the_permalink();?>"><?php the_title();?></a></h2><br> | |
<div class="meta_info"><div class="date"> | |
Posted at: <?php echo get_the_date(); ?> in <?php | |
global $post; | |
$terms = get_the_terms( $post->ID, 'coursescategory' ); | |
//$nterms = get_the_terms( $post->ID, 'product_tag' ); | |
foreach ($terms as $term ) { | |
$store_id = $term->term_id; | |
echo $product_cat_name = $term->name; | |
break; | |
} | |
?> by <?php echo get_author_name(); ?> | |
</div></div><br> | |
<a class="mybtn" href="<?php the_permalink();?>"><?php the_title();?></a> | |
</div> | |
</div> | |
<div class="row"> | |
<div class="course_content"><?php the_content(); ?></div> | |
</div> | |
</section> | |
<!--- PDF SECTION --> | |
<?php if( have_rows('for_pdf') ): ?> | |
<section class="other_content py-50"> | |
<div class="container"> | |
<div class="row middle-center"> | |
<div class="col-md-6 col-sm-6 col-xs-12"> | |
<div class="course_meta row middle-center"> | |
<div class="align-left col-md-3"><a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_pdf.png'; ?>"></a></div> | |
<div class="align-left col-md-9"><h4><?php the_title();?></h4></div> | |
</div> | |
</div> | |
<div class="col-md-6 col-sm-6 col-xs-12"> | |
<div class="course_meta row middle-center"> | |
<div class="align-right col-md-8"><?php echo $product_cat_name; ?></div> | |
<div class="align-right col-md-4"><?php echo get_the_date(); ?></div> | |
</div> | |
</div> | |
</div> | |
<div class="row pdf_course"> | |
<?php | |
// check if the repeater field has rows of data | |
if( have_rows('for_pdf') ): | |
// loop through the rows of data | |
while ( have_rows('for_pdf') ) : the_row(); | |
// display a sub field value | |
echo $pdf = get_sub_field('pdf'); | |
// $pdf = "https://cdn.s3waas.gov.in/master/uploads/2016/09/document_1481208108.pdf"; | |
?> | |
<object data="<?php echo $pdf; ?>" type="application/pdf" width="100%" height="100%" style="width:100%;height:100%;min-height:800px;max-height:1000px;"> | |
<p>Alternative text - include a link <a href="<?php echo $pdf; ?>">to the PDF!</a></p> | |
</object> | |
<?php | |
endwhile; | |
else : | |
// no rows found | |
endif; | |
?> | |
</div> | |
</div> | |
</section> | |
<?php endif; ?> | |
<!--- VIDEO SECTION --> | |
<?php if( have_rows('for_video') ): ?> | |
<section class="other_content py-50"> | |
<div class="container"> | |
<div class="row middle-center pb-20"> | |
<div class="col-md-6 col-sm-6 col-xs-12"> | |
<div class="course_meta row middle-center"> | |
<div class="align-left col-md-3"><a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_video.png'; ?>"></a></div> | |
<div class="align-left col-md-9"><h4><?php the_title();?></h4></div> | |
</div> | |
</div> | |
<div class="col-md-6 col-sm-6 col-xs-12"> | |
<div class="course_meta row middle-center"> | |
<div class="align-right col-md-8"><?php echo $product_cat_name; ?></div> | |
<div class="align-right col-md-4"><?php echo get_the_date(); ?></div> | |
</div> | |
</div> | |
</div> | |
<div class="row video_course"> | |
<?php | |
// check if the repeater field has rows of data | |
if( have_rows('for_video') ): | |
// loop through the rows of data | |
while ( have_rows('for_video') ) : the_row(); | |
// display a sub field value | |
$video = get_sub_field('video'); | |
echo $video; | |
?> | |
<?php | |
endwhile; | |
else : | |
// no rows found | |
endif; | |
?> | |
</div> | |
</div> | |
</section> | |
<?php endif; ?> | |
<!--- AUDIO SECTION --> | |
<?php if( have_rows('for_audio') ): ?> | |
<section class="other_content py-50"> | |
<div class="container"> | |
<div class="row middle-center pb-20"> | |
<div class="col-md-6 col-sm-6 col-xs-12"> | |
<div class="course_meta row middle-center"> | |
<div class="align-left col-md-3"><a href="<?php the_permalink();?>"> | |
<img src="<?php echo get_stylesheet_directory_uri() . '/images/for_audio.png'; ?>"></a></div> | |
<div class="align-left col-md-9"><h4><?php the_title();?></h4></div> | |
</div> | |
</div> | |
<div class="col-md-6 col-sm-6 col-xs-12"> | |
<div class="course_meta row middle-center"> | |
<div class="align-right col-md-8"><?php echo $product_cat_name; ?></div> | |
<div class="align-right col-md-4"><?php echo get_the_date(); ?></div> | |
</div> | |
</div> | |
</div> | |
<div class="row audio_course"> | |
<?php | |
// check if the repeater field has rows of data | |
if( have_rows('for_audio') ): | |
// loop through the rows of data | |
while ( have_rows('for_audio') ) : the_row(); | |
// display a sub field value | |
$video = get_sub_field('audio'); | |
echo $video; | |
?> | |
<?php | |
endwhile; | |
else : | |
// no rows found | |
endif; | |
?> | |
</div> | |
</div> | |
</section> | |
<?php endif; ?> | |
<!-- Article End --> | |
</article> | |
<?php endwhile; ?> | |
<?php endif; ?> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment