多个wordpress自定义后元值

时间:2010-12-22 11:05:02

标签: wordpress post field meta

我对PHP很无望,所以我想我会请求一些帮助!

我在daviesmusic.com使用WordPress作为音乐代理网站的CMS,并通过自定义字段为各种艺术家提供了大量额外信息。到目前为止,我只是逐个检索自定义字段值,如下所示:

<?php
    //audio title
    $audio_title = get_post_meta($post->ID, 'audio_name', true);
    if ($audio_title) :
?>

    <p><?php echo $audio_title; ?></p>

这很好用,只有我有很多自定义字段都需要不同的输出。我有一个艺术家的照片,一个音频文件,音频文件的标题和有关音频文件的一些注释,如下所示:

    <?php
    //profile image
    $profile_pic = get_post_meta($post->ID, 'profileimage', true);
    if($profile_pic) :
?>

    <img class="post-thumb-single" src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo $profile_pic; ?>&a=t&h=278&w=278&zc=1" alt="<?php the_title(); ?>" />

<?php   
    endif;
    //photographer
    $photographer = get_post_meta($post->ID, 'photographer', true);
    if($photographer) :
?>

    <p id="photographer">Photographer: <b><?php echo $photographer; ?></b></p>

<?php
    endif;
    //audio title
    $audio_title = get_post_meta($post->ID, 'audio_title', true);
    if ($audio_title) :
?>

    <h4 class="nsmb">Audio files</h4>

    <p><?php echo $audio_title; ?></p>

<?php
    //audio file
    $audio = get_post_meta($post->ID, 'audiofile', true);
    if ($audio) :
?>


<p class="audio" id="audioplayer_1">Sorry, there was a problem loading the file.</p>
<script>AudioPlayer.embed("audioplayer_1", {soundFile: "<?php echo $audio; ?>"});</script>


  <?php
    $audio_credits_1 = get_post_meta($post->ID, 'audio_credits_1', true);
    if ($audio_credits_1) :
  ?>

    <p class="audio_cred"><?php echo $audio_credits_1; ?></p>

    <?php
        endif; endif; endif;
    ?>

很多代码。我确信必须有一种更有效的方法来检索值!有什么想法吗?!

干杯

1 个答案:

答案 0 :(得分:3)

here获取此示例:

$custom_fields = get_post_custom(72);
$my_custom_field = $custom_fields['my_custom_field'];
foreach ( $my_custom_field as $key => $value ){
    echo $key . " => " . $value . "<br />";
}

这将循环显示帖子的所有指定自定义值(在此示例中为id#72,您可以使用代码中的$post->ID来获取)。您可以使用$key打印字段名称,使用$value打印该字段的值。您可以使用if elseif else语句或switch更详细地了解它们的显示方式。

相关问题