Wordpress: Create a custom page through widget - wordpress

so I am working on a website for a friend right now. Right now I'm trying to figure out a good way to build a customizable staff section. So far I just created a custom template with a widget area, wrote a simple widget with name, age etc.
This works pretty decent so far. Now I'd like to have an extra "about me" page for each staff member.
Can I create a custom page somehow through a widget? Or would there be a better way of doing it?

Your best bet is to use custom post types. Just like pages and regular posts you can create your own type of posts. You can create a 'staff' post type. This will add an extra menu item in your Wordpress backend for staff members. There are also multiple plugins available to create post types without using code, but if you can code I would recommend do it yourself. Simply put the following code in your theme's functions.php:
function create_staff_post_type() {
register_post_type( 'staff',
array(
'labels' => array(
'name' => __( 'Staff' ),
'singular_name' => __( 'Staff member' )
),
'public' => true,
'has_archive' => true,
'supports' => [
'title', 'editor', 'author', 'thumbnail',
'excerpt', 'trackbacks', 'custom-fields',
'comments', 'revisions', 'page-attributes',
'post-formats'
]
)
);
}
add_action( 'init', 'create_staff_post_type' );
After creating the post type you should reset the friendly URL's. You can do that by going to settings->permalinks and click the 'save' button (no need to change any option). This will make sure your new mysite.com/staff URL works.
This page will use your default archive page (or, if you don't have an archive page your index.php file) to generate the overview. If you want a customized template, simply create archive-staff.php and create your custom loop there. Same goes for the detail page, single-staff.php will give you a template for just this specific page.

Related

Adding Custom Fields to a Custom Post Type, the Right Way

i'm create a custom post type and i also add a custom field for add something. so, my problem is that "How can i show this field value in my website" ?
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
i already tried this one. but it's not working.
<?= get_field( 'field_name' ); ?>
The correct function is get_post_meta(). See this docs:
https://developer.wordpress.org/reference/functions/get_post_meta/
Your get_field is a specific function for Advanced Custom Fields plugin. It's only available when you install it.
Each custom fields has its own helper functions to get and display field values. For example, Meta Box uses rwmb_meta function.
If you use any of them, be sure that follow their documentation.

Defining page layout for defualt post formats (aside)

single-aside.php defined but still using single.php for my post that having aside format.
Hi i used post-formats(aside) for some of my post and using single.php for styling them in the past despite defining a new post-type , the problem is now i want to use another page layout diffrent than my other posts and pages just for this aside type , but when i create single-aside.php wordpress doesnt use this page , and i flushed away my permalinks thats not the problem. do you think single page is defineable for defualt post formats like aside, video,quote and... ? any idea?
As per documentation you can use has_post_format() see more here https://codex.wordpress.org/Post_Formats under Using Formats.
Since the post formats aren't custom post types, you would need to add it to single.php:
if ( has_post_format( 'aside' )) {
//Your custom code for aside goes here
}
Alternatively, if you would like to use a different php file, you can create your custom post type and give support for Post formats.
Register with support:
add_action( 'init', 'aside_post_type' );
function aside_post_type() {
register_post_type( 'aside',
array(
'labels' => array( 'name' => __( 'Aside' ) ),
'public' => true,
'supports' => array('title', 'editor', 'post-formats') //this will give support for post formats
)
);
}
Then add single-aside.php to your theme and style this custom post type as needed.

How can I make a custom post type be a pages parent in WordPress?

I'm looking to make a series of pages have a parent of a custom post type I created. For Example, My custom post type "Hospitals" may have an entry of "Some Hospital Name" that entry may have several pages that have generic page content like "contact us". I need this contact us page to have a slug of "website.com/some-hospital-name/contact-us" currently its "website.com/contact-us".
When you initialize your custom post type, you can specify it as 'hierarchical' as part of its $args. If 'hierarchical' is set to true, you also have to specify 'page-attributes' in the 'supports' argument in order to show the parent select box in the editor.
$args = array(
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'page-attriutes' )
);
See the register_post_type reference in the WordPress Codex: https://codex.wordpress.org/Function_Reference/register_post_type

Wordpress theme doesn't show set featured image button in custom post types

I am using the Gonzo Wordpress theme and I've noticed that in the custom post types I created is missing the button to add an image to the post.
In functions.php the support for thumbnails was activated:
add_theme_support('post-thumbnails');
I tried adding an array of custom post types:
add_theme_support('post-thumbnails', array('post', 'page', 'article', 'cpt1', 'cpt2', 'cpt3'));
But it didn't change anything.
And when I register a custom post type there is the support for thumbnails:
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
Where can it be the problem?
Moving this line
add_theme_support('post-thumbnails');
to the bottom of the functions.php file actually solves the issue.
I'm not sure this is really a solution. Does it make sens putting it on the bottom? Is there maybe some parameter to pass to this or other function to change the execution order?

Wordpress - adding custom post type category to menu

I have a custom post type named 'The Books', and a relative category named 'The Books' for these custom posts.
When I add the category The Posts to my nav menu, it doesn't work because it goes to the URL /category/the-books instead of just going to /the-books. If I posted this in the default post section it shows correctly, but when I post in the custom post section it does not return my post. I can, of course, add individual posts from my custom post section to the nav menu, but can't figure out how to add an archive page of the custom posts.
My permalinks are set to: URL/%postname%/ so I'm not sure why that is happening.
Here's the function for my custom posts:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'hpl_books',
array(
'labels' => array(
'name' => __( 'The Books' ),
'singular_name' => __( 'Book' )
),
'taxonomies' => array('category'),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'the-books'),
)
);
}
Any advice is greatly appreciated.
thanks!
You shouldn't need to add a category "the-books" in order to display the results.
Have you created a view in your page-templates directory called "archive-hpl_books.php?" That's the file WordPress will look for to display the archive of your custom post type. Basically, you would create a page called "the-books" or whatever, then set archive-hpl_books.php as the template.
See http://codex.wordpress.org/Template_Hierarchy
I'm only responding to this because I just went through a similar issue, so I'm down to help out. :)

Resources