WordPress 自定义字段
我想使用 WordPress 的自定义字段为我的网站提供不同的标题横幅,这是我的代码:
<?php
get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="BodyWrap">
<!--MAIN CONT-->
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="rotateBanner"><?php
// check for image
$image = get_post_meta($post->ID, 'image', $single = true);?>
<img class="mainImg" src="<?php bloginfo(template_url); echo $image; ?>" alt=""/>
</div>
<div id="mainCopy">
<div id="content">
<h2><?php single_post_title(); ?></h2>
<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
代码呈现,但由于某种原因它仅将 img 路径呈现为:
<img alt="" src="http://www.testground.idghosting.com/philcom/wp-content/themes/phil"/>
现在 演示网站
testground.idghosting.com/philcom/" rel="nofollow noreferrer">这里是我放置在自定义字段中的 :图片 对于我输入的值:/images/sampleHead.png
I wanted to use the custom field for wordpress to have a different header banner for my site here is my code:
<?php
get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="BodyWrap">
<!--MAIN CONT-->
<div id="mainCont">
<?php get_sidebar(); ?>
<div id="rotateBanner"><?php
// check for image
$image = get_post_meta($post->ID, 'image', $single = true);?>
<img class="mainImg" src="<?php bloginfo(template_url); echo $image; ?>" alt=""/>
</div>
<div id="mainCopy">
<div id="content">
<h2><?php single_post_title(); ?></h2>
<?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
Now the code renders but for some reason it only renders the img path as:
<img alt="" src="http://www.testground.idghosting.com/philcom/wp-content/themes/phil"/>
here is the demosite
in the custom field I put this: image
For the value I put this: /images/sampleHead.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 php 代码尝试过,但有一个插件做得很好,尝试一下 http ://wordpress.org/extend/plugins/custom-field-images
tried it using a php code but there is a plugin that does it well it's workth a try http://wordpress.org/extend/plugins/custom-field-images
我认为你只是缺少一个“/” - 如果你看下面的代码(wordpress 中制作图像的正常方式):
因此你应该有
I think that you are just missing a "/" - If you look at the code below (a normal way in wordpress to do an image):
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo.png" alt="logo" /></div>
Therefore you should have
<img class="mainImg" src="<?php bloginfo(template_url); ?>/<?php echo $image; ?>
or something similar to this case since I haven't been haven't been able to test it.
您必须确保在添加帖子页面中创建了正确的
自定义字段...如果您将自定义字段称为“图像”,并且使用“图像”,则图像中的大写字母将通过此关闭...
并且在自定义字段内,我将粘贴完整的 URL,直接指向图像..
这样可以减少编码的麻烦,并且更容易链接到..
You have to make sure inside your add post page, you are creating the correct
custom field... if you called your custom field "Image", and your using "image", the Capital in Image would through this off..
and inside the custom field i would a stick in the full URL, direct to the image..
this way its less hassle to code, and easier to link to..
您的图像链接应该是这样的,正如 bvandrun 指出的那样,
我强烈建议使用 MagicFields wordpress 插件来自定义字段。一旦你设置了一个页面。
Your image link should be something like this as bvandrun points out
I would highly recommend using the MagicFields wordpress plugin for custom fields. Once you have it set up with a page.