Monday, November 23, 2020
therichposts@gmail.com

Skip three post in wordpress post loop

QuestionsCategory: QuestionsSkip three post in wordpress post loop
Lim Shi Jie asked 3 years ago

Hi therichpost,
I’m from stackoverflow user Lim Shi Jie regarding the three post posted, I am using a premium theme and i’m customising the code. 
Regarding the post loop, it is an add-on visual composer called “latest post” which i put under my homepage website: http://www.sharefood.sg . If you have a look at the “latest post”, the first 3 posts there are the latest post which i do not wish to have it as the slider post already have the 3 posts. I find that for UX purpose there shouldn’t be a duplicate post when users view the pages, hence my reason of removing the first 3 posts from the “latest post” add on.
Now i tried to add the codes you gave me on stackoverflow, the code works. But because the add-on comes with a “load-more” function button somehow after clicking it, the next 3 posts loaded are also being hidden.
Anyway here are the codes, if you need the whole file i could send it to you via email (I’m using style ‘grid’ in this file):
 
public static function latest_posts( $atts, $content = null ) {
extract(shortcode_atts(array(
‘style’   => ‘standard’,
‘heading’ => ”,
‘number’  => ’10’,
‘paging’  => ‘numbers’,
‘morenum’ => ‘6’,
‘exclude’ => ”
), $atts));
 
$return = ”;
 
if ( ! isset( $number ) || ! is_numeric( $number ) ): $number = ’10’; endif;
if ( ! isset( $morenum ) || ! is_numeric( $morenum ) ): $morenum = ‘6’; endif;
$paged = max( get_query_var( ‘paged’ ), get_query_var( ‘page’ ), 1 );
$args  = array( ‘post_type’ => ‘post’, ‘paged’ => $paged, ‘posts_per_page’ => $number );
if ( ! empty( $exclude ) ):
$exclude_cats      = str_replace( ‘ ‘, ”, $exclude );
$exclude_array     = explode( ‘,’, $exclude_cats );
$args[‘tax_query’] = array(
array(
‘taxonomy’ => ‘category’,
‘field’    => ‘slug’,
‘terms’    => $exclude_array,
‘operator’ => ‘NOT IN’
)
);
endif;
 
$query_custom = new WP_Query( $args );
if ( $query_custom->have_posts() ) :
ob_start();
?>
 
<?php if ( $heading ) : ?>
<?php
$heading_title = get_theme_mod( ‘penci_featured_cat_style’ ) ? get_theme_mod( ‘penci_featured_cat_style’ ) : ‘style-1’;
$heading_align = get_theme_mod( ‘penci_heading_latest_align’ ) ? get_theme_mod( ‘penci_heading_latest_align’ ) : ‘pcalign-center’;
?>
<div class=”penci-border-arrow penci-homepage-title penci-home-latest-posts <?php echo sanitize_text_field( $heading_title . ‘ ‘ . $heading_align ); ?>”>
<h3 class=”inner-arrow”><?php echo do_shortcode( $heading ); ?></h3>
</div>
<?php endif; ?>
 
<div class=”penci-wrapper-posts-content”>
 
<?php if( in_array( $style, array( ‘standard’, ‘classic’, ‘overlay’ ) ) ): ?><div class=”penci-wrapper-data”><?php endif; ?>
<?php if ( in_array( $style, array( ‘mixed’, ‘mixed-2’, ‘overlay-grid’, ‘overlay-list’, ‘photography’, ‘grid’, ‘grid-2’, ‘list’, ‘boxed-1’, ‘boxed-2’, ‘boxed-3’, ‘standard-grid’, ‘standard-grid-2’, ‘standard-list’, ‘standard-boxed-1’, ‘classic-grid’, ‘classic-grid-2’, ‘classic-list’, ‘classic-boxed-1’, ‘magazine-1’, ‘magazine-2’ ) ) ) : ?><ul class=”penci-wrapper-data penci-grid penci-shortcode-render”><?php endif; ?>
<?php if ( in_array( $style, array( ‘masonry’, ‘masonry-2’ ) ) ) : ?><div class=”penci-wrap-masonry”><div class=”penci-wrapper-data masonry penci-masonry”><?php endif; ?>
 
<?php /* The loop */
while ( $query_custom->have_posts() ) : $query_custom->the_post();
include( locate_template( ‘content-‘ . $style . ‘.php’ ) );
endwhile;
 
?>
 
<?php if( in_array( $style, array( ‘standard’, ‘classic’, ‘overlay’ ) ) ): ?></div><?php endif; ?>
<?php if ( in_array( $style, array( ‘masonry’, ‘masonry-2’ ) ) ) : ?></div></div><?php endif; ?>
<?php if ( in_array( $style, array( ‘mixed’, ‘mixed-2’, ‘overlay-grid’, ‘overlay-list’, ‘photography’, ‘grid’, ‘grid-2’, ‘list’, ‘boxed-1’, ‘boxed-2’, ‘boxed-3’, ‘standard-grid’, ‘standard-grid-2’, ‘standard-list’, ‘standard-boxed-1’, ‘classic-grid’, ‘classic-grid-2’, ‘classic-list’, ‘classic-boxed-1’, ‘magazine-1’, ‘magazine-2’ ) ) ) : ?></ul><?php endif; ?>
 
 
<?php
if( $paging == ‘loadmore’ || $paging == ‘scroll’ ) {
$button_class = ‘penci-ajax-more penci-ajax-home penci-ajax-more-click’;
if( $paging == ‘loadmore’ ):
wp_enqueue_script( ‘penci_ajax_more_posts’ );
wp_localize_script( ‘penci_ajax_more_posts’, ‘ajax_var_more’, array(
‘url’     => admin_url( ‘admin-ajax.php’ ),
‘nonce’   => wp_create_nonce( ‘ajax-nonce’ )
)
);
endif;
 
/* Get data template */
$data_layout = $style;
$data_template = ‘sidebar’;
if ( in_array( $style, array( ‘standard-grid’, ‘classic-grid’, ‘overlay-grid’ ) ) ) {
$data_layout = ‘grid’;
} elseif ( in_array( $style, array( ‘standard-grid-2’, ‘classic-grid-2’ ) ) ) {
$data_layout = ‘grid-2’;
} elseif ( in_array( $style, array( ‘standard-list’, ‘classic-list’, ‘overlay-list’ ) ) ) {
$data_layout = ‘list’;
} elseif ( in_array( $style, array( ‘standard-boxed-1’, ‘classic-boxed-1’ ) ) ) {
$data_layout = ‘boxed-1’;
}
 
if( is_page_template( ‘page-vc.php’ ) ) {
$data_template = ‘no-sidebar’;
}
?>
<div class=”penci-pagination <?php echo $button_class; ?>”>
<a class=”penci-ajax-more-button” data-mes=”<?php echo penci_get_setting(‘penci_trans_no_more_posts’); ?>” data-layout=”<?php echo esc_attr( $data_layout ); ?>” data-number=”<?php echo absint($morenum); ?>” data-offset=”<?php echo absint($number); ?>” data-exclude=”<?php
echo $exclude; ?>” data-from=”vc” data-template=”<?php echo $data_template; ?>”>
<span class=”ajax-more-text”><?php echo penci_get_setting(‘penci_trans_load_more_posts’); ?></span><span class=”ajaxdot”></span><i class=”fa fa-refresh”></i>
</a>
</div>
<?php } else { ?>
<?php echo penci_pagination_numbers( $query_custom ); ?>
<?php } ?>
 
</div>
 
<?php
endif; wp_reset_postdata();
 
$return = ob_get_clean();
 
return $return;
}
 
I would really appreciate your help on this! thanks!
 

1 Answers
Best Answer
lim shi jie answered 2 years ago

Hi therichpost,
 
I still can’t get the codes to work and could i use load more instead of custom pagination?
Here’s the before and after images of implementing the codes:
before: https://ibb.co/f4H2TJ
after: https://ibb.co/jW7moJ
 
If you like, i could send you the whole file here to investigate what is causing the issue here. It’s funny why would the theme cause such issue when implementing the codes.
 
Thanks!