Retrieving WordPress Excerpt of a Post

I was writing a shortcode to retrieve different data for a specific post to display on a page when I come across different ways of retrieving the post excerpt, thought I would share the code snippets here.

If you want to get the default WordPress excerpt, use this:
global $post;
$your_post = get_post( 123 ); //replace 123 with your post ID
setup_postdata ( $your_post );
$scts_excerpt = get_the_excerpt();
wp_reset_postdata();

However, if you want to use the manual excerpt that you have crafted in the Edit Post page, use this:
$your_post = get_post( 123 ); //replace 123 with your post ID
$scts_excerpt = $your_post->post_excerpt;

Advertisements

Debugging PHP

Debugging PHP can be a headache if you are not using any third party software/platform.

The solution I use is calling a Javascript alert box as follows:
echo 'alert("Message to display!")';

Or if you want to display a variable, do the following:
echo 'alert("'.$var1.'")';

For array, I would use this to view the array in the developer’s console:
<script>
var var_dump = <?php echo json_encode($var1); ?>;
console.log(var_dump);
</script>

If you know of any other way, let me know!

Edit: For those who use Firebug in Firefox browser, the answers in Stackoverflow here looks good: http://stackoverflow.com/questions/14995307/sending-var-dump-to-firebug-console

Making WooCommerce Catalog Images Clickable

I spent much time doing research on making product images in my catalog clickable and there wasn’t a clear solution on how to do this so I did some trial and testing on my own and I succeeded!

Theme: Ferado
First I copied the file from parent theme (wordpress>wp-content>themes>ferado>woocommerce>content-product.php) to my child theme (wordpress>wp-content>themes>ferado-child>woocommerce>content-product.php). Locate the following code which render the product thumbnail.

<?php
  /**
  * woocommerce_before_shop_loop_item_title hook
  *
  * @hooked woocommerce_show_product_loop_sale_flash - 10
  * @hooked woocommerce_template_loop_product_thumbnail - 10
  */
  do_action( 'woocommerce_before_shop_loop_item_title' );
?>

and add the <a></a> codes highlighted in red before and after the above PHP codes.

<a href="<?php the_permalink(); ?>">
<?php
  /**
  * woocommerce_before_shop_loop_item_title hook
  *
  * @hooked woocommerce_show_product_loop_sale_flash - 10
  * @hooked woocommerce_template_loop_product_thumbnail - 10
  */
  do_action( 'woocommerce_before_shop_loop_item_title' );
?>
</a>

This applies to my theme and I hope it works for yours too!