I use "Custom Post Type UI" to create my CPT.
I need to create a CPT "homepage" to admin just the homepage.
I remember that the plugin "magic fields" allowed to create a cpt for a single page, so it must be possible to do it also without plugin or with "Custom Post Type UI" but how ?
mod mi answers:
Just add this to your functions.php
The function will check which page is on front and link to the page edit-page that is set on front
if ( ! function_exists( 'toplevel_admin_menu_pages' ) ) {
function toplevel_admin_menu_pages(){
$home_id = get_option( 'page_on_front' );
add_menu_page( 'Home', 'Home', 'edit_posts', 'post.php?post='.$home_id.'&action=edit', '', 'dashicons-editor-help', 6 );
}
add_action( 'admin_menu', 'toplevel_admin_menu_pages' );
}
Update:
Work Around with ACF theme options page and adding the fields with ACF.
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Homepage',
'menu_title' => 'Homepage',
'menu_slug' => 'theme-options-settings',
'capability' => 'edit_posts',
'redirect' => true
));
acf_add_options_sub_page(array(
'page_title' => 'Edit Homepage',
'menu_title' => 'Edit Homepage',
'parent_slug' => 'theme-options-settings',
));
}
You can create your fields with ACF and set them to appear in the Options "Homepage" page an then display them in the index.php template with simple get_field ACF functions, just like you would do with a custom post type
Sébastien | French WordpressDesigner comments:
I could create an option page. But I do not want. Creating a cpt is faster. And that's enough for me.
Then yes I need a simple page to show as the homepage of my site and appear in the admin menu as an item
mod mi comments:
Please check my updated answer
mod mi comments:
Please check my updated answer
Sébastien | French WordpressDesigner comments:
About your code : if my client remove the page 222, what will happen ?
mod mi comments:
I have updated my answer so that the function first checks which page is set on front
Sébastien | French WordpressDesigner comments:
that doesn't change the problem... if my client remove the page in the menu "homepage", what will happen ? He will create a new homepage, with a new ID, and this ID is no more in the settings of the frontpage.
mod mi comments:
With the approach of a front page I don't think there is much else you can do with the "page" solution. I have updated my answer with the creation of theme options via ACF (you mention above that you will use it to add fields etc.) I recommend you to use this solution which is 100% safe that the editor won;t mess around with page deletions etc
Sébastien | French WordpressDesigner comments:
I try your code... It's not really what I need... It's not good for the UX.
An item is created in the menu but that's jut a link to the administration of a page (for example the item highlighted is "page" and no "home"...). And if this page is removed, all is broken because the setting of the frontpage are no more correct.
mod mi comments:
Yes it;s really limited functionality, since with the custom post type as "page" as it was with the magic fields first version it really complicates things. Please see my update on the answer. An options page is the best and simpler solution.
Sébastien | French WordpressDesigner comments:
That seems very interesting but when I create a group of fields in ACF that doesn't appears in the administration page of "homepage"
Sébastien | French WordpressDesigner comments:
See the attachment
mod mi comments:
In ACF Rules menu at the first drop down under "Show the field group if" you have to scroll down to the Forms list and Choose Options Page and then select Homepage
Sébastien | French WordpressDesigner comments:
yeaaaaaaah, coool !
Sébastien | French WordpressDesigner comments:
mod mi : send me an email to [email protected]
Just to have your email adress please...
Farid answers:
Do you want to display it only on Homepage?
Sébastien | French WordpressDesigner comments:
yes but that's not my problem.
Farid comments:
Or you want that 'homepage' CPT to be managed by users with only admin role?
Please explain a little more.
Sébastien | French WordpressDesigner comments:
OK :)
I want to create a CPT for administrate just one page.
See the attachment please.
Reigel Gallarde answers:
you mean, you want to create CPT "homepage". But only to exist for admin users?
Sébastien | French WordpressDesigner comments:
huuum... no.... I want to create a CPT for administrate just one page.
See the attachment please.
Reigel Gallarde comments:
That is not CPT... but an Option page...
Here's one tutorial I found on google...
https://wpshout.com/wordpress-options-page/
Reigel Gallarde comments:
From the link above, this is a sample of the output..
Sébastien | French WordpressDesigner comments:
I could create an option page. But I don't want. Creating a cpt is faster. And that's enough for me.
Then yes I need a simple page to show as the homepage of my site and appear in the admin menu as an item.
In this page, I will create different fields with ACF
Francisco Javier Carazo Gil answers:
Hell,
Why you need a CPT to homepage? You want to show in homepage the archive of this CPT?
Sébastien | French WordpressDesigner comments:
Because it's more "beautiful" for my client :)
Francisco Javier Carazo Gil comments:
Ok and how do you make a CPT single a homepage?
Could be maybe more useful create a menu with a single entry that points to edit a page (the homepage).
Sébastien | French WordpressDesigner comments:
Ok and how do you make a CPT single a homepage?
-> that's exactly my question :))
Could be maybe more useful create a menu with a single entry that points to edit a page (the homepage).
-> Absolutely, it's what I need. I speak about CPT, but what is important for me is just to have the item "homepage" in the menu.
Francisco Javier Carazo Gil comments:
You can create the menu in the usual way and something like it to point to the homepage:
add_submenu_page(
'parent_slug',
'Edit My Special Post', // page title, change to liking
'Edit SP', // menu title, change to liking
'edit_posts', // required capability, change to liking
'post.php?post=9999&action=edit' // menu slug, change post ID
);
Sébastien | French WordpressDesigner comments:
huuummmm... this is not to administrate the homepage. This is to administrate the post with the ID 9999.
In this case if my client remove this post 9999, he can no more administrate the homepage...
Francisco Javier Carazo Gil comments:
Sébastien,
yes, this is an example.
You can get the correct ID here $frontpage_id = get_option( 'page_on_front' );
Sébastien | French WordpressDesigner comments:
But how do you know all that ?! :))
Could you write all the code, please ? Including the creation of the "homepage" element in the WP menu.
Francisco Javier Carazo Gil comments:
Sorry but I have no time and for 10€ I would have to use more time.
Sébastien | French WordpressDesigner comments:
I need just the code to create the top-level menu page "homepage".
mod mi comments:
Please check my answer
Francisco Javier Carazo Gil comments:
Something like it:
<?php
function theme_options_panel(){
add_menu_page('Homepage', 'Homepage', 'manage_options', 'homepage-menu-options', 'homepage_func');
add_submenu_page( 'homepage-menu-options', 'Edit homepage', 'Edit homepage', 'manage_options', 'homepage-menu-options', 'homepage_submenu_func');
add_submenu_page(
'homepage-menu-options',
'Edit homepage', // page title, change to liking
'Edit homepage', // menu title, change to liking
'manage_options', // required capability, change to liking
'post.php?post=' . get_option( 'page_on_front' ) .'&action=edit' // menu slug, change post ID
);
}
add_action('admin_menu', 'theme_options_panel');
Sébastien | French WordpressDesigner comments:
huuum... I try your code... It's not really what I need... It's not good for the UX.
An item is created in the menu but that's jut a link to the administration of a page (for example the item highlighted is "page" and no "homepage"...). And if this page is removed, all is broken because the setting of the frontpage are no more correct.
Do you understand ?
Francisco Javier Carazo Gil comments:
Sébastien,
Yes I understand, but $10 is not enough.