Woocommerce Display Categories with images.

There are some scenarios for adding php coded instead of shorcodes for displaying categories in woocommerce website We can use the below code for the same

<div class=”menu-categs-box”>
<?php $wcatTerms = get_terms(‘product_cat’, array(‘hide_empty’ => 0, ‘orderby’ => ‘ASC’, ‘parent’ =>0)); //, ‘exclude’ => ‘17,77’
foreach($wcatTerms as $wcatTerm) :
$wthumbnail_id = get_woocommerce_term_meta( $wcatTerm->term_id, ‘thumbnail_id’, true );
$wimage = wp_get_attachment_url( $wthumbnail_id );
?>
<ul>
<li class=”libreak”><?php if($wimage!=””):?><img src=”<?php echo $wimage?>”><?php endif;?></li>
<li>
<a href=”<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>”><?php echo $wcatTerm->name; ?></a>
<ul class=”wsubcategs”>
<?php
$wsubargs = array(
‘hierarchical’ => 1,
‘show_option_none’ => ”,
‘hide_empty’ => 0,
‘parent’ => $wcatTerm->term_id,
‘taxonomy’ => ‘product_cat’
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li><a href=”<?php echo get_term_link( $wsc->slug, $wsc->taxonomy );?>”><?php echo $wsc->name;?></a></li>
<?php
endforeach;
?>
</ul>
</li>
</ul>
<?php
endforeach;
?>
</div>

List Pages Heirarchy in Sidebar

<?php
$page_ids = sw_get_all_page_ids();
$exclude_page_ids= array(11,2,4,6,8);
global $wp_query;

$current_page = 0;
if (is_page()) {
$current_page = $wp_query->get_queried_object_id();
}

foreach($page_ids as $page_id) {
if(!sw_get_page_parent_id($page_id) &&  !(in_array($page_id, $exclude_page_ids)) ) { //If (1)

if($page_id == $current_page) {
$class=’id=”currentpage_h1″‘;
} else  {
$class=”;
}
?>
<div>
<h1 <?php echo $class; ?>><a href=”<?php echo get_page_link($page_id); ?>”><?php echo get_the_title($page_id); ?></a></h1>

<?php     if ( ($page_id == $current_page) || ($page_id == sw_get_page_parent_id($current_page)) ) { ?>

<ul id=”subpages”>
<?php wp_list_pages(“title_li=&depth=0&child_of=”.$page_id);?>
</ul>

<?php } ?>
</div>
<?php
}//End of If(1)

} // End of foreach ($page_ids
?>

Content limit

<?php
function chlimit($len, $terminating_string) {
global $post;
$text=get_the_content();
$text=strip_tags($text);
$text=substr($text,0,$len). $terminating_string;
return wpautop($text, 0);
}
?>
————————-
<?php echo chlimit(500, ‘….’); ?>

Excerpt functions:

Add read more text:

function new_jr($more) {
global $post;
return ‘<a href=”‘. get_permalink($post->ID) . ‘”><i style=”font-size:12px;”> &nbsp;Read More…</i></a>’;
}
add_filter(‘excerpt_more’, ‘jr’);

Control length:

function jr_length($length) {
return 20;
}
add_filter(‘excerpt_length’, ‘jr_length’);

 

Pagination Query

Query:

<?php
global $paged;
if(!$paged) {
query_posts(‘post_type=articles&showposts=6’);
}else {
query_posts(‘post_type=articles&showposts=6&paged=’ . $paged);
}
?>

After end while and before end if:

<div>
<?php previous_posts_link(); ?>
</div>
<div>
<?php next_posts_link(); ?>
</div>

 

 

Random image on refresh using custom field

Tags

, ,

<script type=”text/javascript” language=”javascript”>
var theImages = new Array()
<?php $n = get_post_meta($post->ID, ‘Number’, true); ?>

<?php for($i=0; $i<$n; $i++) { ?>
<?php $cur_image = get_post_meta($post->ID, ‘Image’.$i, true); ?>
theImages[<?php echo $i; ?>] = ‘<?php echo $cur_image; ?>’
<?php } ?>

var j = 0
var p = theImages.length;
var preBuffer = new Array()

for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));

function showImage(){
document.write(‘<a href=”https://www.webrecon.com/b/homepage/”><img src=”‘+theImages[whichImage]+'” alt=” ” /></a>’);
}
</script>
<script type=”text/javascript”>showImage();</script>