Ask your WordPress questions! Pay money and get answers fast! (more info)

Help with a conditional statement and tag display WordPress

  • SOLVED

Hello everyone,

Need some help with a simple problem. I am currently using images for tags and outputting them in the loop. My code works properly but i would like to add a conditional statement that if an image doesn't exist for the tag nothing is displayed.

So im looking for this condiitonal:
If post has tag
//output my code
else
//do nothing

Here is my code

<?php
$separator ='';
$posttags = get_the_tags();
if ($posttags) {
$count = '1';
foreach($posttags as $tag) {
if($count == '1') {
echo '<img src="http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png" alt="tag ' . $tag->name .' icon" />';
}
$count++;
}
}
?>


Thanks for your help

Answers (3)

2013-11-11

Nirmal answers:

$separator ='';
$posttags = get_the_tags();
if ( !empty( $posttags ) ) {
foreach( $posttags as $tag ) {
$img_url = 'http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png';
$img = getimagesize( $img_url );
if( is_array( $img ) ) {
echo '<img src="'. $img_url .'" alt="tag ' . $tag->name .' icon" />';
}
else {
// just do nothing or set a default image
$default_image = 'http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/default.png';
}
}
}


Note: Assuming that you have images with name same as tag slugs. :)


George Sprouse comments:

This code does not work. It breaks the loop


Nirmal comments:

Can you paste the whole code here please ?


George Sprouse comments:

Hi Nirmal,

Here is my whole code in the loop. The tag code is under the div "tagreactions".

<?php
/**
* Posts Loop
*
* This file is responsible for outputting the
* blog posts as well as filter options (if
* the user is viewing the frontpage).
*
* In the event that the html needs to be modified,
* this template may be duplicated inside a child theme
* and edited there.
*
* @package Shaken Grid (Premium)
* @since 1.0
* @alter 2.1.3
*
***************************************************************
***************************************************************
*
* Post Filters
*
* Displays filter options if on frontpage and if they aren't
* disabled in the theme options. The filters are based on
* the categories. Each post has its category slug assigned
* as class names. The Isotope plugin handles the filtering.
*/ ?>

<?php if(is_home() && !of_get_option('hide_filters')) { ?>
<div id="filtering-nav">
<a href="#" class="filter-btn"><span><?php _e('Filter', 'shaken'); ?></span></a>
<ul>
<li><a href="#" data-filter="*"><?php _e('All', 'shaken'); ?></a></li>
<?php
$args=array(
'orderby' => 'name',
'hierarchical' => 0
);
$categories=get_categories($args);
foreach($categories as $category) { ?>
<li><a href="#" data-filter=".<?php echo $category->category_nicename; ?>"><?php echo $category->name; ?></a></li>
<?php } ?>
</ul>
<div class="clearfix"></div>
</div><!-- #filtering-nav -->
<?php } ?>

<?php
/**
* Display ALL posts
*
* If this is the homepage and the "show all posts on blog" option
* is checked in the theme options, then display all posts on one
* page without pagination.
*/
if( is_home() && !is_search() && ( of_get_option( 'show_all' ) || of_get_option( 'frontpage_category' ) ) ):

$query_string = false;

if( of_get_option('show_all') ){
$query_string = 'posts_per_page=-1';
}

if( of_get_option('frontpage_category') && of_get_option('frontpage_category') != 'all' ){

if(of_get_option('show_all')){
$query_string .= '&';
}

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_string .= 'cat='.of_get_option('frontpage_category').'&paged='.$paged;
}

if( $query_string ){
query_posts($query_string);
}

endif;

/* Say hello to the Loop... */
if ( have_posts() ) :

/* Anything placed in #sort is positioned by jQuery Masonry */ ?>
<div class="sort">

<?php // Display sidebar if it has widgets assigned to it
if( is_active_sidebar( 'gallery-sidebar' ) ) : ?>
<div class="all box col2" id="gallery-sidebar">
<div class="box-content">
<?php get_sidebar(); ?>
</div>
</div>
<?php endif; ?>

<?php // Display ads set in theme options
get_template_part( 'includes/ads'); ?>

<?php while ( have_posts() ) : the_post();

global $my_size, $force_feat_img, $embed_code, $vid_url;

// Gather custom fields
$embed_code = get_post_meta($post->ID, 'soy_vid', true);
$vid_url = get_post_meta($post->ID, 'soy_vid_url', true);
$force_feat_img = get_post_meta($post->ID, 'soy_hide_vid', true);
$show_title = get_post_meta($post->ID, 'soy_show_title', true);
$show_desc = get_post_meta($post->ID, 'soy_show_desc', true);
$box_size = get_post_meta($post->ID, 'soy_box_size', true);

if( $box_size == 'Medium (485px)' ){
$my_size = 'col3';
$embed_size = '495';
} else if( $box_size == 'Large (660px)' ){
$my_size = 'col4';
$embed_size = '670';
} else if( $box_size == 'Tiny (135px)' ){
$my_size = 'col1';
$embed_size = '145';
}else{
$my_size = 'col2';
$embed_size = '320';
}

/* Check whether content is being displayed
* This determines whether a border should be applied
* above the postmeta section
*/
if($show_title != 'No'){
$content_class = 'has-content';
} else if($show_desc != 'No' && $post->post_content){
$content_class = 'has-content';
}else {
$content_class = 'no-content';
}

// Assign categories as class names to enable filtering
$category_classes = '';

foreach( ( get_the_category() ) as $category ) {
$category_classes .= $category->category_nicename . ' ';
}
?>

<div class="all box <?php echo $category_classes . $my_size; ?>">


<div <?php post_class( 'box-content '.$content_class ) ?>>
<?php
// Display video if available
if( ( $embed_code || $vid_url ) && !$force_feat_img ):

if( $vid_url ){
echo '<div class="vid-container">'.apply_filters('the_content', '[embed width="' . $embed_size . '"]' . $vid_url . '[/embed]').'</div>';
} else {
echo '<div class="vid-container">'.$embed_code.'</div>';
}

// Display gallery
elseif( has_post_format( 'gallery' ) && !$force_feat_img ):

get_template_part( 'includes/loop-gallery' );

// Display featured image
elseif ( has_post_thumbnail() ): ?>



<div class="img-container">
<div class="tagreactions">
<?php
$separator ='';
$posttags = get_the_tags();
if ($posttags) {
$count = '1';
foreach($posttags as $tag) {
if($count == '1') {
echo '<img src="http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png" alt="tag ' . $tag->name .' icon" />';
}
$count++;
}
}
?>
</div>
<?php
// Display the appropriate sized featured image
if ( in_category(4)):?>

<?php elseif( $my_size != 'col2' ): ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail($my_size, array( 'class' => 'feat-img' ) ); ?></a>
<?php else: ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('post-thumbnail', array( 'class' => 'feat-img' ) ); ?></a>
<?php endif;

// Display View/Share/Comment buttons
if( !of_get_option( 'hide_action_buttons' ) ) :
get_template_part( 'includes/action-buttons' );
endif; ?>
</div><!-- #img-container -->



<?php if( has_post_format( 'gallery' ) ) get_template_part( 'includes/gallery-list' ); ?>

<?php endif; // #has_post_thumbnail() ?>

<div class="post-content">

<?php // Display post title
if( $show_title != 'No' && !has_post_format('quote') ): ?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
<?php the_title(); ?>
</a></h2>
<?php endif;

// Display post content

if( in_category(4)):

the_content(); // Only display excerpts for search results

elseif( $show_desc != 'No' ):

if( has_excerpt() ){

the_excerpt();

} else{
the_excerpt(__('Continue Reading &rarr;', 'shaken'));
}

endif; ?>
<font color="#6f777d">Posted <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></font>
<?php edit_post_link(__('Edit this post', 'shaken')); ?>
</div><!-- #entry -->

<?php // Display post footer
if( $my_size != 'col1' ): ?>
<div class="post-footer">
<span class="category-ic"><?php the_category(', '); ?></span>

<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php userphoto_the_author_photo( $curauth ->display_name); ?></a>
<?php endif; ?>
</div>

</div><!-- #box-content -->
</div><!-- #box -->
<?php endwhile; ?>
</div><!-- #sort -->

<?php // Display pagination when applicable
if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older', 'shaken') ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer <span class="meta-nav">&rarr;</span>', 'shaken') ); ?></div>
<div class="clearfix"></div>
</div><!-- #nav-below -->
<?php endif; ?>

<?php else :
/* If there are no posts */ ?>
<div id="sort">
<div class="box">
<div class="box-content not-found">
<h2><?php _e('Sorry, no posts were found', 'shaken'); ?></h2>
<?php get_search_form(); ?>
</div><!-- #not-found -->
</div>
</div><!-- #sort -->
<?php endif; ?>


Nirmal comments:

<?php
/**
* Posts Loop
* This file is responsible for outputting the
* blog posts as well as filter options (if
* the user is viewing the frontpage).
* In the event that the html needs to be modified,
* this template may be duplicated inside a child theme
* and edited there.
* @package Shaken Grid (Premium)
* @since 1.0
* @alter 2.1.3
***************************************************************
***************************************************************
* Post Filters
* Displays filter options if on frontpage and if they aren't
* disabled in the theme options. The filters are based on
* the categories. Each post has its category slug assigned
* as class names. The Isotope plugin handles the filtering.
*/
?>

<?php if ( is_home() && !of_get_option( 'hide_filters' ) ) { ?>
<div id="filtering-nav">
<a href="#" class="filter-btn"><span><?php _e( 'Filter', 'shaken' ); ?></span></a>
<ul>
<li><a href="#" data-filter="*"><?php _e( 'All', 'shaken' ); ?></a></li>
<?php
$args = array(
'orderby' => 'name',
'hierarchical' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
?>
<li><a href="#" data-filter=".<?php echo $category->category_nicename; ?>"><?php echo $category->name; ?></a></li>
<?php } ?>
</ul>
<div class="clearfix"></div>
</div><!-- #filtering-nav -->
<?php } ?>

<?php
/**
* Display ALL posts
* If this is the homepage and the "show all posts on blog" option
* is checked in the theme options, then display all posts on one
* page without pagination.
*/
if ( is_home() && !is_search() && ( of_get_option( 'show_all' ) || of_get_option( 'frontpage_category' ) ) ):

$query_string = false;

if ( of_get_option( 'show_all' ) ) {
$query_string = 'posts_per_page=-1';
}

if ( of_get_option( 'frontpage_category' ) && of_get_option( 'frontpage_category' ) != 'all' ) {

if ( of_get_option( 'show_all' ) ) {
$query_string .= '&';
}

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query_string .= 'cat=' . of_get_option( 'frontpage_category' ) . '&paged=' . $paged;
}

if ( $query_string ) {
query_posts( $query_string );
}

endif;

/* Say hello to the Loop... */
if ( have_posts() ) :

/* Anything placed in #sort is positioned by jQuery Masonry */
?>
<div class="sort">

<?php // Display sidebar if it has widgets assigned to it
if ( is_active_sidebar( 'gallery-sidebar' ) ) : ?>
<div class="all box col2" id="gallery-sidebar">
<div class="box-content">
<?php get_sidebar(); ?>
</div>
</div>
<?php endif; ?>

<?php // Display ads set in theme options
get_template_part( 'includes/ads' ); ?>

<?php while ( have_posts() ) : the_post();

global $my_size, $force_feat_img, $embed_code, $vid_url;

// Gather custom fields
$embed_code = get_post_meta( $post->ID, 'soy_vid', true );
$vid_url = get_post_meta( $post->ID, 'soy_vid_url', true );
$force_feat_img = get_post_meta( $post->ID, 'soy_hide_vid', true );
$show_title = get_post_meta( $post->ID, 'soy_show_title', true );
$show_desc = get_post_meta( $post->ID, 'soy_show_desc', true );
$box_size = get_post_meta( $post->ID, 'soy_box_size', true );

if ( $box_size == 'Medium (485px)' ) {
$my_size = 'col3';
$embed_size = '495';
}
else if ( $box_size == 'Large (660px)' ) {
$my_size = 'col4';
$embed_size = '670';
}
else if ( $box_size == 'Tiny (135px)' ) {
$my_size = 'col1';
$embed_size = '145';
}
else {
$my_size = 'col2';
$embed_size = '320';
}

/* Check whether content is being displayed
* This determines whether a border should be applied
* above the postmeta section
*/
if ( $show_title != 'No' ) {
$content_class = 'has-content';
}
else if ( $show_desc != 'No' && $post->post_content ) {
$content_class = 'has-content';
}
else {
$content_class = 'no-content';
}

// Assign categories as class names to enable filtering
$category_classes = '';

foreach ( ( get_the_category() ) as $category ) {
$category_classes .= $category->category_nicename . ' ';
}
?>

<div class="all box <?php echo $category_classes . $my_size; ?>">


<div <?php post_class( 'box-content ' . $content_class ) ?>>
<?php
// Display video if available
if ( ( $embed_code || $vid_url ) && !$force_feat_img ):

if ( $vid_url ) {
echo '<div class="vid-container">' . apply_filters( 'the_content', '[embed width="' . $embed_size . '"]' . $vid_url . '[/embed]' ) . '</div>';
}
else {
echo '<div class="vid-container">' . $embed_code . '</div>';
}

// Display gallery
elseif ( has_post_format( 'gallery' ) && !$force_feat_img ):

get_template_part( 'includes/loop-gallery' );

// Display featured image
elseif ( has_post_thumbnail() ): ?>



<div class="img-container">
<div class="tagreactions">
<?php
$separator ='';
$posttags = get_the_tags();
if ( !empty( $posttags ) ) {
foreach( $posttags as $tag ) {
$img_url = 'http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png';
$img = @getimagesize( $img_url );
if( is_array( $img ) ) {
echo '<img src="'. $img_url .'" alt="tag ' . $tag->name .' icon" />';
}
}
}
?>
</div>
<?php
// Display the appropriate sized featured image
if ( in_category( 4 ) ):?>

<?php elseif ( $my_size != 'col2' ): ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( $my_size, array( 'class' => 'feat-img' ) ); ?></a>
<?php
else: ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'post-thumbnail', array( 'class' => 'feat-img' ) ); ?></a>
<?php endif;

// Display View/Share/Comment buttons
if ( !of_get_option( 'hide_action_buttons' ) ) :
get_template_part( 'includes/action-buttons' );
endif; ?>
</div><!-- #img-container -->



<?php if ( has_post_format( 'gallery' ) )
get_template_part( 'includes/gallery-list' ); ?>

<?php endif; // #has_post_thumbnail() ?>

<div class="post-content">

<?php // Display post title
if ( $show_title != 'No' && !has_post_format( 'quote' ) ): ?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
<?php the_title(); ?>
</a></h2>
<?php endif;

// Display post content

if ( in_category( 4 ) ):

the_content(); // Only display excerpts for search results

elseif ( $show_desc != 'No' ):

if ( has_excerpt() ) {

the_excerpt();

}
else {
the_excerpt( __( 'Continue Reading &rarr;', 'shaken' ) );
}

endif; ?>
<font color="#6f777d">Posted <?php echo human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago'; ?></font>
<?php edit_post_link( __( 'Edit this post', 'shaken' ) ); ?>
</div><!-- #entry -->

<?php // Display post footer
if ($my_size != 'col1'): ?>
<div class="post-footer">
<span class="category-ic"><?php the_category( ', ' ); ?></span>

<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php userphoto_the_author_photo( $curauth->display_name ); ?></a>
<?php endif; ?>
</div>

</div><!-- #box-content -->
</div><!-- #box -->
<?php endwhile; ?>
</div><!-- #sort -->

<?php // Display pagination when applicable
if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older', 'shaken' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer <span class="meta-nav">&rarr;</span>', 'shaken' ) ); ?></div>
<div class="clearfix"></div>
</div><!-- #nav-below -->
<?php endif; ?>

<?php
else :
/* If there are no posts */
?>
<div id="sort">
<div class="box">
<div class="box-content not-found">
<h2><?php _e( 'Sorry, no posts were found', 'shaken' ); ?></h2>
<?php get_search_form(); ?>
</div><!-- #not-found -->
</div>
</div><!-- #sort -->
<?php endif; ?>


Nirmal comments:

Did this code work ?


George Sprouse comments:

Hey this seems to work but it is making the loading times for pages very slow. Can you check over the code and see if somethign can be causing the slow load time.

If you go on my website now you can see the slow loading time. If i remove the code the pages load fast

Thanks


Nirmal comments:

Try this code. It should be faster !!
<?php
/**
* Posts Loop
* This file is responsible for outputting the
* blog posts as well as filter options (if
* the user is viewing the frontpage).
* In the event that the html needs to be modified,
* this template may be duplicated inside a child theme
* and edited there.
* @package Shaken Grid (Premium)
* @since 1.0
* @alter 2.1.3
***************************************************************
***************************************************************
* Post Filters
* Displays filter options if on frontpage and if they aren't
* disabled in the theme options. The filters are based on
* the categories. Each post has its category slug assigned
* as class names. The Isotope plugin handles the filtering.
*/
?>

<?php if ( is_home() && !of_get_option( 'hide_filters' ) ) { ?>
<div id="filtering-nav">
<a href="#" class="filter-btn"><span><?php _e( 'Filter', 'shaken' ); ?></span></a>
<ul>
<li><a href="#" data-filter="*"><?php _e( 'All', 'shaken' ); ?></a></li>
<?php
$args = array(
'orderby' => 'name',
'hierarchical' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
?>
<li><a href="#" data-filter=".<?php echo $category->category_nicename; ?>"><?php echo $category->name; ?></a></li>
<?php } ?>
</ul>
<div class="clearfix"></div>
</div><!-- #filtering-nav -->
<?php } ?>

<?php
/**
* Display ALL posts
* If this is the homepage and the "show all posts on blog" option
* is checked in the theme options, then display all posts on one
* page without pagination.
*/
if ( is_home() && !is_search() && ( of_get_option( 'show_all' ) || of_get_option( 'frontpage_category' ) ) ):

$query_string = false;
if ( of_get_option( 'show_all' ) ) {
$query_string = 'posts_per_page=-1';
}
if ( of_get_option( 'frontpage_category' ) && of_get_option( 'frontpage_category' ) != 'all' ) {
if ( of_get_option( 'show_all' ) ) {
$query_string .= '&';
}
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query_string .= 'cat=' . of_get_option( 'frontpage_category' ) . '&paged=' . $paged;
}
if ( $query_string ) {
query_posts( $query_string );
}

endif;
/* Say hello to the Loop... */
if ( have_posts() ) :

/* Anything placed in #sort is positioned by jQuery Masonry */
?>
<div class="sort">

<?php // Display sidebar if it has widgets assigned to it
if ( is_active_sidebar( 'gallery-sidebar' ) ) : ?>
<div class="all box col2" id="gallery-sidebar">
<div class="box-content">
<?php get_sidebar(); ?>
</div>
</div>
<?php endif; ?>

<?php // Display ads set in theme options
get_template_part( 'includes/ads' ); ?>

<?php while ( have_posts() ) : the_post();
global $my_size, $force_feat_img, $embed_code, $vid_url;
// Gather custom fields
$embed_code = get_post_meta( $post->ID, 'soy_vid', true );
$vid_url = get_post_meta( $post->ID, 'soy_vid_url', true );
$force_feat_img = get_post_meta( $post->ID, 'soy_hide_vid', true );
$show_title = get_post_meta( $post->ID, 'soy_show_title', true );
$show_desc = get_post_meta( $post->ID, 'soy_show_desc', true );
$box_size = get_post_meta( $post->ID, 'soy_box_size', true );
if ( $box_size == 'Medium (485px)' ) {
$my_size = 'col3';
$embed_size = '495';
}
else if ( $box_size == 'Large (660px)' ) {
$my_size = 'col4';
$embed_size = '670';
}
else if ( $box_size == 'Tiny (135px)' ) {
$my_size = 'col1';
$embed_size = '145';
}
else {
$my_size = 'col2';
$embed_size = '320';
}
/* Check whether content is being displayed
* This determines whether a border should be applied
* above the postmeta section
*/
if ( $show_title != 'No' ) {
$content_class = 'has-content';
}
else if ( $show_desc != 'No' && $post->post_content ) {
$content_class = 'has-content';
}
else {
$content_class = 'no-content';
}
// Assign categories as class names to enable filtering
$category_classes = '';
foreach ( ( get_the_category() ) as $category ) {
$category_classes .= $category->category_nicename . ' ';
}
?>

<div class="all box <?php echo $category_classes . $my_size; ?>">


<div <?php post_class( 'box-content ' . $content_class ) ?>>
<?php
// Display video if available
if ( ( $embed_code || $vid_url ) && !$force_feat_img ):

if ( $vid_url ) {
echo '<div class="vid-container">' . apply_filters( 'the_content', '[embed width="' . $embed_size . '"]' . $vid_url . '[/embed]' ) . '</div>';
}
else {
echo '<div class="vid-container">' . $embed_code . '</div>';
}
// Display gallery
elseif ( has_post_format( 'gallery' ) && !$force_feat_img ):

get_template_part( 'includes/loop-gallery' );
// Display featured image
elseif ( has_post_thumbnail() ): ?>



<div class="img-container">
<div class="tagreactions">
<?php
$separator = '';
$posttags = get_the_tags();
if ( !empty( $posttags ) ) {
foreach ( $posttags as $tag ) {
$image_path = get_stylesheet_directory() . '/images/' . $tag->slug . '.png';
if ( file_exists( $image_path ) ) {
$img_url = 'http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png';
echo '<img src="' . $img_url . '" alt="tag ' . $tag->name . ' icon" />';
}
}
}
?>
</div>
<?php
// Display the appropriate sized featured image
if ( in_category( 4 ) ):?>

<?php elseif ( $my_size != 'col2' ): ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( $my_size, array( 'class' => 'feat-img' ) ); ?></a>
<?php
else: ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'post-thumbnail', array( 'class' => 'feat-img' ) ); ?></a>
<?php endif;

// Display View/Share/Comment buttons
if ( !of_get_option( 'hide_action_buttons' ) ) :
get_template_part( 'includes/action-buttons' );
endif; ?>
</div><!-- #img-container -->



<?php if ( has_post_format( 'gallery' ) )
get_template_part( 'includes/gallery-list' ); ?>

<?php endif; // #has_post_thumbnail() ?>

<div class="post-content">

<?php // Display post title
if ( $show_title != 'No' && !has_post_format( 'quote' ) ): ?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
<?php the_title(); ?>
</a></h2>
<?php endif;

// Display post content

if ( in_category( 4 ) ):

the_content(); // Only display excerpts for search results
elseif ( $show_desc != 'No' ):

if ( has_excerpt() ) {
the_excerpt();
}
else {
the_excerpt( __( 'Continue Reading &rarr;', 'shaken' ) );
}

endif; ?>
<font color="#6f777d">Posted <?php echo human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago'; ?></font>
<?php edit_post_link( __( 'Edit this post', 'shaken' ) ); ?>
</div><!-- #entry -->

<?php // Display post footer
if ($my_size != 'col1'): ?>
<div class="post-footer">
<span class="category-ic"><?php the_category( ', ' ); ?></span>

<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php userphoto_the_author_photo( $curauth->display_name ); ?></a>
<?php endif; ?>
</div>

</div><!-- #box-content -->
</div><!-- #box -->
<?php endwhile; ?>
</div><!-- #sort -->

<?php // Display pagination when applicable
if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older', 'shaken' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer <span class="meta-nav">&rarr;</span>', 'shaken' ) ); ?></div>
<div class="clearfix"></div>
</div><!-- #nav-below -->
<?php endif; ?>

<?php
else :
/* If there are no posts */
?>
<div id="sort">
<div class="box">
<div class="box-content not-found">
<h2><?php _e( 'Sorry, no posts were found', 'shaken' ); ?></h2>
<?php get_search_form(); ?>
</div><!-- #not-found -->
</div>
</div><!-- #sort -->
<?php endif; ?>


Nirmal comments:

Hi George, Let me know if my updated code made loading faster. Thanks !!


George Sprouse comments:

Hey Nirmal,

The code works and it is faster! :-) Thank you very much! Im also having a problem with my infinite scroll script on my site (Gird doesnt have correct alignment on first load) . I will be opening a new question for this maybe you can take a look and see if you can help with this also.

Thanks Again

2013-11-11

zebra webdesigns answers:

Hello George

You can use the javascript method as suggested in the below post
http://stackoverflow.com/questions/3235913/how-to-silently-hide-image-not-found-icon-when-src-source-image-is-not-found

or you can have the image name as tagname_image.png

so when the tag is available you can show the correct image,
If not you can use the default transparant image or show an image which has the test image not availbale .
where the default image has the name
_image.png


George Sprouse comments:

Hi im not too good with javascript but ill take a look at it. In order to display a default image i would need a conditional which is what im looking for.


George Sprouse comments:

This seems like a good option do you know where i would put the javascript code?

2013-11-11

Arnav Joy answers:

try this

<?php

$separator ='';

$posttags = get_the_tags();

if ($posttags) {

$count = '1';

foreach($posttags as $tag) {

$image_path = get_stylesheet_directory().'/'.$tag->slug . '.png'

if($count == '1') {

if (file_exists($image_path)) {

echo '<img src="http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png" alt="tag ' . $tag->name .' icon" />';

}
else{
//do you work here
}

$count++;

}

}
}


George Sprouse comments:

Doesnt work :-/ The code breaks the loop


Arnav Joy comments:

here is the full code

<?php
/**
* Posts Loop
*
* This file is responsible for outputting the
* blog posts as well as filter options (if
* the user is viewing the frontpage).
*
* In the event that the html needs to be modified,
* this template may be duplicated inside a child theme
* and edited there.
*
* @package Shaken Grid (Premium)
* @since 1.0
* @alter 2.1.3
*
***************************************************************
***************************************************************
*
* Post Filters
*
* Displays filter options if on frontpage and if they aren't
* disabled in the theme options. The filters are based on
* the categories. Each post has its category slug assigned
* as class names. The Isotope plugin handles the filtering.
*/ ?>

<?php if(is_home() && !of_get_option('hide_filters')) { ?>
<div id="filtering-nav">
<a href="#" class="filter-btn"><span><?php _e('Filter', 'shaken'); ?></span></a>
<ul>
<li><a href="#" data-filter="*"><?php _e('All', 'shaken'); ?></a></li>
<?php
$args=array(
'orderby' => 'name',
'hierarchical' => 0
);
$categories=get_categories($args);
foreach($categories as $category) { ?>
<li><a href="#" data-filter=".<?php echo $category->category_nicename; ?>"><?php echo $category->name; ?></a></li>
<?php } ?>
</ul>
<div class="clearfix"></div>
</div><!-- #filtering-nav -->
<?php } ?>

<?php
/**
* Display ALL posts
*
* If this is the homepage and the "show all posts on blog" option
* is checked in the theme options, then display all posts on one
* page without pagination.
*/
if( is_home() && !is_search() && ( of_get_option( 'show_all' ) || of_get_option( 'frontpage_category' ) ) ):

$query_string = false;

if( of_get_option('show_all') ){
$query_string = 'posts_per_page=-1';
}

if( of_get_option('frontpage_category') && of_get_option('frontpage_category') != 'all' ){

if(of_get_option('show_all')){
$query_string .= '&';
}

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_string .= 'cat='.of_get_option('frontpage_category').'&paged='.$paged;
}

if( $query_string ){
query_posts($query_string);
}

endif;

/* Say hello to the Loop... */
if ( have_posts() ) :

/* Anything placed in #sort is positioned by jQuery Masonry */ ?>
<div class="sort">

<?php // Display sidebar if it has widgets assigned to it
if( is_active_sidebar( 'gallery-sidebar' ) ) : ?>
<div class="all box col2" id="gallery-sidebar">
<div class="box-content">
<?php get_sidebar(); ?>
</div>
</div>
<?php endif; ?>

<?php // Display ads set in theme options
get_template_part( 'includes/ads'); ?>

<?php while ( have_posts() ) : the_post();

global $my_size, $force_feat_img, $embed_code, $vid_url;

// Gather custom fields
$embed_code = get_post_meta($post->ID, 'soy_vid', true);
$vid_url = get_post_meta($post->ID, 'soy_vid_url', true);
$force_feat_img = get_post_meta($post->ID, 'soy_hide_vid', true);
$show_title = get_post_meta($post->ID, 'soy_show_title', true);
$show_desc = get_post_meta($post->ID, 'soy_show_desc', true);
$box_size = get_post_meta($post->ID, 'soy_box_size', true);

if( $box_size == 'Medium (485px)' ){
$my_size = 'col3';
$embed_size = '495';
} else if( $box_size == 'Large (660px)' ){
$my_size = 'col4';
$embed_size = '670';
} else if( $box_size == 'Tiny (135px)' ){
$my_size = 'col1';
$embed_size = '145';
}else{
$my_size = 'col2';
$embed_size = '320';
}

/* Check whether content is being displayed
* This determines whether a border should be applied
* above the postmeta section
*/
if($show_title != 'No'){
$content_class = 'has-content';
} else if($show_desc != 'No' && $post->post_content){
$content_class = 'has-content';
}else {
$content_class = 'no-content';
}

// Assign categories as class names to enable filtering
$category_classes = '';

foreach( ( get_the_category() ) as $category ) {
$category_classes .= $category->category_nicename . ' ';
}
?>

<div class="all box <?php echo $category_classes . $my_size; ?>">


<div <?php post_class( 'box-content '.$content_class ) ?>>
<?php
// Display video if available
if( ( $embed_code || $vid_url ) && !$force_feat_img ):

if( $vid_url ){
echo '<div class="vid-container">'.apply_filters('the_content', '[embed width="' . $embed_size . '"]' . $vid_url . '[/embed]').'</div>';
} else {
echo '<div class="vid-container">'.$embed_code.'</div>';
}

// Display gallery
elseif( has_post_format( 'gallery' ) && !$force_feat_img ):

get_template_part( 'includes/loop-gallery' );

// Display featured image
elseif ( has_post_thumbnail() ): ?>



<div class="img-container">
<div class="tagreactions">
<?php
$separator ='';
$posttags = get_the_tags();
if ($posttags) {

$count = '1';
foreach($posttags as $tag) {
if($count == '1') {
$image_path = get_stylesheet_directory().'/'.$tag->slug . '.png' ;
if (file_exists($image_path))
echo '<img src="http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png" alt="tag ' . $tag->name .' icon" />';
else
;// do your stuff
}
$count++;
}
}
?>
</div>
<?php
// Display the appropriate sized featured image
if ( in_category(4)):?>

<?php elseif( $my_size != 'col2' ): ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail($my_size, array( 'class' => 'feat-img' ) ); ?></a>
<?php else: ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('post-thumbnail', array( 'class' => 'feat-img' ) ); ?></a>
<?php endif;

// Display View/Share/Comment buttons
if( !of_get_option( 'hide_action_buttons' ) ) :
get_template_part( 'includes/action-buttons' );
endif; ?>
</div><!-- #img-container -->



<?php if( has_post_format( 'gallery' ) ) get_template_part( 'includes/gallery-list' ); ?>

<?php endif; // #has_post_thumbnail() ?>

<div class="post-content">

<?php // Display post title
if( $show_title != 'No' && !has_post_format('quote') ): ?>
<h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
<?php the_title(); ?>
</a></h2>
<?php endif;

// Display post content

if( in_category(4)):

the_content(); // Only display excerpts for search results

elseif( $show_desc != 'No' ):

if( has_excerpt() ){

the_excerpt();

} else{
the_excerpt(__('Continue Reading &rarr;', 'shaken'));
}

endif; ?>
<font color="#6f777d">Posted <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></font>
<?php edit_post_link(__('Edit this post', 'shaken')); ?>
</div><!-- #entry -->

<?php // Display post footer
if( $my_size != 'col1' ): ?>
<div class="post-footer">
<span class="category-ic"><?php the_category(', '); ?></span>

<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php userphoto_the_author_photo( $curauth ->display_name); ?></a>
<?php endif; ?>
</div>

</div><!-- #box-content -->
</div><!-- #box -->
<?php endwhile; ?>
</div><!-- #sort -->

<?php // Display pagination when applicable
if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older', 'shaken') ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer <span class="meta-nav">&rarr;</span>', 'shaken') ); ?></div>
<div class="clearfix"></div>
</div><!-- #nav-below -->
<?php endif; ?>

<?php else :
/* If there are no posts */ ?>
<div id="sort">
<div class="box">
<div class="box-content not-found">
<h2><?php _e('Sorry, no posts were found', 'shaken'); ?></h2>
<?php get_search_form(); ?>
</div><!-- #not-found -->
</div>
</div><!-- #sort -->
<?php endif; ?>


George Sprouse comments:

Hey, this doesn't work no tag images are output.


Arnav Joy comments:

can you let me if what you got in this code?

if ($posttags) {



$count = '1';

foreach($posttags as $tag) {

if($count == '1') {

echo $image_path = get_stylesheet_directory().'/'.$tag->slug . '.png' ;

echo '<br>';

if (file_exists($image_path))

echo '<img src="http://www.watchthefeed.com/wp-content/themes/shaken-grid-premium/images/' . $tag->slug . '.png" alt="tag ' . $tag->name .' icon" />';

else

;// do your stuff

}

$count++;

}

}


George Sprouse comments:

Hi not sure what your question is. I applied your code but no images are displayed


Arnav Joy comments:

test my above code and let me know what output you got

i written following line to see what it produces

echo $image_path = get_stylesheet_directory().'/'.$tag->slug . '.png' ;



echo '<br>';


George Sprouse comments:

Hey Arnav tried this code and it still doesn't display an image.


Arnav Joy comments:

it will not display image , it will print path of the image , did you got any message ?


George Sprouse comments:

Yea it doesn't output anything. No html at all