【问题标题】:Customizing the_content for wordpress为 wordpress 定制 the_content
【发布时间】:2012-03-14 17:09:19
【问题描述】:

我为我的网站创建了一个custom post typecustom 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


    【解决方案1】:

    你不应该在functions.php中添加这个!

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2012-11-28
      • 2019-07-06
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多