Wordpress add property to page - wordpress

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);
?>

Related

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.

woo commerce - how to show custom fields on published page

I have set 2 custom fields on the wordpress / woocommerce product page called "imballo" and "sottoimballo" with their values. How do I display these on the published page?
Thanks!
You would have to display the custom fields in the WooCommerce Single Product Page template,
You could do it in two ways,
Add a code in an existing hook of the Product Single page. Check hooks here
Modify the template by overriding it in your WordPress theme. Read here
And in the place where you want to display do the following,
<?php
//Please add your HTML structure as needed.
echo get_post_meta(get_the_ID(), 'imballo');
echo get_post_meta(get_the_ID(), 'sottoimballo');
?>

Woocommerce product form fields

I would like my product page that customers fill 5 text fields and choose 1 field type radio.
So my product page is a form where customers ask a question, fill some information and choose a paid option.
Like this french page => http://www.avocat-bervard.com/paiement-honoraires-consultation-juridique/
Is it possible with Woocommerce ?
If so, can you explain me how ?
Thanks.
You can create nice forms with plugin Ninja Forms :). Its a very cool plugin, with a couple of settings: sending emails, when submitting forms, etc.
It is also supports shortcodes, with this method you can implement your form on every single product page, or even display it in a widget area.
For widget area:
You need to enable shortcodes first in functions.php
add_filter('widget_text', 'do_shortcode');
Then call the shortcode in Appearence - Widgets
For template:
You need to edit the file where you want this form to display, single-product.php, meta.php :) etc.
To insert shortcode in template file you need to use the below code:
<?php echo do_shortcode( $content ) ?>

Wordpress custom fields content don't appear when searched

I'm using the plugin Advanced Custom Fields on a custom post type. When I enter in content into the custom fields and publish, all looks fine BUT when trying to search for words that are in a custom field it doesn't show up...
I really need this content to be searchable.
I'm displaying the custom fields using this code
<?php echo get_post_meta($post->ID, 'course-code', true); ?>
Can anyone help please.
Post meta is not included in WordPress default search. You could use a plugin like Relevanssi, which extends WP's search functionality.

WP-ecommerce Custom fields

I have a wordpress e-commerce site with a plug-in installed Custom fields Version 1.4.5...
I added a new attribute called size and its a drop-down menu. it only shows up in the admin pages when I goto edit a product, but in wpsc-single_product.php it does not appear just one line of text. How do i get the drop-down to display on wpsc-single_product.php?
You can add fields manually to your theme by adding the following line:
<?php if( function_exists( 'wpsc_the_custom_fields' ) ) wpsc_the_custom_fields( 'slug=' ); ?>
Make sure to append your custom field slug to "slug="
e.g. 'slug=my-dropdown'

Resources