relevant-resources.php
3.71 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
<?php
/**
* Relevant Resources Block Template.
*
* @param array $block The block settings and attributes.
* @param string $content The block inner HTML (empty).
* @param bool $is_preview True during AJAX preview.
* @param (int|string) $post_id The post ID this block is saved to.
*/
// Create id attribute allowing for custom "anchor" value.
$id = 'relevant-resources-' . $block['id'];
if( !empty($block['anchor']) ) {
$id = $block['anchor'];
}
// Create class attribute allowing for custom "className" and "align" values.
$className = 'relevant-resources';
if( !empty($block['className']) ) {
$className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
$className .= ' align' . $block['align'];
}
if( $is_preview ) {
$className .= ' is-admin';
}
$category_slug = get_field( "category" );
$title = get_field( "title" );
?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?> ">
<h2><?php echo $title; ?></h2>
<?php $args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'categories',
'terms' => $category_slug, // term id
'field' => 'slug',
)
)
);
if($category_slug == ""){
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
);
}
$attachments = get_posts($args);
$args = array(
'post_type' => 'documents',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'categories',
'terms' => $category_slug, // term id
'field' => 'slug',
)
)
);
if($category_slug == ""){
$args = array(
'post_type' => 'documents',
'posts_per_page' => -1,
);
}
$documents = get_posts($args);
$posts = array_merge($documents, $attachments);
echo '<table id="relevant-resources"><thead style="display:none;"><tr><th>file</th></tr></thead><tbody>';
foreach($posts as $post ) {
$post_type = get_post_type( $post->ID );
$link = wp_get_attachment_url( $post->ID );
$description = get_post_meta($post->ID, 'description', true );
$terms = wp_get_post_terms( $post->ID, 'document-format');
$imgid = get_post_thumbnail_id($post->ID);
if($imgid) {
$image = wp_get_attachment_image_src($imgid, 'medium');
$image = $image[0];
$image_alt = get_post_meta($imgid, '_wp_attachment_image_alt', TRUE);
}else{
$image = wp_get_attachment_image_src($post->ID, 'medium');
if(isset($image[0])){
$image = $image[0];
}
$image_alt = get_post_meta($post->ID, '_wp_attachment_image_alt', TRUE);
}
$term_list = array();
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_list = $term->name;
}
}
if($term_list == 'PDF'){
$image ="";
}
if(is_array($term_list)){
$term_list = implode(" ", $term_list);
}
if($post_type == 'documents'){ $link = get_post_meta($post->ID, 'document_link', true );};
echo '<tr><td><div class="rel"><a target="_blank" class="'.$term_list.'" href="'.$link.'">';
if($image):
?>
<div class='photo'>
<img src='<?= $image ?>' alt='<?= $image_alt ?>' loading="eager" />
</div>
<?php endif; ?>
<div class='content'>
<?php echo '<h3>'.$post->post_title.'</h3>';
if($description != ""){
echo '<p>'.$description.'</p>';
};
echo '</div></a></div></td></tr>';
};
echo '</tbody></table>';
?>
</div>