relevant-resources.php 3.71 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';
}
$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>