Skip to content

Instantly share code, notes, and snippets.

@JRyven
Last active October 20, 2020 17:43
Show Gist options
  • Save JRyven/e33977834f12553c4c488ca159299129 to your computer and use it in GitHub Desktop.
Save JRyven/e33977834f12553c4c488ca159299129 to your computer and use it in GitHub Desktop.
Standard WordPress Custom Loop
<section class="page-loop easybib-guides-loop">
<div class="container">
<?php
// WP_Query arguments
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'posts_per_page' => 10,
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
);
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
?>
<div class="post__type_post">
<a class="post__type_post_image" href="<?php echo get_the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url( get_the_ID(),'full' ); ?>">
</a>
<a class="post__type_post_title__link" href="<?php echo get_the_permalink(); ?>">
<h2 class="post__type_post_title"><?php echo get_the_title(); ?></h2>
</a>
<div class="post__type_post_meta">
<a class="post__type_post_date"><?php echo date('Y-m-d', strtotime( get_the_date() )); ?></p>
<a class="post__type_post_author" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>"><?php the_author(); ?></a>
<div class="post__type_post_terms">
<?php
$cats = get_the_category();
if (is_array($cats)) : $count = count($cats); $count = $count - 1;
foreach ( $cats as $key => $cat ): ?>
<a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->name; ?></a>
<?php if ($count != 0 && $key != $count) : echo ' / '; endif;
endforeach;
endif; ?>
</div>
</div>
<p class="post__type_post_excerpt"><?php echo get_the_excerpt(); ?> ...</p>
<a class="post__type_post_readmore" href="<?php echo get_the_permalink(); ?>">Learn More</a>
</div>
<?php
endwhile; // END while $query->have_posts()
/* this is for getting the last post number */
if ( get_query_var( 'paged' ) ) {
$current_page = get_query_var( 'paged' );
} else {
$current_page = 1;
}
$permalink_structure = get_option( 'permalink_structure' );
$format = empty( $permalink_structure ) ? '&page=%#%' : 'page/%#%/';
?>
<div class="pagination">
<?php echo
paginate_links( array(
'total' => $query->max_num_pages,
'current' => $current_page,
'prev_text' => '&laquo; ' . __( 'Prev' ),
'next_text' => __( 'Next' ) . ' &raquo;',
'base' => add_query_arg( 'paged', '%#%' ),
'format' => $format,
) ); ?>
</div>
<?php // END $query->have_posts()
endif; ?>
<?php // Restore original Post Data
wp_reset_postdata(); ?>
</div>
</section>
<?php
$news_header = 'Text'
$news_items = '1, 2, 3';
/*
To show specific items - posts, pages, events, or other post types - enter comma separated list of post or page IDs. (EG: "3718, 3592, 3584" )
To show most recent posts within a Post Category, enter a comma separated list of Post Category Slugs. (EG: "icc, news, campaign-update" )
You may not combine query strategies (either all ids or all categories).
*/
?>
<?php if ($news_header != ''): ?>
<h2><?php echo $news_header; ?></h2>
<?php endif; ?>
<?php if ($news_items != ''): ?>
<?php
$query_type = '';
$post_type = '';
$post_in = '';
$tax_query = '';
$news_items_string = str_replace( ' ', '', $news_items );
$news_items = explode( ',', $news_items_string );
foreach ( $news_items as $item ):
$intval_item = intval($item);
// if there are words passed into the field, they'll return Zeros
// so we do tax query
if( $intval_item == 0 ):
$query_type = 'tax_query';
// if they do not return zeros, we do post__in
// or ID Query
else:
$query_type = 'id_query';
endif;
endforeach;
$args = array(
'post_status' => 'publish',
'posts_per_page' => 3,
);
if($query_type == 'tax_query'):
$args['post_type'] = 'post';
$args['category_name'] = $news_items_string;
else:
$args['post_type'] = 'any';
$args['post__in'] = $news_items;
endif;
?>
<div class="news-posts">
<?php
$query = new WP_Query( $args );
$c = 0;
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$c++;
$permalink = get_the_permalink( $post->ID );
$image = get_the_post_thumbnail( $post->ID, 'full', array( 'class' => 'object-fit-image' ));
$title = get_the_title( $post->ID );
$excerpt = bpm_get_the_excerpt( $post->ID );
$first = '';
if ( $c == 1 || $c == 4 || $c == 7 || $c == 10 || $c == 13 || $c == 16 || $c == 19 ) {
$first = ' first';
}
?>
<div class="news-post one-third'.$first.'">
<div>
<div class="mb35">'.$image.'</div>
<h4 class="center font-black uppercase mb35">'.$title.'</h4>
<p class="center font-black mb35">'.$excerpt.'</p>
<div class="bcontainer"><a class="bb-button center " href="'.$permalink.'">read more</a></div>
</div>
</div>
<?php } } ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php
add_shortcode( 'post_shortcode', 'bpm__content__post_shortcode' );
function bpm__content__post_shortcode($atts, $content = null) {
$a = shortcode_atts( array(
'class' => '',
'cat' => '',
), $atts );
//
$classes='post ';
if($atts['class'] != '') {
$classes .= $atts['class'];
}
//
$cat='';
if($atts['cat'] != '') {
$cat = $atts['cat'];
}
ob_start();
?>
<section class="post-loop">
<div class="container">
<?php
// WP_Query arguments
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'posts_per_page' => 10,
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
);
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
?>
<div class="post__type_post">
<a class="post__type_post_image" href="<?php echo get_the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url( get_the_ID(),'full' ); ?>">
</a>
<a class="post__type_post_title__link" href="<?php echo get_the_permalink(); ?>">
<h2 class="post__type_post_title"><?php echo get_the_title(); ?></h2>
</a>
<div class="post__type_post_meta">
<a class="post__type_post_date"><?php echo date('Y-m-d', strtotime( get_the_date() )); ?></p>
<a class="post__type_post_author" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo esc_attr( get_the_author() ); ?>"><?php the_author(); ?></a>
<div class="post__type_post_terms">
<?php
$cats = get_the_category();
if (is_array($cats)) : $count = count($cats); $count = $count - 1;
foreach ( $cats as $key => $cat ): ?>
<a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->name; ?></a>
<?php if ($count != 0 && $key != $count) : echo ' / '; endif;
endforeach;
endif; ?>
</div>
</div>
<p class="post__type_post_excerpt"><?php echo get_the_excerpt(); ?> ...</p>
<a class="post__type_post_readmore" href="<?php echo get_the_permalink(); ?>">Learn More</a>
</div>
<?php
endwhile; // END while $query->have_posts()
/* this is for getting the last post number */
if ( get_query_var( 'paged' ) ) {
$current_page = get_query_var( 'paged' );
} else {
$current_page = 1;
}
$permalink_structure = get_option( 'permalink_structure' );
$format = empty( $permalink_structure ) ? '&page=%#%' : 'page/%#%/';
?>
<div class="pagination">
<?php echo
paginate_links( array(
'total' => $query->max_num_pages,
'current' => $current_page,
'prev_text' => '&laquo; ' . __( 'Prev' ),
'next_text' => __( 'Next' ) . ' &raquo;',
'base' => add_query_arg( 'paged', '%#%' ),
'format' => $format,
) ); ?>
</div>
<?php // END $query->have_posts()
endif; ?>
<?php // Restore original Post Data
wp_reset_postdata(); ?>
</div>
</section>
<?php
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment