Defining page layout for defualt post formats (aside) - wordpress

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.

Related

Custom post with custom URL

I have created a custom post, and I would like the URL to be:
example.com/directory/profile/slug/
With that in mind, this is a snap shot of how I have set it up:
$args = array(
...
'rewrite' => array( 'slug' => 'directory/profile' ),
'supports' => array( 'title', 'thumbnail' ),
);
I have created a corresponding file:
single-profile.php
If I remove the directory/ from the rewrite parameter, the new post shows up at this URL:
example.com/profile/slug/
I have looked at adding rewrite rules but either thats not the right approach or I am not doing that correctly.
Example:
add_rewrite_rule(
'^directory/profile/([^/]+)/?$',
'index.php?pagename=$matches[1]&post_type=profile',
'top'
);
What extra steps do I need to get this to work as intended?
An easy way to do it is: go to settings > permalinks, select Custom and set the value to /%postname%/.
No need for the rewrite rules.
Keep the post type slug as directory/profile.
Note that this will also change the default posts URLs to /postname. If you want to apply this change only for the post type, you can use this plugin, making the change above only for this post type.
Or do it on the code using the post_type_link hook, also works.

Wordpress: Create a custom page through widget

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.

Custom post type multilingual with Polylang

I have this issue that i do not now how to solve. We have a wordpress page that is multilingual thanks to Polylang plugin. Everything works great except custom post type from other plugin. Post created with this custom post type loads content for Base language which is english, but whenever we change the language it stops working. It does not load content
I registered post type like this:
register_post_type( 'placement',
array(
'labels' => array(
'name' => __( 'Placementy' ),
'singular_name' => __( 'Placement' )
),
'public' => true,
'menu_icon' => 'dashicons-welcome-write-blog'
)
);
I tried, registering post type in my functions.php instead of plugins main file, still the same... Other custom post types are working great and they are registered the same way. I also tried creating template for custom post type in my wordpress theme instead of using the one from plugin, but it also failed... Do not really know why it is not working. Especially when everything else works. Also cleared cache, after changes. Checked changed in developer and local environment. What can cause such behavior?
in functions.php
add_filter('pll_get_post_types', 'add_cpt_to_pll', 10, 2);
function add_cpt_to_pll($post_types, $hide) {
if ($hide)
// hides 'my_cpt' from the list of custom post types in Polylang settings
unset($post_types['my_cpt']);
else
// enables language and translation management for 'my_cpt'
$post_types['my_cpt'] = 'my_cpt';
return $post_types;
}
for more details
https://polylang.wordpress.com/documentation/documentation-for-developers/filter-reference/

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. :)

wordpress create custom post type that pulls custom post types

I'm customizing the admin on a new WP site, and I am creating an over-arching custom post type, that will allow the admin to create pages for the site. I want them to be able to pull values from other custom post types and set them here. I'm struggling to find good documentation on how to do this though. I can create multiple custom post types without a problem, just unsure how to pull the values of post_type_y into the meta_options for post_type_x.
Any and all help is appreciated!
Since I can't 'comment' on your question, I'll do my best to answer it as I understand it..
There's a plugin posts to posts that will allow you to associate Post 1 with Post 2. It's a little clunky, but gets the job done.
If you're looking to associate tags, categories or whatever between multiple post-types, I prefer using custom taxonomies as they are relatively easy to implement.
Sample Custom Taxonomy:
function languages_init() {
// create a new taxonomy
register_taxonomy(
'languages',
array('post','clients','positions','projects'), // Set various post-types
array(
'label' => __( 'Languages' ),
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'language' )
)
);
}
add_action( 'init', 'languages_init' );

Resources