Skip to content

Instantly share code, notes, and snippets.

@santanup789
Created August 26, 2021 10:02
Show Gist options
  • Save santanup789/fc4b0c0e8fa2b052b86e9aaa9c0e1c5f to your computer and use it in GitHub Desktop.
Save santanup789/fc4b0c0e8fa2b052b86e9aaa9c0e1c5f to your computer and use it in GitHub Desktop.
Get post for archive page with featured image, category name, post date, author name, short description, title, read more link and if you have youtube video url custom field for each post that is also done. Also fancyapps lightbox for youtube video player with pagination.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$default_posts_per_page = get_option( 'posts_per_page' );
$args = array (
'post_type' => 'post',
'posts_per_page' => -1,
'posts_per_page' => $default_posts_per_page,
'paged' => $paged,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( get_queried_object()->term_id ),
) )
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo "<div class='yt_grid listing'>";
while ( $the_query->have_posts() ) {
$the_query->the_post();
$getTitle= get_the_title();
$postURL= get_permalink();
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'large' );
$url = $src[0];
$org_content = get_the_content();
$trim_content = substr($org_content, 0, 400);
$content = strip_tags($trim_content);
$post_day = get_the_date( 'd' );
$post_month = get_the_date( 'M' );
$get_yt_url = get_field('youtube_url');
$author = get_the_author();
$categories = get_the_terms( $post->ID, 'category' );
$catList = '';
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $get_yt_url, $match);
$youtube_id = $match[1];
foreach($categories as $category) {
if(!empty($catList)) {
$catList .= ', ';
}
//$catID = get_cat_ID( $cat->cat_name );
//$catLink = get_category_link( $catID );
$catList .= '<span>'.$category->name.'</span>';
}
echo "<div class='item'>";
echo "<div class='inner_wrapper'>";
echo "<div class='post_img'>";
if($get_yt_url){
echo "<a href='$get_yt_url' data-fancybox='blog_yt_video'>";
}
else {
echo "<a href='$postURL'>";
}
echo "<img src='$url' alt='$getTitle'>";
echo "</a>";
echo "</div>"; //post_img
echo "<div class='post-content'>";
echo "<div class='post-meta'>";
echo "<div class='post_date'><span>$post_day</span><label>$post_month</label></div>";
echo "<div class='title-meta'>
<h5><a href='$postURL'>$getTitle</a></h5>
<div class='metas'><label><img src='".get_stylesheet_directory_uri()."/images/author_icon.png'>By $author</label><label><img src='".get_stylesheet_directory_uri()."/images/cat_icon.png'>$catList</label></div>
</div>";
echo "</div>";//post-meta
echo "<p>$content</p>";
echo "<div class='custom-button'><a href='$postURL' class=''><span class=''><span class=''><i class='fas fa-arrow-right'></i></span><span class=''>Read More</span></span></a></div>"; //custom-button
echo "</div>"; //post-content
echo "</div>"; //inner_wrapper
echo "</div>"; //item
}
echo "</div>";
if (function_exists("pagination")) {
pagination($the_query->max_num_pages);
}
}
else {
echo __( 'No products found' );
}
wp_reset_postdata();?>
<script>
jQuery(function($) {
$('[data-fancybox="yt_video"]').fancybox({
baseClass: "fancybox-custom-layout",
infobar: false,
touch: {
vertical: false
},
buttons: ["close", "thumbs"],
animationEffect: "fade",
transitionEffect: "fade",
preventCaptionOverlap: false,
idleTime: false,
gutter: 0,
caption: function(instance) {
var caption = $(this).data('caption') || '';
var description = $(this).data('desc') || '';
return ('<strong>'+caption+'</strong>'+'</br>'+ description);
}
})
});
</script>
?>
@santanup789
Copy link
Author

santanup789 commented Aug 26, 2021

For pagination function, here is the code which you have to paste into your functions.php

`<?php function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;

global $paged;
if(empty($paged)) $paged = 1;

if($pages == '')
{
    global $wp_query;
    $pages = $wp_query->max_num_pages;
    if(!$pages)
    {
        $pages = 1;
    }
}

if(1 != $pages)
{
    echo "<div class=\"pagination\">";
	//echo "<span>Page ".$paged." of ".$pages."</span>";
    if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
    if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

    for ($i=1; $i <= $pages; $i++)
    {
        if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
        {
            echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
        }
    }

    if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">&rsaquo;</a>";
    if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
    echo "</div>\n";
}

}
?>`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment