<?php
/**
* functions/inc-blog.php
*
* Blog & Posts: Related Posts, Blog Home Page Posts Offset
*
* @author JJROD Framework
* @see https://docs.jjrod.com
* @version 1.0
*/
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/* Related Posts */
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
function joints_related_posts() {
global $post;
$tag_arr = '';
$tags = wp_get_post_tags( $post->ID );
if($tags) {
foreach( $tags as $tag ) {
$tag_arr .= $tag->slug . ',';
}
$args = array(
'tag' => $tag_arr,
'numberposts' => 3, /* you can change this to show more */
'post__not_in' => array($post->ID)
);
$related_posts = get_posts( $args );
if($related_posts) {
echo __( '<h4>Related Posts</h4>', 'jointswp' );
echo '<ul class="joints-related-posts">';
foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>
<li class="related_post">
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php get_template_part( 'parts/content', 'byline' ); ?>
</li>
<?php endforeach; }
}
wp_reset_postdata();
echo '</ul>';
}
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
/* Offset the main query on blog home page */
/****************************************************************************************************************/
/****************************************************************************************************************/
/****************************************************************************************************************/
function myprefix_query_offset(&$query) {
// Before anything else, make sure this query is on our
// blog's home page, and that it's the main query...
if ( $query->is_home() && $query->is_main_query() ) {
// First, define your desired offset...
$offset = 1;
// Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option('posts_per_page');
// Next, detect and handle pagination...
if ( $query->is_paged ) {
// Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
// Apply adjust page offset
$query->set('offset', $page_offset );
}
else {
// This is the first page. Just use the offset...
$query->set('offset',$offset);
}
} else {
return;
}
}
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
/**
*
* Adjust the found_posts according to our offset.
* Used for our pagination calculation.
*
*/
function myprefix_adjust_offset_pagination($found_posts, $query) {
// Define our offset again...
$offset = 1;
// Ensure we're modifying the right query object...
if ( $query->is_home() && $query->is_main_query() ) {
// Reduce WordPress's found_posts count by the offset...
return $found_posts - $offset;
}
return $found_posts;
}
add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );