| 1 |
<?php |
| 2 |
// Custom post types |
| 3 |
function kad_portfolio_post_init() { |
| 4 |
$portfoliolabels = array( |
| 5 |
'name' => __('Portfolio', 'virtue-toolkit'), |
| 6 |
'singular_name' => __('Portfolio Item', 'virtue-toolkit'), |
| 7 |
'add_new' => __('Add New', 'virtue-toolkit'), |
| 8 |
'add_new_item' => __('Add New Portfolio Item', 'virtue-toolkit'), |
| 9 |
'edit_item' => __('Edit Portfolio Item', 'virtue-toolkit'), |
| 10 |
'new_item' => __('New Portfolio Item', 'virtue-toolkit'), |
| 11 |
'all_items' => __('All Portfolio', 'virtue-toolkit'), |
| 12 |
'view_item' => __('View Portfolio Item', 'virtue-toolkit'), |
| 13 |
'search_items' => __('Search Portfolio', 'virtue-toolkit'), |
| 14 |
'not_found' => __('No Portfolio Item found', 'virtue-toolkit'), |
| 15 |
'not_found_in_trash' => __('No Portfolio Items found in Trash', 'virtue-toolkit'), |
| 16 |
'parent_item_colon' => '', |
| 17 |
'menu_name' => __('Portfolio', 'virtue-toolkit') |
| 18 |
); |
| 19 |
|
| 20 |
$portargs = array( |
| 21 |
'labels' => $portfoliolabels, |
| 22 |
'public' => true, |
| 23 |
'publicly_queryable' => true, |
| 24 |
'show_ui' => true, |
| 25 |
'show_in_menu' => true, |
| 26 |
'query_var' => true, |
| 27 |
//'rewrite' => array( 'slug' => 'portfolio', 'feeds' => true), |
| 28 |
'has_archive' => false, |
| 29 |
'capability_type' => 'post', |
| 30 |
'hierarchical' => false, |
| 31 |
'menu_position' => 8, |
| 32 |
'menu_icon' => 'dashicons-format-gallery', |
| 33 |
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'page-attributes', 'thumbnail', 'custom-fields', 'comments' ) |
| 34 |
); |
| 35 |
// Initialize Taxonomy Labels |
| 36 |
$worklabels = array( |
| 37 |
'name' => __( 'Portfolio Type', 'virtue-toolkit' ), |
| 38 |
'singular_name' => __( 'Type', 'virtue-toolkit' ), |
| 39 |
'search_items' => __( 'Search Type', 'virtue-toolkit' ), |
| 40 |
'all_items' => __( 'All Type', 'virtue-toolkit' ), |
| 41 |
'parent_item' => __( 'Parent Type', 'virtue-toolkit' ), |
| 42 |
'parent_item_colon' => __( 'Parent Type:', 'virtue-toolkit' ), |
| 43 |
'edit_item' => __( 'Edit Type', 'virtue-toolkit' ), |
| 44 |
'update_item' => __( 'Update Type', 'virtue-toolkit' ), |
| 45 |
'add_new_item' => __( 'Add New Type', 'virtue-toolkit' ), |
| 46 |
'new_item_name' => __( 'New Type Name', 'virtue-toolkit' ), |
| 47 |
); |
| 48 |
$portfolio_type_slug = apply_filters('kadence_portfolio_type_slug', 'portfolio-type'); |
| 49 |
// Register Custom Taxonomy |
| 50 |
register_taxonomy('portfolio-type',array('portfolio'), array( |
| 51 |
'hierarchical' => true, // define whether to use a system like tags or categories |
| 52 |
'labels' => $worklabels, |
| 53 |
'show_ui' => true, |
| 54 |
'query_var' => true, |
| 55 |
'rewrite' => array( 'slug' => $portfolio_type_slug ) |
| 56 |
)); |
| 57 |
|
| 58 |
register_post_type( 'portfolio', $portargs ); |
| 59 |
} |
| 60 |
add_action( 'init', 'kad_portfolio_post_init', 1 ); |
| 61 |
function kad_portfolio_permalink_init(){ |
| 62 |
global $wp_rewrite; |
| 63 |
$port_rewrite = apply_filters('kadence_portfolio_permalink_slug', 'portfolio'); |
| 64 |
$portfolio_structure = '/'.$port_rewrite.'/%portfolio%'; |
| 65 |
$wp_rewrite->add_rewrite_tag("%portfolio%", '([^/]+)', "portfolio="); |
| 66 |
$wp_rewrite->add_permastruct('portfolio', $portfolio_structure, false); |
| 67 |
} |
| 68 |
add_action( 'init', 'kad_portfolio_permalink_init', 2 ); |
| 69 |
|
| 70 |
// Add filter to plugin init function |
| 71 |
add_filter('post_type_link', 'kad_portfolio_permalink', 10, 3); |
| 72 |
|
| 73 |
function kad_portfolio_permalink($permalink, $post_id, $leavename) { |
| 74 |
$post = get_post($post_id); |
| 75 |
$rewritecode = array( |
| 76 |
'%year%', |
| 77 |
'%monthnum%', |
| 78 |
'%day%', |
| 79 |
'%hour%', |
| 80 |
'%minute%', |
| 81 |
'%second%', |
| 82 |
$leavename? '' : '%postname%', |
| 83 |
'%post_id%', |
| 84 |
'%category%', |
| 85 |
'%author%', |
| 86 |
$leavename? '' : '%pagename%', |
| 87 |
); |
| 88 |
|
| 89 |
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) { |
| 90 |
$unixtime = strtotime($post->post_date); |
| 91 |
|
| 92 |
$category = ''; |
| 93 |
if ( strpos($permalink, '%category%') !== false ) { |
| 94 |
$cats = wp_get_post_terms($post->ID, 'portfolio-type', array( 'orderby' => 'parent', 'order' => 'DESC' )); |
| 95 |
if ( $cats ) { |
| 96 |
//usort($cats, '_usort_terms_by_ID'); // order by ID |
| 97 |
$category = $cats[0]->slug; |
| 98 |
} |
| 99 |
// show default category in permalinks, without |
| 100 |
// having to assign it explicitly |
| 101 |
if ( empty($category) ) { |
| 102 |
$category = 'portfolio-category'; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$author = ''; |
| 107 |
if ( strpos($permalink, '%author%') !== false ) { |
| 108 |
$authordata = get_userdata($post->post_author); |
| 109 |
$author = $authordata->user_nicename; |
| 110 |
} |
| 111 |
|
| 112 |
$date = explode(" ",date('Y m d H i s', $unixtime)); |
| 113 |
$rewritereplace = |
| 114 |
array( |
| 115 |
$date[0], |
| 116 |
$date[1], |
| 117 |
$date[2], |
| 118 |
$date[3], |
| 119 |
$date[4], |
| 120 |
$date[5], |
| 121 |
$post->post_name, |
| 122 |
$post->ID, |
| 123 |
$category, |
| 124 |
$author, |
| 125 |
$post->post_name, |
| 126 |
); |
| 127 |
$permalink = str_replace($rewritecode, $rewritereplace, $permalink); |
| 128 |
} else { // if they're not using the fancy permalink option |
| 129 |
} |
| 130 |
return $permalink; |
| 131 |
} |
| 132 |
|
| 133 |
|