Add a string to ter permalink in wordpress - php

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

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)

Related

Wordpress Custom Post Type Permalink with Parent Post Title in URL

I just created a custom post type called 'mtl_chapter'. With my script, I assign a post parent of my CPT to the regular 'post' type. So, my CPT are basically the children of my regular posts. I want to change the permalink structure of my CPT from
/base-slug/cpt-post-title/
to
/parent-title/cpt-post-title/
So, It will look like the attachment posts do with the permalink:
/parent-title/attachment-post-title/
My current code is able to change the permalink structure to what I want, but I get
404 not found
when I click the link. Please help me, here is my current code:
function create_posttype() {
register_post_type( 'mtl_chapter',
array(
'labels' => array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'parent_item_colon' => 'Novel Title:',
'add_new' => _x('Add New', 'indomtl'),
'add_new_item' => __( 'Add New Chapter' )
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-format-aside',
'rewrite' => array('slug' => '%parent-post-name%','with_front' => true),
'exclude_from_search' => true,
'show_ui' => true,
'menu_position' => 5
)
);
}
add_action( 'init', 'create_posttype' );
add_filter('post_type_link', 'mtl_update_permalink_structure', 10, 2);
function mtl_update_permalink_structure( $post_link, $post )
{
if ( false !== strpos( $post_link, '%parent-post-name%' ) ) {
$parent_id = wp_get_post_parent_id($post->ID);
$parent_post = get_post($parent_id);
$slug = $parent_post->post_name;
if ( $slug ) {
$post_link = str_replace( '%parent-post-name%', $slug, $post_link );
}
}
return $post_link;
}
try using
function create_myposttype() {
register_post_type( 'mtl_chapter',
array(
'labels' => array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'parent_item_colon' => 'Novel Title:',
'add_new' => _x('Add New', 'indomtl'),
'add_new_item' => __( 'Add New Chapter' )
),
'hierarchical' => true,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-format-aside',
'rewrite' => array('slug' => 'mtl_chapter','with_front' => true),
'exclude_from_search' => true,
'show_ui' => true,
'menu_position' => 5,
'supports' => array(
'page-attributes' /* This will show the post parent field */,
'title',
'editor',
),
)
);
}
add_action( 'init', 'create_myposttype' );

permalink not working on custom post and category

I am trying to add wordpress custom post type and category in wordpress
add_action( 'init', 'work_register' );
function work_register() {
$labels = array(
'name' => _x('Work', 'post type general name'),
'singular_name' => _x('Work Item', 'post type singular name'),
'add_new' => _x('Add New', 'work item'),
'add_new_item' => __('Add New Work Item'),
'edit_item' => __('Edit Work Item'),
'new_item' => __('New Work Item'),
'view_item' => __('View Work Item'),
'search_items' => __('Search Work'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => array( 'slug' => 'work', 'with_front'=> false ),
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'work' , $args );
register_taxonomy( 'categories', array('work'), array(
'hierarchical' => true,
'label' => 'Categories',
'singular_label' => 'Category',
'rewrite' => array( 'slug' => 'categories', 'with_front'=> false )
)
);
register_taxonomy_for_object_type( 'categories', 'work' );
}
My permalink structure is:
/%category%/%postname%/
When I am adding from post it is giving me link
/category/postname
but when I am adding from custom post, it is giving me only
/postname
I want /category/postname in custom post also
Please suggest where I am wrong
Change your rewrite to add the work post type query var:
'rewrite' => array('slug' => 'work/%categories%')
Then filter post_type_link to insert the selected categories into the permalink:
function custom_work_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'work' );
if( $terms ){
return str_replace( '%categories%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'custom_work_post_link', 1, 3 );
There are also plugins like Custom Post Type Permalinks that can do this for you.

Wordpress: Custom Taxonomy Not Consistently in URL

I have a custom post type and custom taxonomy set up using the code below in functions.php.
The URL writes correctly when clicking through on the archive pages on the front end and when clicking "View Post" in the admin edit screen. But when the post is returned in site search results, the custom taxonomy is literally missing. The URL on search results is http://www.example.com/foo//postname.
Any idea why the custom taxonomy in the URL would work in some situations but not in others?
add_action( 'init', 'create_foo_posttype' );
function create_foo_posttype() {
$tax_labels = array(
'name' => _x( 'Foo Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Foo Category', 'taxonomy singular name' ),
'all_items' => __( 'All Foo Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Foo Category' ),
'update_item' => __( 'Update Foo Category' ),
'add_new_item' => __( 'Add New Foo Category' ),
'new_item_name' => __( 'New Foo Category Name' ),
);
register_taxonomy('foo_categories','foo',array(
'labels' => $tax_labels,
'hierarchical' => true,
'has_archive' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'foo' )
));
register_post_type( 'foo',
array(
'labels' => array(
'name' => __( 'Foo' ),
'singular_name' => __( 'Foo' )
),
'public' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 100,
'capability_type' => 'post',
'supports' => array( 'title', 'author', 'thumbnail', 'trackbacks', 'revisions' ),
'taxonomies' => array( 'foo_categories' ),
'has_archive' => true,
'rewrite' => array( 'slug' => 'foo/%foo_categories%')
)
);
flush_rewrite_rules();
}
add_action( 'init', 'cust_rewrite_init' );
function cust_rewrite_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'cust_rewrite_collect_page_rewrite_rules' );
function cust_rewrite_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['cust_rewrite_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'cust_rewrite_prepend_page_rewrite_rules' );
function cust_rewrite_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['cust_rewrite_page_rewrite_rules'] + $rewrite_rules;
}
I found the problem. It wasn't in the code I posted originally. It was here:
add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
function cust_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
$post_type_term = get_the_terms( $post->ID, 'foo_categories' );
$post_link = str_replace( '%foo_categories%', $post_type_term[0]->slug, $post_link );
}
return $post_link;
}
Using $post_type_term[0]->slug; worked if a post had more than one tax value, but failed when the post had only one (which all of my posts do). To fix it, I used array_pop instead of the other array call and it works fine. The resolved code:
add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
function cust_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
$post_type_term = get_the_terms( $post->ID, 'foo_categories' );
$one_term = array_pop($post_type_term);
$post_link = str_replace( '%foo_categories%', $one_term->slug, $post_link );
}
return $post_link;
}

Displayin custom taxonomies in custom post type columns

I've looked over several posts about adding custom taxonomies to CPT columns; I'm able to get everything working except actually displaying said taxonomies (publication). Here's my CPT code:
add_action( 'init', 'pb_custom_post_type' );
function pb_custom_post_type() {
$labels = array(
'name' => _x( 'Press', 'post type general name' ),
'singular_name' => _x( 'Press', 'post type singular name' ),
'add_new' => _x( 'Add New', 'review' ),
'add_new_item' => __( 'Add New Press' ),
'edit_item' => __( 'Edit Press' ),
'new_item' => __( 'New Press' ),
'all_items' => __( 'All Press' ),
'view_item' => __( 'View Press' ),
'search_items' => __( 'Search Press' ),
'not_found' => __( 'No press found' ),
'not_found_in_trash' => __( 'No press found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Press'
);
$args = array(
'labels' => $labels,
'description' => 'Press information',
'public' => true,
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'has_archive' => true,
);
register_post_type( 'press', $args );
}
add_filter( 'manage_edit-press_columns', 'my_edit_press_columns' ) ;
function my_edit_press_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title' ),
'publication' => __( 'Publication' ),
'date' => __( 'Date' )
);
return $columns;
}
Custom Taxonomy
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
}
Now I've seen two different choices, either trying to add show_ui/show_admin_column in the taxonomy arguments, or having another function with switch statements. I've tried both below, is there something crucial I'm missing?
1
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
$args = array (
'show_ui' => true,
'show_admin_column' => true,
);
register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ), $args );
}
2
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case "publication":
echo get_post_meta( $post_id, 'publication', true);
break;
}
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
Remove your custom columns function, and add 'show_admin_column' => true to the actual array of args for register_taxonomy:
register_taxonomy( 'publication', 'press', array( 'show_admin_column' => true, 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
Edit: you might also want to add 'taxonomies' => array( 'publication' ) to the register_post_type args.

how to get custom post based on custom category in wordpress?

i tried with following code to create custom post and custom category
<?php
/*** added for browse plugin custom post - start ***/
add_action('init', 'demoplugin_register');
function demoplugin_register() {
$labels = array(
'name' => _x('Demoplugin', 'post type general name'),
'singular_name' => _x('Demoplugin Entry', 'post type singular name'),
'add_new' => _x('Add New Demoplugin', 'demoplugin'),
'add_new_item' => __('Add New Demoplugin Entry'),
'edit_item' => __('Edit Demoplugin Entry'),
'new_item' => __('New Demoplugin Entry'),
'view_item' => __('View Demoplugin Entry'),
'search_items' => __('Search Demoplugin Entries'),
'not_found' => __('No Demoplugin Entries found'),
'not_found_in_trash' => __('No Demoplugin Entries found in Trash'),
'parent_item_colon' => ''
);
$slugRule = get_option('category_base');
//if($slugRule == "") $slugRule = 'category';
global $paged;
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'_builtin' => false,
'rewrite' => array('slug'=>'demoplugin','with_front'=>false),
'capability_type' => 'post',
'hierarchical' => false,
'show_in_nav_menus'=> false,
'query_var' => true,
'paged' => $paged,
'menu_position' => 5,
'supports' => array('title','thumbnail','excerpt','editor','comments')
);
register_post_type('demoplugin' , $args);
register_taxonomy("demoplugin_entries",
array("demoplugin"),
array( "hierarchical" => true,
"label" => "Demoplugin Categories",
"singular_label" => "Demoplugin Categories",
'rewrite' => array('slug' => 'demoplugin-category'),
"query_var" => true,
'paged' => $paged
));
flush_rewrite_rules( false );
}
function demoplugin_taxonomies() {
register_taxonomy(
'plugtag',
'demoplugin',
array(
'hierarchical' => false,
'label' => 'Demoplugin Tags',
'query_var' => true,
'rewrite' => array( 'slug' => 'plugtag' ),
)
);
}
add_action('init', 'demoplugin_taxonomies', 0);
add_action('admin_init', 'add_demoplugin');
flush_rewrite_rules(false);
add_action('save_post', 'update_demoplugin');
function add_demoplugin(){
add_meta_box("demoplugin_details", "Demoplugin Options", "demoplugin_options", "demoplugin", "normal", "low");
}
function demoplugin_options(){
global $post;
$custom = get_post_custom($post->ID);
$demoplugin_path_url = $custom["demoplugin_path_url"][0];
$download_url = $custom["download_url"][0];
$demoplugin_video_url = $custom["demoplugin_video_url"][0];
//$demoplugin_excerpt = $custom["demoplugin_excerpt"][0];
$demoplugin_radiogroup = $custom["demoplugin_radiogroup"][0];
if ($demoplugin_radiogroup == '') $demoplugin_radiogroup = 'demoplugin_post_action';
/*** added for browse plugin custom post - stop ***/
?>
above code has taken from functions.php.my requirement is list the custom post based on custom category.
i have tried so many ways to achive my requirement but its fails.
kindly advice on this.
why is there a paged argument? there is none.
query_var => true is invalid it should be false or set as a string, I recommend you skip it at all.
Also there is no singular_label for the taxonomy there is a singular_name for the labels array
Read the docs and validate your code first
http://codex.wordpress.org/Function_Reference/register_post_type
http://codex.wordpress.org/Function_Reference/register_taxonomy

Categories