Last active
December 2, 2023 18:44
-
-
Save softiconic/890cfd3ced2ecabed1fd20a196b14ca3 to your computer and use it in GitHub Desktop.
Create a custom post with categories - Wordpress
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
//career post | |
function create_job() { | |
register_post_type( 'job', | |
array( | |
'labels' => array( | |
'name' => __( 'Career' , 'softiconic'), | |
'singular_name' => __( 'Career' , 'softiconic'), | |
'add_new' => __('Add New Career', 'softiconic'), | |
'edit_item' => __('Edit Career', 'softiconic'), | |
'new_item' => __('New Career', 'softiconic'), | |
'view_item' => __('View Career', 'softiconic'), | |
'search_items' => __('Search Career', 'softiconic'), | |
'not_found' => __('No Career found', 'softiconic'), | |
'not_found_in_trash' => __('No Career in Trash', 'softiconic') | |
), | |
'public' => true, | |
'menu_position' => 23, | |
'supports' => array('title','editor') | |
) | |
); | |
} | |
add_action( 'init', 'create_job' ); | |
register_taxonomy( 'jobcat', | |
array('job'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */ | |
array('hierarchical' => true, /* if this is true it acts like categories */ | |
'labels' => array( | |
'name' => __( 'Categories', 'softiconic' ), /* name of the custom taxonomy */ | |
'singular_name' => __( 'Categories', 'softiconic' ), /* single taxonomy name */ | |
'search_items' => __( 'Search Categories', 'softiconic' ), /* search title for taxomony */ | |
'all_items' => __( 'All Categories', 'softiconic' ), /* all title for taxonomies */ | |
'parent_item' => __( 'Parent Categories', 'softiconic' ), /* parent title for taxonomy */ | |
'parent_item_colon' => __( 'Parent Categories:', 'softiconic' ), /* parent taxonomy title */ | |
'edit_item' => __( 'Edit Categories', 'softiconic' ), /* edit custom taxonomy title */ | |
'update_item' => __( 'Update Categories', 'softiconic' ), /* update title for taxonomy */ | |
'add_new_item' => __( 'Add New Categories', 'softiconic' ), /* add new title for taxonomy */ | |
'new_item_name' => __( 'New Categories', 'softiconic' ) /* name title for taxonomy */ | |
), | |
'show_ui' => true, | |
'query_var' => true, | |
) | |
); | |
//shortcode start | |
add_shortcode('career', 'job_shortcode'); | |
function job_shortcode($atts) | |
{ | |
ob_start(); | |
$query = new WP_Query(array( | |
'post_type' => 'job', | |
'order' => 'DESC', | |
'orderby' => 'id' | |
)); | |
if ($query->have_posts()) { ?> | |
<div class="sc_jobs emplois"> | |
<div class="ce_filter"> | |
<div class="filter-buttons text-left"> | |
<a data-filter="*" class="select-all"> </a> | |
<?php | |
$categories = get_terms( array( | |
'taxonomy' => 'jobcat', | |
'hide_empty' => false, | |
'parent' => 0, | |
) ); | |
?> | |
<?php | |
foreach($categories as $category) { | |
// if ($category->count != 0) { | |
?> | |
<a data-filter="<?php echo $category->slug; ?>" class="select-<?php echo $category->slug; ?> <?php if ($category->count != 0) {?> sc001<?php }else{ ?> sc0 <?php } ?>"> <?php echo $category->name; ?></a> | |
<?php | |
// } | |
} | |
?> | |
</div> | |
<div class="filter-container "> | |
<?php while ($query->have_posts()) : $query->the_post(); ?> | |
<?php $terms = get_the_terms( $post->ID, 'jobcat' ); ?> | |
<div class="<?php foreach ( $terms as $term ) { | |
echo ' ' . $term->slug . ' '; | |
} ?> tab cell block"> | |
<input id="tab-desktop-<?php the_ID(); ?>" type="radio" name="accordion"> | |
<label for="tab-desktop-<?php the_ID(); ?>"> | |
<div class="open-tab"> | |
<div class="top-tab"> | |
<span class="location"><?php the_field('sclocation'); ?></span> | |
<span class="job-title"><?php the_title(); ?></span> | |
<span class="sub-title"><?php the_field('scsub_title'); ?></span> | |
</div> | |
</div> | |
</label> | |
<button class="cta bg-blue mt-6 apply scjobapply"> | |
<?php | |
if (ICL_LANGUAGE_CODE=='en') { ?> | |
<p>Apply</p> | |
<?php }else{ ?> | |
<p>Postuler</p> | |
<?php } | |
?> | |
</button> | |
<div class="tab-content"> | |
<div class="inner"> | |
<?php the_content(); ?> | |
</div> | |
</div> | |
</div> | |
<?php endwhile; | |
wp_reset_postdata(); ?> | |
<div class="noitem"> | |
<h4> | |
<?php | |
if (ICL_LANGUAGE_CODE=='en') { ?> | |
<p>No Job Available!!</p> | |
<?php }else{ ?> | |
<p>Aucun emploi disponible !!</p> | |
<?php } | |
?> | |
</h4> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?php $myvariable = ob_get_clean(); | |
return $myvariable; | |
} | |
} | |
//js file | |
var container = $('.filter-container'); | |
var elements = $('.filter-container > *'); | |
var buttons = $('.filter-buttons a'); | |
// set all elements active | |
elements.addClass('selected elements'); | |
// remove select all filter | |
$('.filter-buttons a.select-all').remove(); | |
buttons.click(function(){ | |
// set all elements inactive by first call | |
elements.removeClass('selected'); | |
if ($(this).hasClass("active")) { | |
$("a").removeClass("active"); | |
} | |
else { | |
$("a").removeClass("active"); | |
$(this).addClass("active"); | |
} | |
buttons.each(function() { | |
// set selected elements active equal to active buttons | |
if($(this).hasClass('active')) { | |
container.find('.'+$(this).data('filter')).addClass('selected'); | |
} | |
}); | |
// set all elements active if no filter selected | |
// if(!elements.hasClass('noitem')) { | |
//elements.addClass('acsc'); | |
//} | |
}); | |
$('.sc0').click(function() { | |
$('.noitem').addClass("showsc"); | |
}); | |
$('.sc001').click(function() { | |
$('.noitem').removeClass("showsc"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment