是否显示带有GET_POST_META的图标?

时间:2013-06-12 作者:Charitos

我想在循环中的post元数据中显示一个图标。php,对于每个帖子都有一个特定的meta\\u值!

更具体地说,我使用此代码在发布新帖子时显示一个图标,该图标持续24小时:

<?php if( date(\'U\') - get_the_time(\'U\', $post->ID) < 24*60*60 ) : ?>
<span class="new-post"></span>
<?php endif; ?>
下面是风格代码。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;
}
我真正想要的是,当一篇文章有一个特定的meta\\u值时,显示一个图标。以下是我试图获取的代码,但没有结果:

<?php $icon = get_post_meta(apaitei_logariasmo_facebook); ?>
<?php if( ! $icon == attr_esc(Ναι) ) ?>
<span class="facebook-badge"></span>
<?php endif; ?>
这是我在风格上使用的代码。css插入图标:

.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;
}
要使代码正常工作并显示特定meta\\u值的图标,我必须做些什么?

P、 我在用这个attr_esc(Ναι) 因为我在meta\\u值中使用希腊字符。此外,我正在使用“高级自定义字段插件”创建和管理自定义字段。

这就是整个循环。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; ?>

1 个回复
最合适的回答,由SO网友:a web dev 整理而成

您在中使用的get\\u post\\u meta的语法不正确。是的documented here.

您需要为第二个参数传递一个字符串。因此,例如,使用

$icon = get_post_meta( $post->ID, \'apaitei_logariasmo_facebook\', true );
而不是

$icon = get_post_meta( $post->ID, apaitei_logariasmo_facebook, true );
此外,字符串文字must 在PHP中介于引号之间。

if( $icon == esc_attr( \'Ναι\' ) ) :
因此,解决方案是:

<?php
$icon = get_post_meta( $post->ID, \'apaitei_logariasmo_facebook\', true );
if( $icon == esc_attr( \'Ναι\' ) ) : ?>
<span class="facebook-badge"></span>
<?php endif; ?>

结束

相关推荐

Featured Images not appearing

我帮助一位朋友写WordPress博客,因为我是一个“电脑爱好者”,虽然我没有WordPress的具体经验,多年来也没有使用过PHP。所以,我真的很感激任何人都能对这个问题有所启发。该博客的主要目的是对项目进行分类,大约有8000篇帖子,每篇都包含一张照片。这个博客开始于另一个平台,几年前被迁移到WordPress,所以后端的事情相当混乱。数千条帖子从其他平台迁移,然后又有数千条帖子从WP CMS发布。他们最近升级到了WordPress的最新版本(还有一个新主题Themolio),现在有两个主要问题(第一