relevant-resources.php 2.62 KB
<?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>