Skip to content

Instantly share code, notes, and snippets.

@raekul
Last active November 18, 2015 16:48
Show Gist options
  • Save raekul/dedaaeaa10897cad9636 to your computer and use it in GitHub Desktop.
Save raekul/dedaaeaa10897cad9636 to your computer and use it in GitHub Desktop.
function offset_blog_posts($query)
{
if ($query->is_home() || $query->is_category() && $query->is_main_query() && !is_admin()) {
$post_type = array('post');
$query->set('post_type', $post_type);
$query->set('post_status', 'publish');
$query->set('ignore_sticky_posts', '-1');
$ppp = get_option('posts_per_page');
$offset = 1;
if (!$query->is_paged()) {
$query->set('posts_per_page', $offset + $ppp);
} else {
$offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('posts_per_page',$ppp);
$query->set('offset',$offset);
}
}
}
add_action('pre_get_posts','offset_blog_posts')
<?php
get_header();
// Used for the subnav bar so it knows what to pull through
$catArgs = ['hide_empty' => 1, 'type' => 'post'];
$categories = get_categories($catArgs);
// used for managing the display of categories
$curCat = get_the_category();
$curCategoryName = single_cat_title("", false);
$catCount = $curCat[1]->count-1;
if ($curCategoryName === 'All') {
$catID = $curCat[0]->cat_ID;
} else {
$catID = $curCat[1]->cat_ID;
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$do_not_duplicate = '';
remove_action('pre_get_posts', 'offset_blog_posts');
?>
<?php if($paged == 1): ?>
<?php
global $post;
$args = array(
'numberposts' => 1,
'offset'=> 0,
'category' => 1
);
$firstPost = get_posts( $args );
foreach( $firstPost as $post ) : setup_postdata($post); ?>
<?php
$do_not_duplicate = $post->ID;
if (has_post_thumbnail($post->ID) ); {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
}
?>
<header class="b-hero hero" style="background-image: url('<?php echo $image[0]; ?>');" height="<?php echo $image[2]; ?>">
<a class="hero__link" href="<?php the_permalink(); ?>">
<time class="hero__date"><?php the_time('d . m . y'); ?></time>
<h1 class="hero__title"><?php the_title(); ?></h1>
<p class="hero__read-more">Read More</p>
</a>
<div class="hero__overlay"></div>
</header>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<div class="subBar">
<div class="subBar__inner">
<ul class="subBar__list">
<?php foreach ($categories as $category): ?>
<?php $activeClass = ($category->name === $curCategoryName ? 'subBar__links--active' : '' ) ; ?>
<li class="subBar__items">
<?php if ($category->name === 'All'): ?>
<a class="subBar__links <?php echo $activeClass; ?>" href="<?php echo home_url('/blog'); ?>"><?php echo $category->name; ?></a>
<?php else: ?>
<a class="subBar__links <?php echo $activeClass; ?>" href="<?php echo get_category_link( $category->cat_ID ); ?>"><?php echo $category->name; ?></a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<div class="blogListing">
<div class="blogListing__inner">
<?php add_action('pre_get_posts', 'offset_blog_posts'); ?>
<?php if ( have_posts() ) : ?>
<?php;
$published_posts = ( !is_category() ? wp_count_posts('post')->publish : $catCount);
$posts_per_page = get_option('posts_per_page');
$page_number_max = ceil($published_posts / $posts_per_page);
$nextPage = get_next_posts_page_link();
wp_localize_script(
'script',
'post_ajax', [
'postType' => get_post_type(),
'maxPages' => $page_number_max,
'nextPage' => $nextPage
]
);
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
// get the post ID
$id = get_the_ID();
// pass this is equal to the first blog post
if ($id === $do_not_duplicate ) {
continue;
}
// get our thumbnail image
$image = (has_post_thumbnail( $id ) ? wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'large' ) : null );
// category info for each article
$category = get_the_category( $id );
$catnames = [];
foreach ($category as $cat) {
if ($cat->slug !== 'all') {
array_push($catnames, $cat->name);
}
}
?>
<article class="blogListing__content" role="article">
<a class="blogListing__linkWrap" href="<?php the_permalink(); ?>">
<img class="blogListing__img lazy" data-cslayzr="<?php echo $image[0]; ?>" draggable="false" />
<noscript><img class="blogListing__img lazy" src="<?php echo $image[0]; ?>" draggable="false" /></noscript>
<small class="blogListing__category"><?php foreach ($catnames as $catname) { echo $catname; } ?></small>
</a>
<div class="bloglisting__info">
<time class="blogListing__date"><?php the_time('d . m . y'); ?></time>
<a class="blogListing__linkWrap" href="<?php the_permalink(); ?>">
<h2 class="blogListing__title"><?php the_title(); ?></h2>
</a>
<p class="blogListing__shortDescription"><?php echo substr(preg_replace("/&#?[a-z0-9]+;/i", "", get_the_excerpt()), 0, 90); ?>...</p>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<div class="spinner" role="presentation">
<div class="spinner__inner">
<span class="spinner__dot"></span>
</div>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment