Amy-Mir / loop-templates / single-sidebar.php
single-sidebar.php
Raw
<?php
/**
 * The template part for displaying post page sidebar
 *   
 */
    
$totalPosts = wp_count_posts()->publish;
$recentPosts = new WP_Query(array(
    "posts_per_page" => 4
));

$socialMedia = get_field('social_media_links','options');

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;


?>

<aside class="sidebar__wrapper" role="complemantary">
    <div class="sidebar__recents">
        <h1 class="btnText">Siste innlegg</h1>

        <?php if( $recentPosts->have_posts() && $totalPosts > 4 ): ?>

        <ul>

        <?php while($recentPosts->have_posts()): $recentPosts->the_post(); 
            $link = $cat->name === "Article" ? get_field("post_url", get_the_ID( )) : get_permalink();
            $target = $cat->name === "Article" ? 'target="_blank" rel="noopener noreferrer"': '';

            $cover = get_field("cover", $postID); ?>

            <div class="sidebar__recentsItem">
                <div class="sidebar__recentsItemBlock">
                    <div class="recentPost__cover">
                        <img alt="<?php echo esc_attr($cover['alt']); ?>" class="lazy"
                            src="<?php echo esc_attr($cover['sizes']['thumbnail']); ?>"
                            data-src="<?php echo esc_attr($cover['sizes']['large']); ?>" 
                            data-srcset="<?php echo esc_attr($cover['sizes']['medium']); ?> 300w,
                            <?php echo esc_attr($cover['sizes']['large']); ?> 768w,
                            <?php echo esc_attr($cover['sizes']['1536x1536']); ?> 1024w" 
                            data-sizes="100w" />
                    </div>
                    <a href="<?php echo esc_attr( $link );?>" class="sidebar__recentsItemExc" <?php echo $target; ?> >
                        <div class="recentPost__title">
                            <h2 class="heading4"><?php echo esc_html( get_the_title() ); ?></h2>                     
                        </div>                   
                        <div class="recentPost__date">
                            <span class="far fa-clock"></span>
                            <span><?php the_time( 'F j, Y' ); ?></span>
                        </div>
                    </a>
                </div>
            </div>

        <?php endwhile; ?>

        </ul>  

        <?php else: ?>

        <div class="sidebar__recentsEmpty">                
            <img alt="Ingen innlegg tilgjengelig ennå" class="lazy"
                    src="<?php echo get_template_directory_uri(  ); ?>/assets/noposts__100.jpg"
                    data-src="<?php echo get_template_directory_uri(  ); ?>/assets/noposts__1023.jpg" 
                    data-srcset="<?php echo get_template_directory_uri(  ); ?>/assets/noposts__300.jpg 300w,
                    <?php echo get_template_directory_uri(  ); ?>/assets/noposts__768.jpg 768w,
                    <?php echo get_template_directory_uri(  ); ?>/assets/noposts__1023.jpg 1024w" 
                    data-sizes="100w" />
            <span class="btnText__alt">Ingen innlegg tilgjengelig ennå</span>
        </div>

        <?php endif; ?>
    </div>

    <div class="sidebar__socials">
        <h1 class="btnText">Følg meg på</h1>
        <ul>
            <li>
                <a href="<?php echo esc_attr( $socialMedia['facebook'])?>" target='_blank'
                    rel="noopener noreferrer">
                    <span class="fab fa-facebook-f"></span>
                    <span>Facebook</span>
                </a>
            </li>
            <li>
                <a href="<?php echo esc_attr( $socialMedia['instagram'])?>" target='_blank'
                    rel="noopener noreferrer">
                    <span class="fab fa-instagram"></span>
                    <span>Instagram</span>
                </a>
            </li>
            <li>
                <a href="<?php echo esc_attr( $socialMedia['linkedin'])?>" target='_blank'
                    rel="noopener noreferrer">
                    <span class="fab fa-linkedin-in"></span>
                    <span>Linkedin</span>
                </a>
            </li>
        </ul>
    </div>
</aside>