自定义wordpress的the_content

时间:2012-02-24 08:25:56

标签: php image wordpress function conditional-statements

对于我的网站,我使用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);

此代码实际上具有功能,但有上述例外情况。有没有人建议如何将条件添加到代码中或修复图像?

1 个答案:

答案 0 :(得分:0)

您不应该在functions.php中添加此内容!

内容将在某种页面中吐出,可能是当前single.php,您不想编辑single.php,因为它会对所有页面产生影响。

从您的示例中可以看出,显示single的是recipes页面。所以复制single.php;调用文件single-recipes.php并从那里开始工作。

在这里查看:http://codex.wordpress.org/Template_Hierarchy以了解有关文件层次结构的更多信息。

相关问题