为 WordPress 定制 the_content

发布于 2025-01-08 07:43:37 字数 1970 浏览 0 评论 0原文

对于我的网站,我创建了一个带有自定义字段自定义帖子类型。虽然我已经弄清楚如何将自定义字段显示到循环中,但我发现我无法利用 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);

除了上述例外之外,该代码实际上可以正常工作。有人对如何将条件添加到代码中或修复图像有任何建议吗?

For my website I have created a custom post type to with custom fields. While I have figured out how to display the custom fields into the loop, I found I could not take advantage wordpress's ability to use the_except, and consequently was my slider which runs on wordpress's the_except was empty. So I came up with the idea of inserting my custom fields directly into the_content via the function.php. This as has work well for the most part, but I'm having a few difficulties. I apologize, but I'm not very advanced in php. So if the answer is simple please forgive.

Okay, problem 1: I cannot get my image to show. I have tried working the code a few ways, and the best I can do is get a URL or a broken image icon to show.

Problem 2: I have a few titles I want show prior to my insertion of the meta fields, but I want their appearance conditional on the meta field have a positive value.

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

This code actually functional, with the aforementioned exceptions. Does anyone have any suggest as to how to add the conditionals into the code or fix the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2025-01-15 07:43:37

您不应该将其添加到 functions.php 中!

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

从您的示例来看,它是用于显示食谱的单个页面。所以复制 single.php;调用文件 single-recipes.php 并从那里开始工作。

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

You shouldn't be adding this in functions.php!

The content will be spat out in some kind of page, perhaps currently single.php and you don't want to edit single.php because it will have effect on all pages.

As it seems from your example, it is the single page for displaying recipes. So duplicate single.php; call the file single-recipes.php and work from there.

Have a look here: http://codex.wordpress.org/Template_Hierarchy to understand more about the file hierarchy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文