Created
March 7, 2016 23:59
-
-
Save BurlesonBrad/62e3af6f98d5f7227e03 to your computer and use it in GitHub Desktop.
Christopher Roy's Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Christopher's Plugin | |
* Description: Pucker, Pray, and hope that I didn't miss anything. Should make prev & next stay within the same category! *SHOULD* ;-) | |
* Version: 1.0.0 | |
* Author: Brad Griffin | |
* Author URI: https://bradgriffin.me | |
*/ | |
/** Christopher: Download this file, then upload it via FTP into your /plugins folder. | |
* I gotta shoot straight with 'ya, when I get to coding without a third party looking | |
* over the code, and adding another set of eyeballs, there's always a chance I missed | |
* something. If this doesn't work, just let me know in the comments below. I'll be | |
* checking with some other people as well to get their input. In the time being, | |
* give this a whirl and tell me what happens ;-) | |
* / | |
function ShowLinkToProduct($post_id, $categories_as_array, $label) { | |
// get post according post id | |
$query_args = array( 'post__in' => array($post_id), 'posts_per_page' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array( | |
array( | |
'taxonomy' => 'product_cat', | |
'field' => 'id', | |
'terms' => $categories_as_array | |
))); | |
$r_single = new WP_Query($query_args); | |
if ($r_single->have_posts()) { | |
$r_single->the_post(); | |
global $product; | |
?> | |
<ul class="product_list_widget"> | |
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"> | |
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?> | |
<?php echo $label; ?> | |
<?php if ( get_the_title() ) the_title(); else the_ID(); ?> | |
</a> <?php echo $product->get_price_html(); ?></li> | |
</ul> | |
<?php | |
wp_reset_query(); | |
} | |
} | |
if ( is_singular('product') ) { | |
global $post; | |
// get categories | |
$terms = wp_get_post_terms( $post->ID, 'product_cat' ); | |
foreach ( $terms as $term ) $cats_array[] = $term->term_id; | |
// get all posts in current categories | |
$query_args = array('posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array( | |
array( | |
'taxonomy' => 'product_cat', | |
'field' => 'id', | |
'terms' => $cats_array | |
))); | |
$r = new WP_Query($query_args); | |
// show next and prev only if we have 3 or more | |
if ($r->post_count > 2) { | |
$prev_product_id = -1; | |
$next_product_id = -1; | |
$found_product = false; | |
$i = 0; | |
$current_product_index = $i; | |
$current_product_id = get_the_ID(); | |
if ($r->have_posts()) { | |
while ($r->have_posts()) { | |
$r->the_post(); | |
$current_id = get_the_ID(); | |
if ($current_id == $current_product_id) { | |
$found_product = true; | |
$current_product_index = $i; | |
} | |
$is_first = ($current_product_index == $first_product_index); | |
if ($is_first) { | |
$prev_product_id = get_the_ID(); // if product is first then 'prev' = last product | |
} else { | |
if (!$found_product && $current_id != $current_product_id) { | |
$prev_product_id = get_the_ID(); | |
} | |
} | |
if ($i == 0) { // if product is last then 'next' = first product | |
$next_product_id = get_the_ID(); | |
} | |
if ($found_product && $i == $current_product_index + 1) { | |
$next_product_id = get_the_ID(); | |
} | |
$i++; | |
} | |
if ($prev_product_id != -1) { ShowLinkToProduct($prev_product_id, $cats_array, "next: "); } | |
if ($next_product_id != -1) { ShowLinkToProduct($next_product_id, $cats_array, "prev: "); } | |
} | |
wp_reset_query(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment