Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function custom_rewrite_tag() {
- add_rewrite_tag( '%city%', '([^&]+)', 'city=' );
- add_rewrite_tag( '%postname%', '([^&]+)', 'name=' );
- }
- add_action( 'init', 'custom_rewrite_tag', 10, 0 );
- function add_vars( $vars ) {
- $vars[] = "city";
- $vars[] = "postname";
- return $vars;
- }
- add_filter( 'query_vars', 'add_vars' );
- function wpa_course_post_link( $post_link, $id = 0 ) {
- $post = get_post( $id );
- if ( is_object( $post ) ) {
- $city = get_field( 'sd_city', $post->ID );
- if ( $city ) {
- // $post_link = home_url( 'sexdate/' . strtolower($city) . '/' . $post->post_name . '-' . $post->ID);
- }
- }
- // var_dump($post_link);
- // exit;
- return $post_link;
- }
- // add_filter( 'post_link', 'wpa_course_post_link', 10, 3 );
- // add_filter( 'post_type_link', 'wpa_course_post_link', 10, 3 );
- function mycptonomy_permalink( $permalink, $post_id, $leavename = false ) {
- if ( strpos( $permalink, '%city%' ) === FALSE ) {
- return $permalink;
- }
- // Get post
- $post = get_post( $post_id );
- if ( ! $post ) {
- return $permalink;
- }
- // Get custom info
- $city_info = get_field( 'sd_city_selector', $post->ID );
- $post_slug = $post->post_name;
- if ( ! is_wp_error( $city_info ) && !empty( $city_info ) ) {
- $city_replace = str_replace('\'', '', $city_info['cityNameAscii'] );
- $city_replace = str_replace(' ', '-', $city_replace );
- $city_slug = strtolower( $city_replace );
- // echo '<pre>'; var_dump($city_slug ); echo '</pre>';
- } else {
- $city_slug = ''; // not really necessary since every post has obligated city field
- }
- $new_permalink = str_replace( array( '%city%', '%postname%', '%post_id%' ), array( $city_slug, $post_slug, $post->ID ), $permalink );
- return $new_permalink;
- }
- add_filter( 'post_link', 'mycptonomy_permalink', 10, 3 );
- add_filter( 'post_type_link', 'mycptonomy_permalink', 10, 3 );
- function force_mycpt_rewrite() {
- global $wp_rewrite;
- $wp_rewrite->add_permastruct( 'sexdate', 'sexdate/%city%/%postname%-%post_id%/', false );
- // add_rewrite_rule( 'sexdate/([a-z])/(.+)/?$', 'index.php?post_type=sexdate&p=$matches[2]', 'top' );
- // add_rewrite_rule( 'sexdate\/([a-z]+)\/(.+)-[0-9]+\/?$', 'index.php?post_type=sexdate&city=$matches[1]&p=$matches[3]', 'top' ); // lists posts
- add_rewrite_rule( 'sexdate\/([a-z-]+)\/(.+)-[0-9]+\/?$', 'index.php?post_type=sexdate&p=$matches[2]&city=$matches[1]&name=$matches[2]', 'top' );
- // add_rewrite_rule(
- // sprintf( '^%s/([a-z])/([^/]*)?$', 'sexdate' ),
- // sprintf( 'index.php?post_type=%s&%s=$matches[1]', 'sexdate', 'sexdate' ),
- // 'top'
- // );
- // $wp_rewrite->flush_rules( true ); // !!!
- // echo '<pre>'; var_dump( $wp_rewrite ); echo '</pre>';
- // exit;
- }
- add_action( 'init', 'force_mycpt_rewrite' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement