|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Element to output the Newsfeed preview on section pages. |
| 4 | + * Gets the 4 most recent posts belonging to the selected categories. |
| 5 | + * Outputs them with markup if at least one category is set and a minimum of 4 posts exist. |
| 6 | + * |
| 7 | + * @link https://codex.wordpress.org/Template_Hierarchy |
| 8 | + * |
| 9 | + * @package UWCde_Website |
| 10 | + */ |
| 11 | + |
| 12 | +$taxonomies = get_field( 'newsfeed' ); |
| 13 | + |
| 14 | +$posts = get_posts(array( |
| 15 | + 'posts_per_page' => 4, |
| 16 | + 'category' => $taxonomies, |
| 17 | + 'orderby' => 'date', |
| 18 | + 'order' => 'DESC', |
| 19 | + 'post_status' => 'publish', |
| 20 | +)); |
| 21 | +$count = count( $posts ); |
| 22 | + |
| 23 | +if ( $taxonomies && $count >= 4 ) { |
| 24 | + echo '<section class="section-newsfeed">'; |
| 25 | + $index = 1; |
| 26 | + foreach ( $posts as $post ) { |
| 27 | + $categories = array(); |
| 28 | + |
| 29 | + foreach ( $taxonomies as $taxonomy ) { |
| 30 | + if ( in_category( $taxonomy, $post->ID ) ) { |
| 31 | + array_push( $categories, $taxonomy ); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + if ( get_field( 'feed_image', $post->ID ) ) { |
| 36 | + $image = get_field( 'feed_image', $post->ID ); |
| 37 | + $size = 'medium'; |
| 38 | + $image_url = $image['sizes'][ $size ]; |
| 39 | + |
| 40 | + echo '<article class="section-post post-' . intval( $index ) . ' -thumbnail"><div class="section-image" style="background-image: url('; |
| 41 | + echo esc_url( $image_url ); |
| 42 | + echo ')"></div>'; |
| 43 | + } elseif ( has_post_thumbnail( $post->ID ) ) { |
| 44 | + $featured = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); |
| 45 | + $featured_url = $featured[0]; |
| 46 | + |
| 47 | + echo '<article class="section-post post-' . intval( $index ) . ' -thumbnail"><div class="section-image" style="background-image: url('; |
| 48 | + echo esc_url( $featured_url ); |
| 49 | + echo ')"></div>'; |
| 50 | + } else { |
| 51 | + echo '<article class="section-post post-' . intval( $index ) . ' -no-thumbnail">'; |
| 52 | + } |
| 53 | + echo '<div class="section-wrapper">'; |
| 54 | + foreach ( $categories as $category ) { |
| 55 | + echo '<a href="' . esc_url( get_category_link( $category ) ) . '" class="section-category">' . esc_html( get_cat_name( $category ) ) . '</a> '; |
| 56 | + } |
| 57 | + echo '<a href="' . esc_url( get_permalink( $post->ID ) ) . '"><h2 class="section-headline">' . get_the_title( $post->ID ) . '</h2><h4 class="section-date">' . get_the_date( '', $post->ID ) . '</h4></a></div></article>'; |
| 58 | + ++$index; |
| 59 | + } |
| 60 | + echo '</section>'; |
| 61 | +} |
0 commit comments