how to add awordpress plugin custom thumbnial - php

i am create a wordpress plugin, this plugin add custompost but this post not see featured image
add_theme_support('post-thumbnails',array('km-lightbox','post'));
add_action( 'init', 'create_posttype_light_box' );
function create_posttype_light_box() {
register_post_type( 'km-lightbox',
array(
'labels' => array(
'name' => __( 'Lightboxs' ),
'singular_name' => __( 'lightbox' )
),
'public' => true,
'supports'=> array('title', 'editor','thumbnail' ),
'has_archive' => true,
'rewrite' => array('slug' => 'lightbox')
)
);
}

Related

Doesn't go to single post page

Created Custom Post Type. The page template for each post is single-speaker.php. But when creating a post and going to its page, it goes to the main page. Can't see page template. And the condition does not work -if(is_singular( 'speaker' )). How can this be fixed?
// create post type
function create_post_type() {
register_post_type( 'speaker',
array(
'labels' => array(
'name' => __( 'Speakers' ),
'singular_name' => __( 'speaker' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'speaker' ),
)
);
register_post_type( 'sessions',
array(
'labels' => array(
'name' => __( 'Sessions' ),
'singular_name' => __( 'sessions' )
),
'public' => true,
'has_archive' => true,
)
);
register_taxonomy(
'positions',
'speaker',
array(
'label' => ( 'Positions' ),
'hierarchical' => true,
)
);
register_taxonomy(
'countries',
'speaker',
array(
'label' => ( 'Countries' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_post_type' );
What you can do first, is to install Query Monitor to check the templates you're landing on and which conditional tags you can use to call this specific template
Try using is_single('speaker') instead
And last, check your loop. Are you calling the main loop or a custom loop for the speaker template
I hope this helps!

How to display custom post for user only in wordpress

I am new in wordpress, i created custom post type and i want this should display only for subscriber only(not for admin ) So how can i do this Here is my current code?
function create_posttype() {
register_post_type( 'UserProject',
array(
'labels' => array(
'name' => __( 'UserProject' ),
'singular_name' => __( 'UserProject' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'UserProject'),
'show_in_rest' => true,
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
if ( current_user_can( 'editor' ) && is_user_logged_in()) {
function book_setup_post_type() {
$args = array(
'public' => true,
'label' => __( 'Books', 'textdomain' ),
'menu_icon' => 'dashicons-book',
);
register_post_type( 'book', $args );
}
add_action( 'init', 'book_setup_post_type' );
}
Book is a custom post type you can write your own code here.
Subscribers have no rights to the dashboard. You can set the condition for editor rule or another rule.

Adding Meta Boxes to Custom Post Type not working

I've tried many things to fix this, I'm fairly new to coding a plugin but I can't seem to get this to work.
I have created a custom post type ("Book") perfectly fine. Now I'm trying to add meta-boxes into it. I can't seem to get the coding to connect to each other properly.
I've tried adding the slugs of each meta box to the "support", I've tried various instances of function and add_meta_box as well as putting different things under "register_meta_box_cb" and nothing I'm doing seems to be working.
Any help is appreciated.
// Register Custom Post Type
function post_type_book() {
$args = array(
'label' => __( 'book', 'author_station' ),
'description' => __( 'Custom Book Entry', 'author_station' ),
'supports' => array('title'),
'taxonomies' => array( 'genres', 'series', 'tags' ),
'register_meta_box_cb' => ('as_add_book' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'menu_position' => 20,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => array('slug' => 'Book'),
'capability_type' => 'page',
);
register_post_type( 'as_book', $args );
}
function as_add_book( $meta_boxes ) {
add_meta_box(
'add_meta_boxes',
array( $this, 'as_add_book_boxes' ));
$types = array('post', 'page', 'book');
if (in_array($types)) {
add_meta_box(
'as_add_book',
'add_meta_boxes',
'Book',
'as_add_book_callback',
$types,
'normal',
'high'
);
}
$prefix = 'as_';
$meta_boxes[] = array(
'id' => 'as_add_book',
'title' => esc_html__( 'Book', 'author_station_book' ),
'pages'=> array('as_book'),
'context' => 'advanced',
'priority' => 'high',
'autosave' => 'false',
'fields' => array(
array(
'id' => $prefix . 'book_cover',
'type' => 'image',
'name' => esc_html__( 'Book Cover', 'author_station_book' ),
),
array(
'id' => $prefix . 'book_title',
'type' => 'text',
'name' => esc_html__( 'Title', 'author_station_book' ),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'as_add_book' );
add_action( 'add_meta_boxes', 'my_custom_meta_box' ) );
function my_custom_meta_box(){
$args = array();
add_meta_box(
'my_metabox_id',
__( 'My Meta Box', 'my_textdomain' ), // Title
'my_callback_function', // Callback function that renders the content of the meta box
'post', // Admin page (or post type) to show the meta box on
'side', // Context where the box is shown on the page
'high', // Priority within that context
$args // Arguments to pass the callback function, if any
);
}
function my_callback_function( $args ){
//The markup for your meta box goes here
}

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

Wordpress Custom Post Type link not showing content

I have created 2 custom post types. One called artist and one called work. When I create a post in artist and view it the link is fine and I can view the content. However in work when I add a new post and go to view it it says that it cannot find the content and the page is missing.
Here is my functions.php code.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'artist',
array(
'labels' => array(
'name' => __( 'Talent' ),
'singular_name' => __( 'talent' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'thumbnail' ),
'supports' => array('thumbnail', 'title', 'editor'),
'rewrite' => array('slug' => 'talent'),
)
);
register_post_type( 'work',
array(
'labels' => array(
'name' => __( 'Content' ),
'singular_name' => __( 'content' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'thumbnail' ),
'supports' => array('thumbnail', 'title', 'editor'),
'rewrite' => array('slug' => 'content'),
)
);
}

Categories