Skip to content

Instantly share code, notes, and snippets.

@Archie22is
Created May 21, 2024 08:51
Show Gist options
  • Save Archie22is/2baf9091e4c2fc5dba082dab501cff42 to your computer and use it in GitHub Desktop.
Save Archie22is/2baf9091e4c2fc5dba082dab501cff42 to your computer and use it in GitHub Desktop.
Fist this
<?php
/**
* Plugin Name: ThriveDX Custom Post Types
* Description: Registeres custom post types used by the ThriveDX website
*
*
*/
/**
* Disabled and used the CPT UI plugins already installed
* @author Archie M
*
*
// Register Custom Post Type
function custom_post_type_short_courses() {
$labels = array(
'name' => _x( 'Short Courses', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Short Course', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Short Courses', 'text_domain' ),
'name_admin_bar' => __( 'Short Course', 'text_domain' ),
'archives' => __( 'Short Course Archives', 'text_domain' ),
'attributes' => __( 'Short Course Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Short Course:', 'text_domain' ),
'all_items' => __( 'All Short Courses', 'text_domain' ),
'add_new_item' => __( 'Add New Short Course', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Short Course', 'text_domain' ),
'edit_item' => __( 'Edit Short Course', 'text_domain' ),
'update_item' => __( 'Update Short Course', 'text_domain' ),
'view_item' => __( 'View Short Course', 'text_domain' ),
'view_items' => __( 'View Short Courses', 'text_domain' ),
'search_items' => __( 'Search Short Course', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into Short course', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this Short course', 'text_domain' ),
'items_list' => __( 'Short courses list', 'text_domain' ),
'items_list_navigation' => __( 'Short courses list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter short courses list', 'text_domain' ),
);
$args = array(
'label' => __( 'Short Course', 'text_domain' ),
'description' => __( 'Post Type Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 25,
'menu_icon' => 'dashicons-book',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'courses' ), // Slug definition
);
register_post_type( 'courses', $args );
}
add_action( 'init', 'custom_post_type_short_courses', 0 );
*/
/**
* Custom courses shortcode (Short Course Types)
* @author Archie M
*
*
*
*
function short_course_types_taxonomy() {
// Register Custom Taxonomy
function custom_taxonomy() {
// Register Custom Taxonomy
function custom_taxonomy() {
$labels = array(
'name' => _x( 'Short Course Types', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Short Course Type', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Short Course Type', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'short_course_type', array( 'courses' ), $args );
}
add_action( 'inint', 'short_course_types_taxonomy', 0 );
*
* End of commented out code
*/
/**
* Display courses using a shortcode
* @author Archie M
*
*
*/
function short_courses_loop( $atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'post_type' => 'courses',
), $atts ));
// Query arguments
$args_default = array(
'post_type' => $post_type,
'post_status' => 'publish',
//'posts_per_page' => 3,
'posts_per_page' => -1,
'orderby' => 'rand',
);
// Run Query
$query_default = new WP_Query($args_default);
// Set counter
$counter = 0;
// Output
ob_start();
if( $query_default->have_posts() ) { ?>
<div id="short-courses" class="short-courses">
<?php while( $query_default->have_posts() ) { ?>
<?php $query_default->the_post(); ?>
<?php
// Increcemt counter
$counter++;
?>
<div class="short-course-item course-item-<?php echo $counter; ?> <?php if( $counter > 3) { echo "extra"; } ?>">
<?php
//Get_featured_image
$post_id = get_the_ID();
$course_image = get_the_post_thumbnail_url();
//$course_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'courde-img' )[0];
$course_taxonomy = get_the_term_list( $post->ID, 'short_course_type', '', ', ' );
?>
<?php if( !empty($course_image) ) { ?>
<a href="<?php the_permalink(); ?>" class="feat-img" style="background-image: url('<?php echo $course_image;?>')" >
<!-- background image -->
</a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>" class="feat-img" style="background-image: url('https://thrivedx.com/wp-content/uploads/2021/10/cropped-Favicon.png')">
<!-- background image -->
</a>
<?php } ?>
<div class="short-course-content">
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php
// Get ACF fields
$course_skill_level = get_field( 'course_skill_level' );
$course_summary = get_field( 'course_summary' );
$course_duration = get_field( 'course_duration' );
?>
<?php if( $course_summary ) { ?>
<p class="course-summary"><?php echo $course_summary; ?>
<?php } ?>
<!-- Course listing only -->
<div class="course-meta">
<?php if( $course_duration ) { ?>
<p><img src="<?php ?>/wp-content/uploads/2024/05/carbon_time.png"> <span><?php echo $course_duration; ?></span></p>
<?php } ?>
<?php if( $course_taxonomy ) { ?>
<p><img src="<?php ?>/wp-content/uploads/2024/05/Vector.png "> <span><?php echo strip_tags($course_taxonomy); ?></span></p>
<?php } ?>
</div>
<!-- Course signle page -->
<?php
// Get course meta
$course_price = get_field('course_price');
?>
<div class="course-details">
<p class="details-heading">Details</p>
<ul>
<?php if( $course_taxonomy) { ?>
<li>
<img src="/wp-content/uploads/2024/05/icon.png" alt="Learning method">
<span class="course-label">Learning Method<br> <span class="course-detail"><?php echo strip_tags($course_taxonomy); ?></span></span>
</li>
<?php } ?>
<?php if( $course_skill_level ) { ?>
<li>
<img src="/wp-content/uploads/2024/05/icon-skilldifficulty-1.png" alt="Skill level">
<span class="course-label">Skill Level <span class="course-detail"><?php echo $course_skill_level ?></span></span>
</li>
<?php } else { ?>
<li>
<img src="/wp-content/uploads/2024/05/icon-skilldifficulty-1.png" alt="Skill level">
<span class="course-label">Skill Level <span class="course-detail">Essentials</span></span>
</li>
<?php } ?>
<?php if( $course_duration ) { ?>
<li>
<img src="/wp-content/uploads/2024/05/icon-1.png" alt="Duration">
<span class="course-label">Duration <span class="course-detail"><?php echo $course_duration; ?></span></span>
</li>
<?php } ?>
<?php if( $course_price ) { ?>
<li>
<img src="/wp-content/uploads/2024/05/icon-2.png" alt="Price">
<span class="course-label">Price <span class="course-detail"><?php echo $course_price; ?></span></span>
</li>
<?php } ?>
<!--
<?php ?>
<li>
<img src="" alt="">
<span class="course-label"></span>
<span class="course-detail"></span>
</li>
<?php ?>
-->
</ul>
</div>
<div class="see-more-btn-wrapper">
<a class="see-more-btn" href="<?php the_permalink(); ?>" title="See More">See More ⟶</a>
</div>
</div>
</div>
<?php } ?>
</div>
<!-- View More Courses -->
<button id="view-more-button" class="view-more-button">More Courses</button>
<!-- Styles -->
<style>
/** Standard Lising Page **/
#short-courses {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.short-course-item {
background-color: #fff;
border-width: 0px 0px 6px 0px;
box-shadow: 0px 0px 10px -4px rgba(0, 0, 0, 0.3);
border-radius: 8px 8px 0 0;
display: flex;
flex-direction: column;
margin-bottom: 30px;
width: calc(33.333% - 20px)
}
.extra {
display: none;
}
a.feat-img {
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
height: 224px;
border-radius: 8px 8px 0 0;
width: 100%;
}
.short-course-content {
padding: 30px 20px 20px 20px;
}
.course-summary {
font-family: 'Inter';
font-size: 16px;
line-height: 20px;
min-height: 140px;
}
.course-details,
p.details-heading {
display: none;
}
.course-details ul {
font-size: 14px;
line-height: 18px;
margin: 0;
padding: 0;
}
.course-details ul li {
list-style-type: none;
position: relative;
margin: 10px 0;
}
.course-details ul li img {
position: absolute;
}
.course-details ul li:first-child img {
top: -0px;
}
.course-details ul li img {
top: -4px;
}
.course-details .course-label,
.course-details .course-detail {
font-family: 'Inter';
font-size: 14px;
line-height: 16px;
}
.course-details .course-label {
font-weight: 700;
padding-left: 40px;
}
.course-details ul li:first-child .course-label,
.course-details ul li:first-child .course-detail {
padding-left: 40px;
}
.course-details .course-detail {
font-weight: 400;
}
.short-course-content h2 {
color: #000;
font-family: 'Inter';
font-size: 24px;
font-weight: 700;
line-height: 28px;
margin: 0 0 20px;
min-height: 56px;
}
.short-course-content h2 a,
.short-course-content h2 a:hover,
.short-course-content h2 a:focus {
color: #000;
}
.course-meta {
margin-top: 10px;
margin-bottom: 20px;
}
.course-meta p {
color: #000;
font-size: 16px;
font-weight: 400;
line-height: 20px;
}
.course-meta p img {
height: auto;
width: 28px;
margin-right: 10px;
position: absolute;
}
.course-meta p span {
padding-left: 40px;
}
.see-more-btn-wrapper {
position: relative;
display: block;
height: 30px;
margin-top: 30px;
width: 100%;
}
.see-more-btn {
color: #000 !important;
font-family: 'Inter';
font-weight: 600;
font-size: 18px;
line-height: 24px;
position: absolute;
right: 20px;
}
.see-more-btn:hover,
.see-more-btn:focus {
color: #09B261 !important;
}
/* View more courses button */
#view-more-button {
background-color: #09B261;
border-style: solid;
border-width: 2px 2px 2px 2px;
border-color: #09B261;
border-radius: 0px 10px 0px 10px;
font-family: "Inter", inter;
font-size: 16px;
font-weight: 600;
line-height: 24px;
letter-spacing: 0.5px;
color: #fff !important;
display: block;
margin: 40px auto 0;
padding: 12px 15px 12px 15px;
text-align: center;
transition: all .3s;
max-width: 380px;
width: 100%;
}
#view-more-button:hover,
#view-more-button:focus {
border-color: #09915000;
border-radius: 0px !important;
color: #fff;
}
.page-id-31828 .elementor-widget-container .elementor-shortcode {
display: none;
}
/** Single Course Page **/
.single {
}
.single a.feat-img,
.single .course-meta,
.single .see-more-btn,
.single .see-more-btn-wrapper {
display: none;
}
.single .course-details {
display: block;
}
.single .short-course-item {
border-radius: 8px;
}
.single .short-course-item {
border-bottom: 6px solid #09B261;
}
.single .short-course-content h2 {
font-size: 20px;
line-height: 24px;;
}
.single .course-summary {
font-size: 14px;
line-height: 18px;
min-height: 100px;
}
.single p.details-heading {
color: #000;
display: block;
font-family: 'Inter';
font-weight: 800;
font-size: 16px;
line-height: 24px;
}
/** Responsive **/
@media screen and (max-width:991px) {
.short-course-item {
width: calc(50% - 20px);
}
}
@media screen and (max-width:767px) {
}
@media screen and (max-width:680px) {
.short-course-item {
width: 100%;
}
}
@media screen and (max-width:480px) {
}
</style>
<!-- Javascript -->
<script>
jQuery(document).ready(function($){
$('#view-more-button').click(function(){
$('.extra').toggle();
$this.prop('disabled', true); // Disable button after clik
});
});
</script>
<!-- More courses -->
<?php
} else { ?>
<h3>No courses were found.</h3>
<?php }
wp_reset_postdata();
//return ob_start();
if( is_page() ) {
// Page layout
return ob_start();
} else {
// Single post page
$output = ob_get_contents();
ob_end_clean();
}
return $output;
}
add_shortcode( 'shortcourses', 'short_courses_loop' );
// AJAX handler
/*
add_action('wp_ajax_load_more_courses', 'load_more_courses_callback');
add_action('wp_ajax_nopriv_load_more_courses', 'load_more_courses_callback');
function load_more_courses_callback() {
$offset = $_POST['offset'];
$post_type = $_POST['post_type'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => 3,
'offset' => $offset
);
$query = new WP_Query($args);
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
?>
<div class="short-course-item">
<?php
//get_featured_image
$post_id = get_the_ID();
$course_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'courde-img' )[0];
$course_taxonomy = get_the_term_list( $post->ID, 'short_course_type', '', ', ' );
?>
<a href="<?php the_permalink(); ?>">
</a>
<div class="short-course-content">
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php
// Get ACF fields
$course_summary = get_field('course_summary');
$course_duration = get_field('course_duration');
?>
<?php if( $course_summary ) { ?>
<p class="course-summary"><?php echo $course_summary; ?>
<?php } ?>
<div class="course-meta">
<?php if( $course_duration ) { ?>
<p><img src="<?php ?>/wp-content/uploads/2024/05/carbon_time.png"> <span><?php echo $course_duration; ?></span></p>
<?php } ?>
<?php if( $course_taxonomy ) { ?>
<p><img src="<?php ?>/wp-content/uploads/2024/05/Vector.png "> <span><?php echo strip_tags($course_taxonomy); ?></span></p>
<?php } ?>
</div>
<a class="see-more-btn" href="<?php the_permalink(); ?>" title="See More">See More ⟶</a>
</div>
</div>
<?php
}
}
wp_die();
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment