Skip to content

Instantly share code, notes, and snippets.

@doubleedesign
Last active February 24, 2018 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doubleedesign/4f90095db064e406a95d2abaf2deac71 to your computer and use it in GitHub Desktop.
Save doubleedesign/4f90095db064e406a95d2abaf2deac71 to your computer and use it in GitHub Desktop.
WordPress custom post type for addresses, ACF export file of fields, and example markup
<?php
// Register Custom Post Type
function doublee_address_cpt() {
$labels = array(
'name' => 'Addresses',
'singular_name' => 'Address',
'menu_name' => 'Addresses',
'name_admin_bar' => 'Address',
'archives' => 'Address Archives',
'attributes' => 'Item Attributes',
'parent_item_colon' => 'Parent Address:',
'all_items' => 'All Addresses',
'add_new_item' => 'Add New Address',
'add_new' => 'Add New',
'new_item' => 'New Address',
'edit_item' => 'Edit Address',
'update_item' => 'Update Address',
'view_item' => 'View Address',
'view_items' => 'View Addresses',
'search_items' => 'Search Addresses',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
'featured_image' => 'Featured Image',
'set_featured_image' => 'Set featured image',
'remove_featured_image' => 'Remove featured image',
'use_featured_image' => 'Use as featured image',
'insert_into_item' => 'Insert into Address',
'uploaded_to_this_item' => 'Uploaded to this address',
'items_list' => 'Address list',
'items_list_navigation' => 'Address list navigation',
'filter_items_list' => 'Filter address list',
);
$args = array(
'label' => 'Address',
'description' => 'List contact details to be formatted semantically in the theme.',
'labels' => $labels,
'supports' => array( 'title', 'author', 'revisions', 'page-attributes', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-location-alt',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'address', $args );
}
add_action( 'init', 'doublee_address_cpt', 0 );
?>
[
{
"key": "group_585762b879e7d",
"title": "Address fields",
"fields": [
{
"key": "field_585731f63443d",
"label": "Street or postal address",
"name": "street_address",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html"
},
{
"key": "field_585732213443e",
"label": "Suburb",
"name": "suburb",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html"
},
{
"key": "field_5857322c3443f",
"label": "State",
"name": "state",
"type": "select",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"multiple": 0,
"allow_null": 0,
"choices": {
"ACT": "ACT",
"NSW": "NSW",
"NT": "NT",
"QLD": "QLD",
"SA": "SA",
"TAS": "TAS",
"VIC": "VIC",
"WA": "WA"
},
"default_value": [
"VIC"
],
"ui": 0,
"ajax": 0,
"placeholder": "",
"return_format": "value"
},
{
"key": "field_5857328b34440",
"label": "Postcode",
"name": "postcode",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html"
},
{
"key": "field_5857329834441",
"label": "Phone 1",
"name": "phone_1",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html"
},
{
"key": "field_5857329f34442",
"label": "Phone 2",
"name": "phone_2",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html"
},
{
"key": "field_585732a634443",
"label": "Fax",
"name": "fax",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html"
},
{
"key": "field_585732c1280da",
"label": "Email",
"name": "email",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html"
},
{
"key": "field_58f9ac06d2283",
"label": "Email 2",
"name": "email_2",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_58f9ac10d2284",
"label": "Email 3",
"name": "email_3",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "address"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]
<?php
// WP_Query arguments
$args = array(
'post_type' => array( 'address' ),
'post_status' => array( 'publish' ),
'posts_per_page' => '1',
);
// The Query
$address = new WP_Query( $args );
// The Loop
if ( $address->have_posts() ) {
while ( $address->have_posts() ) {
$address->the_post();
?>
<address class="" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
<div class="">
<?php if(get_field('email')) { ?>
<div itemprop="email"><?php the_field('email'); ?></div>
<?php } ?>
<?php if(get_field('email_2')) { ?>
<div itemprop="email"><?php the_field('email_2'); ?></div>
<?php } ?>
<?php if(get_field('email_3')) { ?>
<div itemprop="email"><?php the_field('email_3'); ?></div>
<?php } ?>
</div>
<div class="">
<?php if(get_field('phone_1')) { ?>
<div itemprop="telephone"><?php the_field('phone_1'); ?></div>
<?php } ?>
<?php if(get_field('phone_2')) { ?>
<div itemprop="telephone"><?php the_field('phone_2'); ?></div>
<?php } ?>
<?php if(get_field('fax')) { ?>
<div itemprop="faxNumber"><?php the_field('fax'); ?></div>
<?php } ?>
</div>
</address>
<?php
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment