Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to display an icon in the post meta data in the loop.php, for each post have a specific meta_value!

To be more specific i use this code to display an icon when a new posts has been published and this lasts for 24 hours:

<?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?>
<span class="new-post"></span>
<?php endif; ?>

And here is the code in style.css:

.item-list span.new-post {
padding-left: 20px;
width: 20px;
height: 18px;
background: 0px 0px url(http://www.e-win.gr/wp-content/themes/e-win/images/new-post.png) no-repeat;
}

What exactly i want is display an icon when a post has a specific meta_value. Here is the code i am trying to reach it but with no results:

<?php $icon = get_post_meta(apaitei_logariasmo_facebook); ?>
<?php if( ! $icon == attr_esc(Ναι) ) ?>
<span class="facebook-badge"></span>
<?php endif; ?>

And here is the code i use in style.css to insert the icon:

.item-list span.facebook-badge {
padding-left:20px;
width: 20px;
height: 14px;
background: 0px 0px url(http://www.e-win.gr/wp-content/themes/e-win/images/facebook-badge.png) no-repeat;
}

What do i have to do to make the code work and display the icon for the specific meta_value?

P.S. I am using this attr_esc(Ναι) because i use Greek chars in the meta_value. Also i am using the "Advanced Custom Fields plugin" to create and manage custom fields.

This is the whole loop.php:

    <?php if ( ! have_posts() ) : ?>
<div id="post-0" class="post not-found post-listing">
    <h1 class="post-title"><?php _e( 'Not Found', 'tie' ); ?></h1>
    <div class="entry">
        <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'tie' ); ?></p>
        <?php get_search_form(); ?>
    </div>
  </div>

      <?php else : $count = 0;?>
      <div class="post-listing <?php if( tie_get_option( 'blog_display' ) == '2col' )  echo"archives_2col" ?>">
      <?php while ( have_posts() ) : the_post(); $count++; ?>
      <?php if( tie_get_option( 'blog_display' ) != 'full_thumb' ): ?>
<article class="item-list <?php echo'item_'.$count; ?>">
  <h2 class="post-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <p class="post-meta">

       <?php 
            if( get_field( "hmeromhnia_lhkshs" ) ): ?>
            <span class="date"><?php the_field('hmeromhnia_lhkshs'); ?>     </span>  
       <?php endif; 
     ?>

     <?php 
            if( get_field( "wra_lhkshs" ) ): ?>
            <span class="time"><?php the_field('wra_lhkshs'); ?></span>   
       <?php endif; 
     ?>

     <?php tie_get_time() ?>

    <span class="category-label">
    <?php
    $caturls = array();
    foreach( (get_the_category()) as $cat ):
    $caturls[] = '<a href="' . get_category_link( $cat->cat_ID  ) . '">' . $cat->cat_name . '</a> ';
    endforeach;
    echo implode( ', ', $caturls );        ?></span>

    <span class="post-comments"><?php comments_popup_link( __( '0', 'tie' ), __( '1 Comment', 'tie' ), __( '% Comments', 'tie' ) ); ?></span> 

    <?php if( date('U') - get_the_time('U', $post->ID) < 24*60*60 ) : ?>
    <span class="new-post"></span>
    <?php endif; ?>


    <?php $icon = get_post_meta( $post->ID, 'apaitei_logariasmo_facebook', true); ?>
    <?php  if( !empty($icon) ) : ?>
    <span class="facebook-badge"></span>
    <?php endif; ?>


    </p>


    <?php if( tie_get_option( 'blog_display' ) == 'content' ): ?>
    <div class="entry">
        <?php the_content( __( 'Read More &raquo;', 'tie' ) ); ?>
    </div>
    <?php else: ?>
    <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : ?>         
    <div class="post-thumbnail">
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
            <?php tie_thumb('',200,200); ?>
            <?php tie_get_score( true ); ?>
    </a>
    </div><!-- post-thumbnail /-->
        <?php endif; ?>
    <div class="entry">
        <p><?php tie_excerpt() ?></p>
        <a class="more-link" href="<?php the_permalink() ?>"><?php _e( 'Read More &raquo;', 'tie' ) ?></a>
    </div>
    <?php endif; ?>

    <?php tie_include( 'post-share' ); // Get Share Button template ?>
    <div class="clear"></div>
   </article><!-- .item-list -->
   <?php if( $count == 2 &&  tie_get_option( 'blog_display' ) == '2col' ): $count = 0; ?>
    <div class="sep"></div>
   <?php endif; ?>
       <?php else: ?>
   <article class="item-list">
    <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : ?>         
    <div class="post-thumbnail single-post-thumb archive-wide-thumb">
        <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php tie_thumb('', 620 , 330 ); ?><?php tie_get_score( true ); ?></a>
    </div>
    <?php endif; ?>
    <h2 class="post-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <p class="post-meta">
        <?php tie_get_time() ?>
        <span class="post-comments"><?php comments_popup_link( __( '0', 'tie' ), __( '1 Comment', 'tie' ), __( '% Comments', 'tie' ) ); ?></span>
        <?php echo tie_views(); ?>
    </p>
    <div class="entry">
        <p><?php tie_excerpt() ?></p>
        <a class="more-link" href="<?php the_permalink() ?>"><?php _e( 'Read More &raquo;', 'tie' ) ?></a>
    </div>
    <?php tie_include( 'post-share' ); // Get Share Button template ?>  
    <div class="clear"></div>
</article><!-- .item-list -->
    <?php endif; ?>

    <?php endwhile;?>
    </div>
    <?php endif; ?>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Okay there are a few things making this not work here.

First, you need to pass strings to functions like get_post_meta. So, for example, use

$icon = get_post_meta( $post->ID, 'apaitei_logariasmo_facebook', true);

Instead of

$icon = get_post_meta( $post->ID, apaitei_logariasmo_facebook, true);

Second, as you may have noticed above, the syntax for get_post_meta that you are using in incorrect. It is documented here in the WordPress Codex.

The same reasoning applies with strings here. They must be between the quotes in PHP.

if( $icon == esc_attr('Ναι') ) :

Note about esc_attr: I don't have experience working with the Greek alphabet in PHP, but if you say it's necessary, okay.

So, the solution is:

<?php
$icon = get_post_meta( $post->ID, 'apaitei_logariasmo_facebook', true);
if( $icon == esc_attr('Ναι') ) : ?>
<span class="facebook-badge"></span>
<?php endif; ?>
share|improve this answer
    
,using the corrections you gave me i succeeded displaying the icon. But now i can see that all the posts have the icon. I want only the posts that have the specific 'meta_value' -> 'Ναι'. Do you know how to do that? –  Charitos Jun 12 '13 at 23:30
    
Good, glad we're making progress! Please see my updated answer. –  XIROXAS Jun 12 '13 at 23:40
    
unfortunately doesn't work. Probably because i am using Greek characters. What if i use this: if( $icon == attr_esc('Ναι') ) : ? –  Charitos Jun 12 '13 at 23:44
    
No it didn't work.. –  Charitos Jun 12 '13 at 23:48
    
Okay. Using Google Translate tells me that basically means "yes", so I assume that you want this span to be displayed when it is yes, correct? –  XIROXAS Jun 12 '13 at 23:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.