【发布时间】:2012-03-14 17:09:19
【问题描述】:
我为我的网站创建了一个custom post type 和custom fields。虽然我已经弄清楚如何将自定义字段显示到循环中,但我发现我无法利用 wordpress 使用 the_except 的能力,因此我在 wordpress 的 the_except 上运行的滑块是空的。所以我想出了通过function.php将我的自定义字段直接插入the_content的想法。这在大多数情况下都运行良好,但我遇到了一些困难。我很抱歉,但我在 php 方面不是很先进。所以如果答案很简单,请见谅。
好的,问题 1:我无法显示我的图像。我已经尝试了几种方法来处理代码,我能做的最好的就是获取一个 URL 或一个损坏的图像图标来显示。
问题 2:我想在插入元字段之前显示一些标题,但我希望它们以元字段为条件的外观具有正值。
function custom_post_type_default_content($content) {
global $post; global $wp_query;
if ($post->post_type == 'recipes') {
$content .=
'<p> <div id="recipe_name"><h1>'.
get_post_meta($post->ID, "recipe_name", true).'
</h1><p></div>
<br />
<p> <div id="recipe_image">
'.get_post_meta($post->ID, 'recipe_image', true ).'
<p>
</p></div>
<br />
<p><div id="serves"><b> Serves: </b>'.
get_post_meta( $post->ID, "serves", true ).'
</p></div>
<br />
<p><div id="prep_time"><b> Preparation Time: </b>'.
get_post_meta( $post->ID, "prep_time", true ).'
</p></div>
<br />
<p><div id="wpcf-ingredients"><b> Ingredients: </b><br />'.
get_post_meta( $post->ID, "wpcf-ingredients", true ).'
</p></div>
<br />
<p><div id="wpcf-cooking_instructions"><b> Cooking Instructions: </b><br />'.
get_post_meta( $post->ID, "wpcf-cooking_instructions", true ).'
</p></div>
<br />
<p><div id="additional_information"><b> Additional Information: </b><br />'.
get_post_meta( $post->ID, "additional_information", true ).'
</p></div>';}
return $content;
}
add_filter('the_content', 'custom_post_type_default_content', 1);
此代码实际上可以正常工作,但上述例外情况除外。有人对如何在代码中添加条件或修复图像有任何建议吗?
【问题讨论】:
标签: php image wordpress function conditional-statements