我在PHP方面非常绝望,所以我想我会寻求帮助!

我正在使用WordPress作为音乐代理网站的CMS daviesmusic.com 并通过定制领域为各种艺术家提供许多额外的信息。到目前为止,我刚刚检索了一个自定义字段值,例如:

<?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;
    ?>

很多代码。我敢肯定,必须有一种更有效的方法来检索值!有任何想法吗?!

干杯

有帮助吗?

解决方案

以这个例子为例 这里:

$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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top