Amy-Mir / global-templates / nextprev-card.php
nextprev-card.php
Raw
<?php
/**
 * The template part for displaying previous or next post card
 * 
 * 
 * post_id = ID of the current post 
 * post_type = one of "next" or "prev" 
 * 
 * dependent on wp methods (get_previous_post, get_next_post) 
 * used in the following file: 
 * @see global-templates/single-post.php 
 */

$post_cover = get_field("cover", $args["post_id"]);
$post_title = get_the_title($args["post_id"]);
$post_type = $args["type"]; 
$post_cat = get_the_category( $args["post_id"] )[0];

$link = $post_cat->name === "Article" ? get_field("post_url", $args["post_id"]) : get_permalink($args["post_id"]);
$target = $post_cat->name === "Article" ? 'target="_blank" rel="noopener noreferrer"': '';

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

?>

<div class="npcard__wrapper">
    <div class="npcard__cover">
        <img alt="<?php echo esc_attr($post_cover['alt']); ?>" class="lazy"
            src="<?php echo esc_attr($post_cover['sizes']['thumbnail']); ?>"
            data-src="<?php echo esc_attr($post_cover['sizes']['large']); ?>" 
            data-srcset="<?php echo esc_attr($post_cover['sizes']['medium']); ?> 300w,
            <?php echo esc_attr($post_cover['sizes']['large']); ?> 768w,
            <?php echo esc_attr($post_cover['sizes']['1536x1536']); ?> 1024w" 
            data-sizes="100w" />
    </div>
    <a class="npcard__body" href="<?php echo esc_attr($link);?>" <?php echo $target?>>
        <div class="npcard__dir">
            <?php if ($post_type === "prev"): ?>
                <div class="npcard__prev">
                    <span class="fas fa-arrow-left"></span>
                    <span class="btnText__alt">Tidligere</span>
                </div>
            <?php elseif ($post_type === "next"): ?>
                <div class="npcard__prev">
                    <span class="btnText__alt">Neste</span>
                    <span class="fas fa-arrow-right"></span>
                </div>
            <?php endif; ?>
        </div>
        <div class="npcard__title">
            <h2 class="heading3"><?php echo esc_html($post_title) ?> </h2>
        </div>
    </a>
</div>