i have posttypes.php file in which i register post types
<?php
add_action('init', 'lovetocreateelevationprojects_init_posttypes');
function lovetocreateelevationprojects_init_posttypes()
{
$projects_args = array(
'public' => true,
'public_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'
),
'has_archieve' => true
);
register_post_type('projects', $projects_args);
}
I have new post type - gallery and this post type is visible in wordpress administration panel. I created new archive-projects.php file and everything what is inside so far is:
<?php
echo 'its working';
The file seems not to be working because when i enter http://localhost/webname/projects/ path it redirects me to index.php and i dont have 'its working' message.
I use my own link format: http://localhost/webname/%category%/%postname%/
I think every thing is ok but rewrite and query_var correctly to be accessible from url.
<?php
add_action('init', 'lovetocreateelevationprojects_init_posttypes');
function lovetocreateelevationprojects_init_posttypes()
{
$projects_args = array(
'public' => true,
'public_queryable' => true,
'show_ui' => true,
'query_var' => 'projects',
'rewrite' => ['slug'=>'project'],
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'
),
'has_archive' => true
);
register_post_type('projects', $projects_args);
}
Related
I'm trying to get custom post types, but I can not seem to find any solution. The WP REST API documentation only returns blog posts.
Note: I'm using the Dooplay theme
The code below is in the file tipo.php
directory: inc > includes > series
if( ! function_exists( 'doo_series' ) ) {
function doo_series() {
$labels = array(
'name' => _x('TV Shows', 'Post Type General Name','mtms'),
'singular_name' => _x('TV Show', 'Post Type Singular Name','mtms'),
'menu_name' => __d('TV Shows %%PENDING_COUNT_TV%%'),
'name_admin_bar' => __d('TV Shows'),
'all_items' => __d('TV Shows'),
);
$rewrite = array(
'slug' => get_option('dt_tvshows_slug','tvshows'),
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __d('TV Show'),
'description' => __d('TV series manage'),
'labels' => $labels,
'supports' => array('title', 'editor','comments','thumbnail','author'),
'taxonomies' => array('genres'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-view-site',
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type('tvshows', $args );
}
add_action('init', 'doo_series', 0 );
get_template_part('inc/includes/series/metabox');
}
Compare with
register_post_type( 'post', array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
),
'public' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'menu_position' => 5,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
'show_in_rest' => true,
'rest_base' => 'posts',
'rest_controller_class' => 'WP_REST_Posts_Controller',
) );
which registers the built in post type post. Observe the last three fields. These fields must be set to make the post type accessible via REST.
ADDENDUM
Actually only 'show_in_rest' is required as WordPress has default values for the other two. Also, if a developer wrote his own WP_REST_Controller for the custom post type instead of using the builtin WP_REST_Posts_Controller then even this is not required as his own WP_REST_Controller can call the register_rest_route() functions by some other means.
This question has been asked before but I couldnt find an answer for my situation.
I have some existing posts which needs to be changed to a new CPT .
when I change to the new CPT , the url is also updated. how can I prevent this ?
Also I do want the slug to appear for any new pages that I create in future for the CPT .
$args = array(
'labels' => $labels,
'label' => 'Custom Page',
'description' => 'Custom Page',
'menu_icon' => 'dashicons-smartphone',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'custom',
'with_front' => false
),
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'excerpt', 'page-attributes'),
);
register_post_type( 'custom_page', $args );
I use a neat plugin Post Type Switcher.
What this plugin would do is switch any post type to any post type with seconds i think you can always use this plugin and delete this plugin.. give it a try
Seen a bunch or responses that talk about post ID, and menus... but this is a brand new blog. I am building a plugin, but when I register a custom post type I get this error...
Catchable fatal error: Object of class WP_Post could not be converted to string in /wp-includes/formatting.php on line 1280
register_post_type('car_post', array(
'labels' => array(
'name' => 'Car Content'
),
'singular_label' => 'Car Content',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'show_in_menu' => 'car-post',
'rewrite' => array("slug" => "car_post"),
'supports' => array( 'title', 'editor', 'author', 'revisions', 'comments' )
)
);
Anyone have an idea on what could possibly be causing that? If I comment out the custom post block... then no errors are thrown.
Custom post types are registered this way.
<?php
add_action( 'init', 'my_post_type' );
function my_post_type() {
register_post_type('car_post', array(
'labels' => array(
'name' => 'Car Content'
),
'singular_label' => 'Car Content',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'show_in_menu' => 'car-post',
'rewrite' => array("slug" => "car_post"),
'supports' => array( 'title', 'editor', 'author', 'revisions', 'comments' )
)
);
}
?>
I have created new post type in my theme but when i try to get to the permalink for the post, it give me that the post not found
function my_post_type_news() {
register_post_type( 'news',
array(
'label' => __('News'),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'menu_position' => 5,
'rewrite' => array(
'slug' => 'news',
'with_front' => FALSE,
),
'supports' => array(
'title',
'thumbnail',
'editor',
'excerpt')
)
);
register_taxonomy('news_category', 'news', array('hierarchical' => true, 'label' => 'News Categories', 'singular_name' => 'Category', "rewrite" => true, "query_var" => true));
}
add_action('init', 'my_post_type_news');
function my_post_type_news() {
register_post_type( 'news',
array(
'label' => __('News'),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'menu_position' => 5,
'rewrite' =>false,
'supports' => array(
'title',
'thumbnail',
'editor',
'excerpt')
)
);
register_taxonomy('news_category', 'news', array('hierarchical' => true, 'label' => 'News Categories', 'singular_name' => 'Category', "rewrite" => true, "query_var" => true));
}
add_action('init', 'my_post_type_news');
Use above function, it will works. I have changed rewrite as false.
Hope it helps.
I have made a new section for posts on my wordpress site, but I am having trouble rewriting the URL's as they are all messed up right now, here is my code, can anyone push me in the right direction?
register_post_type('services', array(
'label' => __('Services'),
'singular_label' => __('Services'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' )
));
Right now my URL currently looks like this...
http://examplesite.com/?post_type=services&p=52
and I want it to look like this
http://examplesite.com/services/postname
Can anyone help me?!
Change 'rewrite' => false to 'rewrite' => true!
Or you could be more explicit:
'rewrite' => array ('slug' => 'services'),