-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-directory.php
More file actions
224 lines (197 loc) · 8.69 KB
/
Copy pathsingle-directory.php
File metadata and controls
224 lines (197 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Generic Single Template for Directory Types
*
* Displays individual directory item details with a modern contact card design.
* Used for all directory types selected in Theme Settings > Directory Types.
*
* FIELDS DISPLAYED (create these in ACF → Field Groups):
* - name: Primary name (used in header and card title)
* - subtitle OR health_service_name: Secondary text (appended to title with dash)
* - city, state, zip_code, address: Location info
* - opening_hours: Business hours (group/repeater field)
* - website: Website URL
* - main_phone OR phone: Phone number
* - fax_phone OR fax: Fax number
* - email: Email address
* - image: Featured image (or use Featured Image, or Default from Theme Settings)
*
* CARD TITLE FORMAT: "Name - Subtitle" (e.g. "Javier - Addiction and substance use care")
*/
// Get the current post type
$current_post_type = get_post_type();
// Get directory type configuration
$directory_config = one_get_directory_type($current_post_type);
// Set defaults if no config found
$singular_name = $directory_config['singular_name'] ?? ucwords(str_replace('-', ' ', $current_post_type));
$plural_name = $directory_config['plural_name'] ?? ucwords(str_replace('-', ' ', $current_post_type)) . 's';
$item_label = $directory_config['item_label'] ?? $singular_name;
// Redirect users if not viewing a valid directory post type
if (!one_is_directory_type($current_post_type)) {
wp_redirect(home_url());
exit;
}
// Get field values
$name = get_field('name') ?: get_the_title();
$subtitle = get_field('subtitle') ?: get_field('health_service_name');
$city = get_field('city');
$state = get_field('state');
$zip_code = get_field('zip_code');
$address = get_field('address');
$website = get_field('website');
$phone = get_field('main_phone') ?: get_field('phone');
$fax = get_field('fax_phone') ?: get_field('fax');
$email = get_field('email');
$opening_hours = get_field('opening_hours');
// Build display title
$display_title = $name . ($subtitle ? " - {$subtitle}" : "");
// Build location string
$location_parts = array_filter([$city, $state]);
$location = implode(', ', $location_parts);
if ($zip_code) $location .= " {$zip_code}";
if ($address) $location = $address . ($location ? ", {$location}" : "");
// Get image URL
$image_url = '';
// Check for URL-based image fields
$url_fields = ['directory_image_url', 'image_url', $current_post_type . '_image_url'];
foreach ($url_fields as $field_name) {
$url = get_field($field_name);
if ($url && is_string($url)) {
$image_url = $url;
break;
}
}
// Check for ACF image field (returns array or ID)
if (!$image_url) {
$image_fields = ['image', 'directory_image', $current_post_type . '_image'];
foreach ($image_fields as $field_name) {
$image = get_field($field_name);
if ($image) {
if (is_array($image) && isset($image['url'])) {
$image_url = $image['url'];
break;
} elseif (is_numeric($image)) {
$image_url = wp_get_attachment_url($image);
break;
} elseif (is_string($image) && filter_var($image, FILTER_VALIDATE_URL)) {
$image_url = $image;
break;
}
}
}
}
// Try featured image
if (!$image_url && has_post_thumbnail()) {
$image_url = get_the_post_thumbnail_url(get_the_ID(), 'medium');
}
// Fallback to default image for this post type from Theme Settings
if (!$image_url) {
$default_images = get_field('directory_default_images', 'option');
if ($default_images && is_array($default_images)) {
foreach ($default_images as $row) {
if (!empty($row['post_type']) && $row['post_type'] === $current_post_type && !empty($row['image'])) {
$image_url = $row['image'];
break;
}
}
}
}
// Final fallback - placeholder
if (empty($image_url)) {
$image_url = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 200'%3E%3Crect width='200' height='200' fill='%23E8F4FD'/%3E%3Ctext x='100' y='105' font-family='Arial' font-size='12' fill='%233172C1' text-anchor='middle'%3ENo Image%3C/text%3E%3C/svg%3E";
}
get_header();
?>
<section class="org-header">
<div class="c">
<?php get_template_part('template-parts/components/breadcrumbs'); ?>
<h1><?php echo esc_html($name); ?></h1>
<span class="org-label"><?php echo esc_html($item_label); ?></span>
</div>
</section>
<main>
<div class="c">
<!-- Modern Contact Card -->
<div class="directory-card">
<div class="directory-card-image">
<img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($singular_name); ?>">
</div>
<div class="directory-card-content">
<h2><?php echo esc_html($display_title); ?></h2>
<?php if ($location): ?>
<div class="directory-card-location">
<i class="fa-solid fa-location-dot"></i>
<span><?php echo esc_html($location); ?></span>
</div>
<?php endif; ?>
<?php if (!empty($opening_hours) && isset($opening_hours[0])): ?>
<div class="directory-card-hours">
<h3>Opening Hours</h3>
<ul>
<?php foreach ($opening_hours[0] as $day => $hours):
$short_day = substr(ucfirst($day), 0, 3);
$hours = str_ireplace(['AM', 'PM'], ['am', 'pm'], $hours);
?>
<li><strong><?php echo $short_day; ?></strong> <?php echo esc_html($hours); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<div class="directory-card-contact">
<?php if ($website): ?>
<div class="directory-card-contact-item">
<div class="icon"><i class="fa-solid fa-globe"></i></div>
<div class="info">
<span class="label">Website</span>
<a href="<?php echo esc_url($website); ?>" target="_blank" rel="noopener"><?php echo esc_html(preg_replace('#^https?://#', '', rtrim($website, '/'))); ?></a>
</div>
</div>
<?php endif; ?>
<?php if ($phone): ?>
<div class="directory-card-contact-item">
<div class="icon"><i class="fa-solid fa-phone"></i></div>
<div class="info">
<span class="label">Phone</span>
<a href="tel:<?php echo esc_attr(preg_replace('/[^0-9+]/', '', $phone)); ?>"><?php echo esc_html($phone); ?></a>
</div>
</div>
<?php endif; ?>
<?php if ($fax): ?>
<div class="directory-card-contact-item">
<div class="icon"><i class="fa-solid fa-fax"></i></div>
<div class="info">
<span class="label">Fax</span>
<a href="tel:<?php echo esc_attr(preg_replace('/[^0-9+]/', '', $fax)); ?>"><?php echo esc_html($fax); ?></a>
</div>
</div>
<?php endif; ?>
<?php if ($email): ?>
<div class="directory-card-contact-item">
<div class="icon"><i class="fa-solid fa-envelope"></i></div>
<div class="info">
<span class="label">Email</span>
<a href="mailto:<?php echo esc_attr($email); ?>"><?php echo esc_html($email); ?></a>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php
// Display post content if available
$content = get_the_content();
if ($content): ?>
<div class="directory-about">
<h3>About</h3>
<?php echo apply_filters('the_content', $content); ?>
</div>
<?php endif; ?>
<!-- Back to archive link -->
<div class="directory-navigation">
<a href="<?php echo esc_url(get_post_type_archive_link($current_post_type)); ?>" class="btn btn-outline">
← View All <?php echo esc_html($plural_name); ?>
</a>
</div>
</div>
</main>
<?php get_footer(); ?>