Use custom templates in posts (not pages) - wordpress

Is there a way to allow the user to select a template for a post in the same way you can select one for a page?
I could create a new post type and create a single.php file for that type, but there are several templates to add I it would mean when I am pulling in posts, I would have to add those post types to the feed throughout the site, so I would like to avoid it if possible.

Sure, you can use the same process as for page templates by creating your single file with this header at the top of the file:
<?php
/*
* Template Name: Featured Article
* Template Post Type: post, page, product
*/
get_header(); ?>
Then on the dashboard, you will be able to select the template for your post.

Related

Wordpress custom single post page

so i need to create a custom single post page in wordpress according to a client's specification on a mock up of the page. It's gotten quite difficult trying to customize the single post page of the theme i'm currently using. What can i do to make the single post page look exactly like the one in the mock up because both look way different from each other. This is the mock up of the proposed page
You have to options either you can goto child theme and customize your post template single.php or like example (themes\twentyseventeen\single.php)(themes\twentyseventeen\template-parts\post\content.php) and apply your design there or you can create new template for single post.
New template
<?php
/*
* Template Name: New Template
* Template Post Type: post
*/
get_header(); ?>
// your html design and content of post
// along with the post loop
<?php get_footer(); ?>

WordPress Page Templates for Custom Post Type

I have a custom post type, "Store Pages" for instance, which is an almost identical duplicate of the default Wordpress "Page" post type.
Like the "page" post type, I would like to create page templates (not post-type templates) and be able to select them from the "Template" drop-down within the "Page attributes" box in the page editor.
I have created several templates, but the drop-down menu does not appear; I am assuming this is because a custom post types does not allow support for this.
Is there a way I can create page templates for a custom post type without using "single-{post-type-name}.php" and having a dozen queries to load up different template files?
I have double checked the comments for the templates are correct as they appear when I create a new page (post type, "Page").
Help would be greatly appreciated.
starting 4.7 you can use the new feature described here https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/
// … your code here
If I understood correctly you want a Select template dropdown for your custom post type.
You can do this easily through Advanced Custom Fields, here's a quick guide how to get through.
After installing the plugin you can access the Custom Field section, if you open it and click Add new it bring you to the field editor. Name it whatever you want, it's just for administrative display.
On Field type choose "Select", this will allow you to construct a select box on your backend, keep in mind the value of "Field name" you will need this later on the code.
Scrolling down you can add the values of the select box in the following format: "key value : Textual label" just assume for now you want 2 templates, one for audio posts and one for video posts.
If you keep scrolling down you can see the display rule for this field group, now you will have "post" and "page" by default, when you add different content types you will have the additional content types here to select, just go ahead and pick yours.
And, ta-da. If you go on the custom content type edit window you will find your new fresh select box here waiting for you.
The code integration now is very simple, just go onto your single-{post-type-name}.php template and pull the custom field data into your loop. Then you can use this to use get_template_part() to pull your custom templates.
<?php $template_type = get_field('template'); // This must match with the field name value ?>
<?php if (isset($template_type) && !empty($template_type)): ?>
<?php get_template_part( 'store', $template_type ); ?>
<?php else: ?>
// You should have a fallback for the all the existing posts without template set or if you create a new post without a template.
<?php endif; ?>
In this specific example the pulled template files will be in the format of store-{key-value-of-the-selectbox}.php, of course you can readapt that for you best convenience.

In WordPress how to add post to custom pages:

I am new to WordPress.
suppose i have made pages as follows
home, news, about us
now if i want to create post for news page only
news1,
news2
then posting new post how could i set this settings?
You can create page templates.
Create one for all your pages.
Then, change the template of the page of news to the news template.
In that PHP file now you can do whatever.
For example, for news.php
<?php
/*
Template Name: My Custom Page
*/
//Here comes your PHP code, where create a $WP_Query(); and loop troough on that.

Different templates for Posts in WordPress

I have a created a custom WordPress Theme.
All the Posts have a different layout and Pages have a different layout.
By default for Posts wordpress uses index.php and content.php
By default for Pages wordpress uses page.php and content-page.php
I have created two different posts.
What I want to know is how to display one post in the page template layout and the other post in post template layout?
Is this possible?
Please help.
Yest this is possible by using a Plugin named - Custom post template.
Here is the link to the plugin: http://wordpress.org/plugins/custom-post-template/
No,It is not possible to do.Different Posts is a normal post type called posts.And pages are called Page.
So you can not mess up with pages and posts.Please see wordpress codex templates
http://codex.wordpress.org/Templates
In your template you create a template called template-post.php and template-page.php
At the beginning of the file you need to use
<?php
/**
* Template Name: Page Builder
* Template Post Type: post, page
* #package WordPress
*/
Please note the Template Post Type: post, page.
Now the templates are available for both posts and pages.

wordpress : how to use a custom HTML file to create a new post?

I have generated pages using a custom template by creating a php file in
/wp-content/themes
something like :
<?php
*
* Template Name: Contact Page
*/
?>
<html ..... </html>
and then adding a new page on the dashboard selecting this new template
The problem is i cannot associate tags and categories to each pages.
i would need apparently instead to create posts.
How could i then create posts from a custom html file ?
Use custom post types and create a single-{posttype}.php
I think this will solve your problem.
Custom post types are exactly what you need. Essentially, they're custom content containers and an extensible version of default posts. They can be associated with tags and categories just like normal posts can.
You can find implementation instructions here. :)

Resources