Skip to content

Instantly share code, notes, and snippets.

@Garconis
Created August 23, 2018 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garconis/c93ce89c3d378bfb7f5d8df97acbebc2 to your computer and use it in GitHub Desktop.
Save Garconis/c93ce89c3d378bfb7f5d8df97acbebc2 to your computer and use it in GitHub Desktop.
WordPress | Crazy loop shortcode to also inject CPT after X posts
<?php
// [fs_burn_homepage_loop]
/**
* https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
* On the page that is set to be the blog homepage (Posts Page), exclude a post from the loop, if it has a certain tag ID
**/
// exclude posts from the main blog loop, if it has the certain info
/*
function fs_exclude_specific_tag( $query ) {
if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
// $query->set( 'tag', '-141' );
// $query->set( 'category__not_in ', array('12') );
// $query->set( 'tag__not_in', array('12') );
// $query->set( 'author', array('12') );
}
}
add_action( 'pre_get_posts', 'fs_exclude_specific_tag' );
*/
/*
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'project') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
*/
// we also force create a new image size for WordPress to crop to, for ad sizes */
add_action( 'after_setup_theme', 'fs_theme_setup' );
function fs_theme_setup() {
add_image_size( 'fs-ad-size', 300, 250, true ); // (cropped)
}
/* --
Old idea: POSSBILY add a new CPT of "advertisement" to this loop,
and then use PHP math somehow to add a new CSS "order" rule to each post in the "regular" post types (posts and events), counting up by 1.
But if it's an advertisement post type, then set a hard-fixed number, in increments of a number such as 8 (e.g, 8, 16, 24, etc.)
New idea: Currently being used by the code further down in this function,
is to just pull in a specific post ID (of the CPT) and "inject" it into the desired area
-- */
// create shortcode to list all Featured Itineraries
add_shortcode( 'fs_burn_homepage_loop', 'fs_all_featured_itineraries_shortcode' );
function fs_all_featured_itineraries_shortcode( $atts ) {
ob_start();
// Query posts from the database.
$options = array(
// Arguments for your query.
//'post_type' => 'project',
'post_type' => array('post', 'ajde_events'),
'posts_per_page' => -1,
'paged' => 1,
//'meta_key' => 'featured-product',
//'meta_value' => '1',
);
/* TRYING TO GET LOAD MORE TO WORK */
// $options['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
//$options['paged'] = if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); } elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); } else { $paged = 1; }
if ( get_query_var( 'paged' ) ) {
$options['paged'] = get_query_var( 'paged' );
}
elseif ( get_query_var( 'page' ) ) {
$options['paged'] = get_query_var( 'page' );
} else {
$options['paged'] = 1;
}
/* INJECTING THE ADS of specific IDs from the Advertisements custom post type
-- alternatively consider using something like https://wpadvancedads.com/add-ons/advanced-ads-pro/ for less hard-coded hackery
*/
$ad_wrapper_start = '<article class="fs-post-article fs-post-ad"><div class="fs-post-content-wrapper"><div class="fs-ad-wrapper">';
$ad_wrapper_content = '<h6>Advertise with us</h6><p>This ad spot is available. Interested? </p><a href="/advertise/" class="ad-btn">Submit an ad</a>';
$ad_wrapper_end = '</div></div></article>';
// custim image crop size we made earlier
$ad_img_size = 'fs-ad-size';
// AD 01
$ad_01_id = 239;
$ad_01_status = get_field( "status", $ad_01_id );
$ad_01_type = get_field( "ad_type", $ad_01_id );
$ad_01_adsense = get_field( "google_adsense_code", $ad_01_id );
$ad_01_link = get_field( "custom_ad_link", $ad_01_id );
$ad_01_img = get_field( "custom_ad_image", $ad_01_id );
$ad_01_custom = '<a href="'. $ad_01_link .'" target="_blank"><img src="'. $ad_01_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_01_img['alt'] .'"></a>';
if ($ad_01_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_01_content = $ad_wrapper_content;
if ($ad_01_type == 'adsense') {
$ad_wrapper_01_content = $ad_01_adsense;
}
elseif ($ad_01_type == 'custom') {
$ad_wrapper_01_content = $ad_01_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_01_display = $ad_wrapper_start . $ad_wrapper_01_content . $ad_wrapper_end;
}
// AD 02
$ad_02_id = 241;
$ad_02_status = get_field( "status", $ad_02_id );
$ad_02_type = get_field( "ad_type", $ad_02_id );
$ad_02_adsense = get_field( "google_adsense_code", $ad_02_id );
$ad_02_link = get_field( "custom_ad_link", $ad_02_id );
$ad_02_img = get_field( "custom_ad_image", $ad_02_id );
$ad_02_custom = '<a href="'. $ad_02_link .'" target="_blank"><img src="'. $ad_02_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_02_img['alt'] .'"></a>';
if ($ad_02_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_02_content = $ad_wrapper_content;
if ($ad_02_type == 'adsense') {
$ad_wrapper_02_content = $ad_02_adsense;
}
elseif ($ad_02_type == 'custom') {
$ad_wrapper_02_content = $ad_02_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_02_display = $ad_wrapper_start . $ad_wrapper_02_content . $ad_wrapper_end;
}
// AD 03
$ad_03_id = 242;
$ad_03_status = get_field( "status", $ad_03_id );
$ad_03_type = get_field( "ad_type", $ad_03_id );
$ad_03_adsense = get_field( "google_adsense_code", $ad_03_id );
$ad_03_link = get_field( "custom_ad_link", $ad_03_id );
$ad_03_img = get_field( "custom_ad_image", $ad_03_id );
$ad_03_custom = '<a href="'. $ad_03_link .'" target="_blank"><img src="'. $ad_03_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_03_img['alt'] .'"></a>';
if ($ad_03_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_03_content = $ad_wrapper_content;
if ($ad_03_type == 'adsense') {
$ad_wrapper_03_content = $ad_03_adsense;
}
elseif ($ad_03_type == 'custom') {
$ad_wrapper_03_content = $ad_03_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_03_display = $ad_wrapper_start . $ad_wrapper_03_content . $ad_wrapper_end;
}
// AD 04
$ad_04_id = 243;
$ad_04_status = get_field( "status", $ad_04_id );
$ad_04_type = get_field( "ad_type", $ad_04_id );
$ad_04_adsense = get_field( "google_adsense_code", $ad_04_id );
$ad_04_link = get_field( "custom_ad_link", $ad_04_id );
$ad_04_img = get_field( "custom_ad_image", $ad_04_id );
$ad_04_custom = '<a href="'. $ad_04_link .'" target="_blank"><img src="'. $ad_04_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_04_img['alt'] .'"></a>';
if ($ad_04_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_04_content = $ad_wrapper_content;
if ($ad_04_type == 'adsense') {
$ad_wrapper_04_content = $ad_04_adsense;
}
elseif ($ad_04_type == 'custom') {
$ad_wrapper_04_content = $ad_04_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_04_display = $ad_wrapper_start . $ad_wrapper_04_content . $ad_wrapper_end;
}
// AD 05
$ad_05_id = 244;
$ad_05_status = get_field( "status", $ad_05_id );
$ad_05_type = get_field( "ad_type", $ad_05_id );
$ad_05_adsense = get_field( "google_adsense_code", $ad_05_id );
$ad_05_link = get_field( "custom_ad_link", $ad_05_id );
$ad_05_img = get_field( "custom_ad_image", $ad_05_id );
$ad_05_custom = '<a href="'. $ad_05_link .'" target="_blank"><img src="'. $ad_05_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_05_img['alt'] .'"></a>';
if ($ad_05_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_05_content = $ad_wrapper_content;
if ($ad_05_type == 'adsense') {
$ad_wrapper_05_content = $ad_05_adsense;
}
elseif ($ad_05_type == 'custom') {
$ad_wrapper_05_content = $ad_05_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_05_display = $ad_wrapper_start . $ad_wrapper_05_content . $ad_wrapper_end;
}
// AD 06
$ad_06_id = 245;
$ad_06_status = get_field( "status", $ad_06_id );
$ad_06_type = get_field( "ad_type", $ad_06_id );
$ad_06_adsense = get_field( "google_adsense_code", $ad_06_id );
$ad_06_link = get_field( "custom_ad_link", $ad_06_id );
$ad_06_img = get_field( "custom_ad_image", $ad_06_id );
$ad_06_custom = '<a href="'. $ad_06_link .'" target="_blank"><img src="'. $ad_06_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_06_img['alt'] .'"></a>';
if ($ad_06_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_06_content = $ad_wrapper_content;
if ($ad_06_type == 'adsense') {
$ad_wrapper_06_content = $ad_06_adsense;
}
elseif ($ad_06_type == 'custom') {
$ad_wrapper_06_content = $ad_06_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_06_display = $ad_wrapper_start . $ad_wrapper_06_content . $ad_wrapper_end;
}
// AD 07
$ad_07_id = 246;
$ad_07_status = get_field( "status", $ad_07_id );
$ad_07_type = get_field( "ad_type", $ad_07_id );
$ad_07_adsense = get_field( "google_adsense_code", $ad_07_id );
$ad_07_link = get_field( "custom_ad_link", $ad_07_id );
$ad_07_img = get_field( "custom_ad_image", $ad_07_id );
$ad_07_custom = '<a href="'. $ad_07_link .'" target="_blank"><img src="'. $ad_07_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_07_img['alt'] .'"></a>';
if ($ad_07_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_07_content = $ad_wrapper_content;
if ($ad_07_type == 'adsense') {
$ad_wrapper_07_content = $ad_07_adsense;
}
elseif ($ad_07_type == 'custom') {
$ad_wrapper_07_content = $ad_07_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_07_display = $ad_wrapper_start . $ad_wrapper_07_content . $ad_wrapper_end;
}
// AD 08
$ad_08_id = 248;
$ad_08_status = get_field( "status", $ad_08_id );
$ad_08_type = get_field( "ad_type", $ad_08_id );
$ad_08_adsense = get_field( "google_adsense_code", $ad_08_id );
$ad_08_link = get_field( "custom_ad_link", $ad_08_id );
$ad_08_img = get_field( "custom_ad_image", $ad_08_id );
$ad_08_custom = '<a href="'. $ad_08_link .'" target="_blank"><img src="'. $ad_08_img['sizes'][ $ad_img_size ] .'" alt="'. $ad_08_img['alt'] .'"></a>';
if ($ad_08_status == 'enable') {
// set this ad to use regular content initially
$ad_wrapper_08_content = $ad_wrapper_content;
if ($ad_08_type == 'adsense') {
$ad_wrapper_08_content = $ad_08_adsense;
}
elseif ($ad_08_type == 'custom') {
$ad_wrapper_08_content = $ad_08_custom;
}
// output the display to use the wrapper and whatever the content became
$ad_08_display = $ad_wrapper_start . $ad_wrapper_08_content . $ad_wrapper_end;
}
// Used to set first post counter to 0
// $post_counter=0;
$query = new WP_Query( $options /* 'post_type=post&posts_per_page=3&paged='.$paged */);
// Check that we have query results
if ( $query->have_posts() ) {
// we have posts, so lets wrap them
echo'<div id="fs-homepage-loop" class="fs-all-posts">';
// Start looping over the query results.
while ( $query->have_posts() ) {
// Set up post data.
$query->the_post();
// Set the featured image variable
$feat_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large')[0];
// Permalinks
$permalink = get_permalink();
// get the excerpt and content
$post_excerpt = get_the_excerpt();
$post_content = get_the_content();
$event_subtitle = get_post_meta( get_the_ID(), 'evcal_subtitle' );
// get other stuff
$date = get_the_date("M j, Y");
$avatar = get_avatar( get_the_author_meta( 'ID' ), 30 );
$author = get_the_author_meta( 'display_name' );
$author_posts_link = get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) );
/* get category terms via taxonomy name*/
$post_categories = get_the_terms( $post->ID, 'category' );
$event_types = get_the_terms( $post->ID, 'event_type' );
/* make default button text */
$button_text = 'Read More';
if ('ajde_events' == get_post_type()) {
$button_text = 'Event Details';
}
/* Contents of the queried post results go here. Spitting out each individual post, as: */
echo '<article id="post-'. get_the_ID() .'" class="fs-post-article '. join( ' ', get_post_class() ) .'">
<div class="fs-post-content-wrapper">';
if ( $feat_image ) {
echo '<a href="'. $permalink .'" class="fs-featured-img-link" title="'. get_the_title() .'">
<div class="fs-featured-img-wrapper" style="background-image:url('. $feat_image .');"></div>
</a>';
}
echo '<div class="fs-post-details">
<div class="fs-post-name">
<h2 class="entry-title"><a href="'. $permalink .'">'. get_the_title() .'</a></h2>
</div>
<div class="entry-meta">
<span class="byline">by <span class="author vcard"><a href="'. $author_posts_link .'">'. $author .'</a></span></span>';
if (!is_wp_error($post_categories) && !empty($post_categories) && is_object($post_categories[0])) {
echo '<span class="fs-post-terms">';
foreach( $post_categories as $category ) {
echo'<span><a href="'. get_term_link($category) .'" rel="category">' . $category->name . '</a></span> ';
}
echo '</span>';
}
elseif ( 'ajde_events' == get_post_type() ) {
echo '<span class="fs-post-terms">';
$event_in_sponsored = false;
if (!is_wp_error($event_types) && !empty($event_types) && is_object($event_types[0])) {
foreach( $event_types as $event_type ) {
if ($event_type->name == 'Sponsored') {
$event_in_sponsored = true;
}
}
}
if ($event_in_sponsored) {
echo '<span class="sponsored-event"><a href="/events/" rel="category">Sponsored Event</a></span>';
}
else {
echo '<span><a href="/events/" rel="category">Event</a></span>';
}
echo '</span>';
}
echo'<span class="published">'. $date .'</span>
</div>
<div class="fs-post-content">';
if ( !empty( $event_subtitle ) ) {
echo $event_subtitle[0];
}
elseif ( !empty( $post_excerpt ) ) {
echo $post_excerpt;
}
else {
echo $post_content;
}
echo '</div>
<div class="fs-post-button-wrapper">
<a href="'. $permalink .'">'. $button_text .'</a>
</div>
</div><!-- close fs-post-details -->
</div><!-- close fs-post-content-wrapper -->
</article>';
/*
// Used to set additional posts counter to increment by value of 1
$post_counter++;
// if post is 3rd one, add this text
if ($post_counter == 2) {
echo 'Ad 1 goes here.';
}
// if post is 6th one, add this text
if ($post_counter == 5) {
echo 'Ad 2 goes here.';
}
*/
// built-in way to get current post number (first post starts at value of 0):
if( ($query->current_post == 5) && ($ad_01_status == 'enable') ) {
echo $ad_01_display;
}
if( ($query->current_post == 8) && ($ad_02_status == 'enable') ) {
echo $ad_02_display;
}
if( ($query->current_post == 12) && ($ad_03_status == 'enable') ) {
echo $ad_03_display;
}
if( ($query->current_post == 19) && ($ad_04_status == 'enable') ) {
echo $ad_04_display;
}
if( ($query->current_post == 23) && ($ad_04_status == 'enable') ) {
echo $ad_05_display;
}
if( ($query->current_post == 31) && ($ad_06_status == 'enable') ) {
echo $ad_06_display;
}
if( ($query->current_post == 37) && ($ad_07_status == 'enable') ) {
echo $ad_07_display;
}
if( ($query->current_post == 42) && ($ad_08_status == 'enable') ) {
echo $ad_08_display;
}
// close the loop WHILE
}
wp_reset_postdata();
// close post wrapper
echo '</div>';
//function for the Easy Load More plugin
// load_more_button();
/*
echo'<div class="pagination">';
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
echo'</div>';
*/
$myvariable = ob_get_clean();
return $myvariable;
// close the query IF
}
// ELSE it cant find ANY posts that match
else {
echo '<div class="fs-no-posts-found"><h4>We currently have no stories to show you.</h4></div>';
// close the query ELSE */
}
}
/*
function insert_post_wpse_96347($posts) {
global $wp_query;
$desired_post = 157;
if (is_main_query() && 0 == get_query_var('paged')) {
$p2insert = new WP_Query(array('p'=>$desired_post,'suppress_filters'=>true));
$insert_at = 6;
if (!empty($p2insert->posts)) {
array_splice($posts,$insert_at,0,$p2insert->posts);
}
}
return $posts;
}
add_filter('posts_results','insert_post_wpse_96347');
*/
/*
add_action( 'loop_start', 'wpse_141253_loop_start' );
function wpse_141253_loop_start( $query )
{
if( $query->is_main_query() )
{
add_action( 'the_post', 'wpse_141253_the_post' );
add_action( 'loop_end', 'wpse_141253_loop_end' );
}
}
function wpse_141253_the_post()
{
static $nr = 0;
if( 0 === ++$nr % 4 )
echo '<div> -------- MY AD HERE ------- </div>';
}
function wpse_141253_loop_end()
{
remove_action( 'the_post', 'wpse_141253_the_post' );
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment