John Davidson

php - Add a string to ter permalink in wordpress

0 comments
Message:


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);


My Answer:
to the permalink in WordPress using PHP?

You can add a string to the permalink in WordPress by using the `post_link` filter hook. Here's an example code snippet to add a string to the permalink:

php
function custom_permalink($permalink, $post) {
// Add your custom string here
$custom_string = 'custom-string';

// Append the custom string to the permalink
$permalink = $permalink . $custom_string;

return $permalink;
}

add_filter('post_link', 'custom_permalink', 10, 2);


You can add this code to your theme's `functions.php` file or in a custom plugin. This code will append the custom string to the end of the permalink for all posts in your WordPress site.

Rate this post

3 of 5 based on 7934 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us