Search code examples
phpwordpresspermalinks

Add a string to ter permalink in wordpress


How can I add the string "p" in front of every post url in this cutom post type? I added it in my code, wordpress is generating the link but when I click, it returns 404. What do I need to do?

<?php
/**
 * news ====================================================
 */
add_action('init', 'my_custom_news');
function my_custom_news()
{
  $labels = array(
    'name' => _x('新着情報', 'post type general name'),
    'singular_name' => _x('news', 'post type singular name'),
    'add_new' => _x('新しく新着情報を書く', 'news'),
    'add_new_item' => __('新着情報記事を書く'),
    'edit_item' => __('新着情報記事を編集'),
    'new_item' => __('新しい新着情報記事'),
    'view_item' => __('新着情報記事を見る'),
    'search_staff' => __('新着情報記事を探す'),
    'not_found' =>  __('新着情報記事はありません'),
    'not_found_in_trash' => __('ゴミ箱に新着情報記事はありません'),
    'parent_item_colon' => ''
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array(
      "slug" => "news",
      "with_front" => false
    ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
    'supports' => array('title','excerpt','editor','revisions'),
    'has_archive' => true
  );
  register_post_type('news',$args);
}


/** ---- 新着情報タクソノミー追加 ---- **/
add_action ('init','create_newscat_taxonomy','0');
function create_newscat_taxonomy () {
    $taxonomylabels = array(
    'name' => _x('新着情報カテゴリー','post type general name'),
    'singular_name' => _x('新着情報カテゴリー','post type singular name'),
    'search_items' => __('新着情報カテゴリー'),
    'all_items' => __('新着情報カテゴリー'),
    'parent_item' => __( 'Parent Cat' ),
    'parent_item_colon' => __( 'Parent Cat:' ),
    'edit_item' => __('新着情報カテゴリーを編集'),
    'add_new_item' => __('新着情報カテゴリーを書く'),
    'menu_name' => __( '新着情報カテゴリー' ),
    );
    $args = array(
    'labels' => $taxonomylabels,
    'hierarchical' => true,
    'has_archive' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'newscat' )
    );
    register_taxonomy('newscat','news',$args);
}
// Rewrite Custom Taxonomy to share base slug with Custom Post Type
function rewrite_news_category() {
$news_category_args = get_taxonomy( 'newscat' ); // returns an object

$news_category_args->show_admin_news = true;
$news_category_args->rewrite['slug'] = 'news';
$news_category_args->rewrite['with_front'] = false;

register_taxonomy( 'newscat', 'news', (array) $news_category_args );
}
add_action( 'init', 'rewrite_news_category', 11 );

function news_rewrites_init($post_link, $post = 0){
    add_rewrite_rule('news\/([0-9]+)?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&post_type=news&p=$matches[1]', 'top');
    add_rewrite_rule('news\/([A-Za-z0-9-_]+)\/?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&newscat=$matches[1]', 'top');
}
add_action('init', 'news_rewrites_init');

function cms_links1($post_link, $post) {
    if($post->post_type === 'news') {
        return home_url('news/p' . $post->ID . '/');
    } else {
        return $post_link;
    }
}
add_filter('post_type_link', 'cms_links1', 1, 3);

Solution

  • By the adding string "p" in the "rewrite" argument of your custom post type:

    'rewrite' => array(
      "slug" => "news/p",
      "with_front" => false
    )
    

    Change the links:

    function mycustomname_links($post_link, $post = 0) {
      if($post->post_type === 'news') {
         return home_url('news/p' . $post->ID . '/');
      }
      else{
        return $post_link;
      }
    }
    add_filter('post_type_link', 'mycustomname_links', 1, 3);
    

    Add the correct rewrites rules:

        function mycustomname_rewrites_init(){
        add_rewrite_rule('news/p([0-9]+)?$', 'index.php?post_type=news&p=$matches[1]', 'top');
    }
    add_action('init', 'mycustomname_rewrites_init');
    

    After this you need to flush the permalink:

    In your WordPress backend go to settings-> permalinks-> save changes
    

    Now your URL looks like this: localhost/demo/news/p83(post id)