【问题标题】:get value of image custom field at frontent of wordpress?在wordpress前端获取图像自定义字段的值?
【发布时间】:2013-10-09 09:53:56
【问题描述】:

请帮帮我。我已经战斗了三天了。

如何获取自定义字段的值以将其显示在帖子内容区域?

【问题讨论】:

  • 图片自定义字段?你在哪里存储文件信息和图像。 get_post_meta。可以帮助您带来自定义帖子元的价值
  • 感谢回复。我在function.php中添加了一些代码,通过它我可以在上传任何图像时看到我的自定义字段,它保留了我给它的那些值,但我想要的是将该自定义字段值提取到我的帖子中。尝试了很多事情,但没有结果。
  • 首先你没有回答我的问题。您是否尝试过“get_post_meta”。你能解释一下你尝试过的很多事情吗

标签: wordpress custom-fields


【解决方案1】:

您可以使用以下代码获取要显示的自定义字段:

get_post_meta($post_id, $key, $single);

【讨论】:

  • add_filter('attachment_fields_to_edit', 'edit_media_custom_field', 11, 2 ); add_filter('attachment_fields_to_save', 'save_media_custom_field', 11, 2); function edit_media_custom_field( $form_fields, $post ) { $form_fields['custom_field'] = array( 'label' => 'Product Code', 'input' => 'text', 'value' => get_post_meta( $post-> ID, '_custom_field', true ) );返回 $form_fields; } 函数 save_media_custom_field( $post, $attachment ) { update_post_meta( $post['ID'], '_custom_field', $attachment['custom_field'] );返回 $post;
  • 我想在 category.php 中获取它。我正在使用 echo get_post_meta($post->ID, '_custom_field', true);
  • $key:它将是您的自定义字段的名称
  • $single :如果设置为 true,则函数将返回单个结果,作为字符串。如果为 false 或未设置,则该函数返回自定义字段的数组。
  • 但这会显示自定义字段 w.r.t 以发布。我想要 w.r.t 图片。可以吗?
【解决方案2】:

可以使用以下函数在模板内部(循环内部)获取自定义字段数据:

获取所有字段

<?php the_meta(); ?>

OR 表示特定值

get_post_meta($post_id, $key, $single);

您可以在官方 wordpress 文档中了解更多关于 custom fields 的示例。

【讨论】:

    【解决方案3】:

    您可以通过以下代码使用它:

    <?php echo get_post_meta($post_id, $key, true); ?>
    

    这里您的参数必须与您在 function.php 中提到的相同,同时创建您的自定义字段,如下所示:

    <?php add_post_meta($post_id, $key, $single,true); ?>
    

    谢谢。

    【讨论】:

    • 但这会显示自定义字段w.r.t要发布。我想要 w.r.t 图片。可以吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多