WordPress Page Templates for Custom Post Type - wordpress

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.

Related

Use custom templates in posts (not pages)

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.

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

Wordpress add property to page

Is it possible to add some kind of "properties" to a Wordpress page? In the admin interface i would like to have a couple of different properties that the page should have to choose from, and then i can "pick up" the value of those properties in my template. Is this possible?
WordPress pages have custom meta values, which can be retrieved in the theme.
In the page editor, you can add meta values by pulling down the "Screen Options" menu at the top-right of the page and checking "Custom Fields". If you do that, a box will show up below the post editor where you can enter custom fields.
You can read more about custom fields (including how to retrieve them from your theme) on the WordPress.org Custom Field documentation page.
http://codex.wordpress.org/Custom_Fields
For example, if you store a "weather" field, you can get it in the theme with something like the following:
<?php
global $post;
echo "Weather: " . get_post_meta($post->ID, 'weather', true);
?>

add custom field template to theme admin page

in WP admin how to add the custom field template plugin to a theme page?
it automatically shows in posts and pages but i want this in the theme page. the theme am using is from iwak "creations" portfolio page.
what files do i need to modify to add this?
It's very hard to say what you need to modify without being able to look at the code. Being a premium theme, we can't just download it and take a look.
Having said that, the theme may use the WordPress custom post type functionality. Search the code for a call to the register_post_type function. If it's used, you may be in luck. Either
add 'custom-fields' to the supports argument in that call, or
call add_post_type_support after the post type is registered. The $post_type parameter will be the first value passed to the register_post_type function, and the $supports parameter will be 'custom-fields'.
Daniel, are you using this Custom Post Type Plugin - http://wordpress.org/extend/plugins/custom-field-template/screenshots/? I've used it before, and the guy who created it is Japanese, so his personal page isn't very useful as far as support for english goes.
I had some trouble with this at first. But what I think you're trying to do is add the custom fields to your new pages you've created, correct?
It's not very straightforward, but once it works it's pretty awesome.
Basically, when you set up the plugin you create these different "Custom fields," right? Well part of that should look like this:
[Custom Field Name]
type = textarea
rows = 4
cols = 40
tinyMCE = true
htmlEditor = true
Ok, so once you've created those "Custom fields" keep note of the part in brackets. You'll add this to your template pages:
<?php getCustomField('Custom Field Name'); ?>
Now when you enter the info in the pages or posts, the content should appear as you've entered it.

Can WordPress post content be external data (not from the database)

I am trying to display a page based on some data returned from an external API (Amazon). This data is formatted then, has to be displayed on a page, created on the fly, based on URL querys. I can already do this with shortcodes but this has to be from the query.
I see all kinds of info in the codex on returning custom query_posts into the loop from the database. However, I cannot find info getting external data to appear on a page.
Is this possible in WordPress? (anything is possible, right?) Just point me to some functions, filters or tutorials please.
If I understand you correctly, you want to retrieve data dynamically and display it in a WordPress page?
There are many ways to do this, but here's one option:
Create a Page Template
Create a WordPress page and use the Page Template created in step 1.
Edit the Page Template to call the external API and display the data
I'm guessing you've been trying to find a way to do this from the "content" of a page or post, but the easiest way is to put the code in a custom Page Template.
UPDATE: If you want to programmatically create a page, this might work for you: http://wordpress.org/support/topic/how-to-create-pages-programmatically?replies=5#post-1230619
http://www.prelovac.com/vladimir/wordpress-shortcode-snippet-to-display-external-files
This is a snippet to display external data in a post.
Would this help? If the dynamic page was a HTML page and THEM displayed in WP.
Yep, Thats possible.
I Did by using bridges.
You can do it by add "add_meta_boxes" action.
Inside the meta box function you can add call and get the external page contents, or can give your own forms .etc
My Code :
/*
* Add Meta Product Type Field to POST
*/
add_action('add_meta_boxes', 'meta_box_product_type_add');
My Meta Box
/*
* Product Type Meta Box Init
*/
function meta_box_product_type_add()
{
add_meta_box('ptype_testing', 'Product Type', 'add_ptype', 'testing', 'normal', 'high');
}
/*
* Product Type Field
*/
function add_ptype()
{ ?>
<label>Type of Product : </label>
<select name="ptype" id="ptypes">
<option>----Select----</option>
<option>Physical</option>
<option>Virtual</option>
</select>
<label>Unit :</label>
<select name="punit" id="units">
<option>----Select----</option>
<option>KG</option>
<option>Mtr</option>
<option>Ltr</option>
</select>
<?php
}
Try It....

Resources