How to set custom post type as parent of page? - wordpress

I've found plenty of info on how to set a page as the parent of a custom post type, but not the other way round - I want to set a custom post type as the parent of my page.
I've found the following hook, originally intended to add drafts to the list of available parents for a page. It works for adding drafts, but if I use it to chaneg the retrieved post type, the parent pulldown on the page edit screen disappears. Any ideas?
add_filter( 'page_attributes_dropdown_pages_args', 'so_3538267_enable_drafts_parents' );
add_filter( 'quick_edit_dropdown_pages_args', 'so_3538267_enable_drafts_parents' );
function so_3538267_enable_drafts_parents( $args )
{
//$args['post_status'] = 'draft,publish,pending';//works
$args['post_type'] = 'campaign';//custom post type name - doesn't work
return $args;
}
I've got it working. The code above is good, I just needed to set my custom post type to be hierarchical.
Edit:
I spoke too soon. The above change allowed me to select the custom post type as the parent of the page, but now the child page is inaccessible on the frontend. I get a 404 error. I've tried flushing permalinks as usual.
The URL it's trying to use for the page:
http://www.example.com/sample-campaign-6/about-campaign/
the first part is the slug for the parent custom post type.
Can anyone tell me how to get this working?
Thanks.

Related

How to add a child page to a Custom Post Type "Post"

Friends, Hello.
I have an issue, i'm searching for an answer for 2 days now. I think I have checked all the 10 firsts google pages of 20 different requests by now.
So, this is it.
I have a custom post type named "Footballer".
I have created 2 "Posts" of that CPT, which are "Messi" and "Ronaldo".
My permalinks for these 2 footballers are "mysite.com/footballer/messi/" and "mysite.com/footballer/ronaldo/". It is OK for me.
Now, I want to add a child page "about" (or "stats", or anything) to my footballers and I want this page to be available by the link "mysite.com/footballer/messi/about" (just add the child page slug after the cpt post link).
Obviously, I want to create a single page "about", not one for every footballer I have, and in this page I would retrieve data for the specific footballer (by functions/shortcodes).
Nota: the cpt and values in this post are fictionnals, I know I would display the about/stats directly in the footballer cpt post. I am just trying to explain what I need -> a generic child page for a cpt, available by "mysite.com/cpt/cptpost/childpage".
How can I do it ? I s it even possible ?
Thanks for your help.
EDIT ;
As it is not possible with Divi, can I try to rewrite the URL of my subpage ? If my subpage url is "mysite/subpage/?foot=messi", can I make it appear "mysite/footballer/messi/subpage".
I tried this code, without success
function my_rewrite_url() {
add_rewrite_tag( '%foot%','([^&]+)' );
add_rewrite_rule(
'footballer/([^/]+)/subpage',
'index.php?pagename=subpage&foot=$matches[1]',
'top'
);
}
add_action( 'init', 'my_rewrite_url' );
You are looking for custom taxonomies on the custom post type.

Remove sub menus from the admin view for a plugin that registers a custom post type

I know there are 357,982 other posts about this BUT they all kinda lack something ie. an actual example that works for those of us who don't write 4,594,334 line of code every day.
SO - As it stands the scenerio is:
A plugin that registers a custom post type
A sub-menu you want to hide
What next?
The best solution I have found is actually pretty easy and requires a bit of inspection of the source and a good understanding of what to look for.
In this example woocommerce is registering the custom post type 'product' with a sub-menu that appears as 'Product Options'. We want to hide this for non-admin users.
Doing an inspection of the menu items we find that the hyperlink for the parent menu is 'edit.php?post_type=event_ticket' - looking a little further we see that the hyperlink for the sub-menu is 'https://websitename.com/wp-admin/edit.php?post_type=product&page=product_attributes'
We will use the 'add_action' hook as shown below. Please note that we are using the url for the parent menu however we are ONLY using the page parameter for the child.
add_action('admin_menu', 'remove_menu_pages', 999);
function remove_menu_pages()
{
if (current_user_can('manage_options') == false)
{
//1st parameter is parent URL | second is the 'page' parameter from the child url
remove_submenu_page('edit.php?post_type=product', 'product_attributes');
}
}
Add this to your functions.php and then login as a non-admin user and the submenu should now be hidden.

Wordpress template for specific nested page

In Wordpress I have a page with the slug logged-user with a parent page ecadmin, making it accessible under the URL /ecadmin/logged-user/.
Since it has a parent page, I (or any other user) can create a page with the same slug logged-user as long as it isn't nested under the same parent page.
The problem now is that I cannot create a single page template page-logged-user.php in theme's folder, as this template can be potentially applied to any other page named logged user no matter where it belongs hierarchly.
Is there a way to name the template file in such way that is referencing to its parent page(s) as well? E.g. page-ecadmin-logged-user.php
The feature you are hoping for doesn't exist, but there is a quite simple way to accomplish what you want to do:
You can create a custom page template [whatever-you-want].php and place this comment in the header
/*
Template Name: Logged In User
*/
That template will be available in the Page Attributes meta box for any "Page" in Wordpress, but will only be applied to a given page if selected in that meta box.
Note:
All content creators have access to the page attributes meta box by default.
If you are worried about users applying that template inappropriately you could hide the Page Attributes meta box from everyone who isn't an admin:
function remove_post_custom_fields() {
if (!current_user_can( 'create_users' )){
remove_meta_box( 'pageparentdiv' , 'post' , 'normal' );
}
}
add_action( 'admin_menu' , 'remove_post_custom_fields' );
http://www.wpbeginner.com/wp-themes/how-to-create-a-custom-page-in-wordpress/
You could try using using page-$id.php which would be unique to that page.

Wordpress Custom Post type page display

Hi I am reading Building wordpress themes from scratch in order to understand theme development.I got to a point in the book where it explains how to create the custom post type pages.And the author mentions that this function is required in order to display the page:
add_action('init' , 'director_rewrite');
function director_rewrite(){
global $wp_rewrite;
$wp_rewrite->add_permastruct('typename','typename/%year%%postname%/' , true , 1);
add_rewrite_rule('typename/([0-9]{4})/(.+)/?$','index.php?typename=$matches[2]', 'top');
$wp_rewrite->flush_rules();
}
I have deleted the function and the page still display corectly without it.That leads me to belive that I do not understand wha this actualy does.
So is this function required to properly display custom post type pages?If so what does it do?
It adds rewrite rule for the permalinks structure of your page. If you remove it, the page still show correctly, but link towards your page is different!

WordPress - Custom Post Type Entry Won't Load Into A Page

I created a very basic custom post type in my WP site to hold events. Basically, a user can input an event title, some information about the event and the date.
Currently, the only time the events are being displayed are in my sidebar where I just pull the title and date of the upcoming events.
However, I want to be able to allow the user to also click on the title of the link to lead to the event's individual page. I tried previewing my custom post type event and it kept leading me to my 404.php page. The events don't seem to load into a page at all.
I'm hoping this is a small property that I didn't set when I registered my post type. Any help?
Wordpress has incorporated custom post types into the template hierarchy and you have access to them at single-$posttype.php and archive-$posttype.php.
If you create these templates and your posts don't show up in the loop, you need to reset your permalinks/flush your rewrite rules.
As well, it would help to see how you are registering your post types, as in order for your archive-$posttype.php to be available you need to call has_archive => true in your arguments.
What i've done when i needed it:
Add this code in the same function that create your post type:
add_action("template_redirect", array(&$this, 'template_redirect'));
function template_redirect() {
global $wp;
if ($wp->query_vars["post_type"] == "events")
{
include(TEMPLATEPATH . "/events.php");
die();
}
}
Then, just create events.php with the_loop (like single.php)!
Hope it helps
[]'s

Resources