WordPress tutorial for, How to create wordpress custom post type permalink structure. Here we creating the Product post type with permalink. we given code.
How to create wordpress custom post type permalink structure
Please open the functions.php file and put following code in that file.
04
function
productposttype_with_custom_permalinks() {
05
register_post_type(
'product'
,
array
(
06
'label'
=> __(
'My product'
),
07
'singular_label'
=> __(
'product'
),
09
'capability_type'
=>
'post'
,
10
'hierarchical'
=> false,
11
"supports"
=>
array
(
"title"
,
"editor"
,
"thumbnail"
,
"author"
,
"comments"
),
12
'taxonomies'
=>
array
(
'category'
,
'post_tag'
),
13
'rewrite'
=>
array
(
'slug'
=>
'product'
),
19
add_rewrite_tag(
'%product%'
,
'([^/]+)'
);
20
$extra_post_types
= get_post_types(
array
(
'_builtin'
=> false,
'publicly_queryable'
=> true));
21
if
(
empty
(
$extra_post_types
))
23
add_rewrite_tag(
'%post_type%'
,
'('
. implode(
'|'
,
$extra_post_types
) .
')'
);
24
add_permastruct(
'product'
,
'/%post_type%/%year%/%monthnum%/%day%/%product%/'
, true, 1);
30
add_action(
'init'
,
'productposttype_with_custom_permalinks'
);
36
function
productposttype_with_product_permalink(
$link
,
$post
,
$leavename
,
$sample
){
37
if
(
'product'
!=
$post
->post_type )
46
$leavename
?
''
:
'%postname%'
,
49
$leavename
?
''
:
'%pagename%'
,
50
$leavename
?
''
:
'%product%'
,
52
$unixtime
=
strtotime
(
$post
->post_date);
53
$date
=
explode
(
' '
,
date
(
'Y m d H i s'
,
$unixtime
));
54
$replace_array
=
array
(
67
$path
=
str_replace
(
$rewritecode
,
$replace_array
,
$link
);
71
add_action(
'post_type_link'
,
'productposttype_with_product_permalink'
, 10, 4 );
If you have any issues or problem with permalink then please write to me.
How to create wordpress custom post type permalink structure
very helpful for creating the custom post type