relevant-resources.php
2.62 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
<?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';
}
$categoryID = get_field( "categories" );
$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' => $categoryID, // term id
'field' => 'term_id',
)
)
);
if($categoryID == ""){
$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' => $categoryID, // term id
'field' => 'term_id',
)
)
);
if($categoryID == ""){
$args = array(
'post_type' => 'documents',
'posts_per_page' => -1,
);
}
$documents = get_posts($args);
$posts = array_merge($documents, $attachments);
echo '<ul>';
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');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_list = $term->name;
}
}
if($post_type == 'documents'){ $link = get_post_meta($post->ID, 'document_link', true );};
echo '<li><a class="'.$term_list.'" href="'.$link.'">'.$post->post_title.'</a><p>'.$description.'</p></li>';
};
echo '</ul>';
?>
</div>