Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Created May 5, 2020 14:40
Show Gist options
  • Save maheshwaghmare/4ca391953fde877a55f652295fc6b8c3 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/4ca391953fde877a55f652295fc6b8c3 to your computer and use it in GitHub Desktop.
<?php
/**
* Add rewrite rule.
*
* @since 1.0.0
*
* @param array $rules Existing rewrite rules
* @return array
*/
if( ! function_exists( 'prefix_add_rewrite_rules' ) ) {
function prefix_add_rewrite_rules( $rules ) {
// Add new rule.
$new = array(
'astra-portfolio/([^/]+)/(.+)/?$' => 'index.php?astra-portfolio=$matches[2]'
);
return array_merge( $new, $rules );
}
add_filter( 'rewrite_rules_array', 'prefix_add_rewrite_rules' );
}
/**
* Handle the '%astra-portfolio-categories%' URL placeholder
*
* @since 1.0.0
*
* @param array $link New link.
* @return array
*/
if( ! function_exists( 'prefix_replace_placeholders' ) ) {
function prefix_replace_placeholders( $link, $post ) {
if ( $post->post_type == 'astra-portfolio' ) {
$categories = get_the_terms( $post->ID, 'astra-portfolio-categories' );
if ( $categories ) {
$link = str_replace( '%astra-portfolio-categories%', current( $categories )->slug, $link );
}
}
return $link;
}
add_filter( 'post_type_link', 'prefix_replace_placeholders', 10, 2 );
}
/**
* Handle the '%astra-portfolio-categories%' URL placeholder
*
* @since 1.0.0
*
* @param array $rules Existing rewrite rules
* @return array
*/
if( ! function_exists( 'prefix_add_post_type_rewrite_slug' ) ) {
function prefix_add_post_type_rewrite_slug( $args ) {
$args['rewrite'] = array(
'slug' => 'astra-portfolio/%astra-portfolio-categories%',
'with_front' => true
);
return $args;
}
add_filter( 'astra_portfolio_post_type_args', 'prefix_add_post_type_rewrite_slug' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment