WordPress 自定义字段

发布于 2024-08-26 22:14:23 字数 1421 浏览 6 评论 0原文

我想使用 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 &raquo;</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 技术交流群。

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

发布评论

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

评论(4

嘦怹 2024-09-02 22:14:23

使用 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

等风来 2024-09-02 22:14:23

我认为你只是缺少一个“/” - 如果你看下面的代码(wordpress 中制作图像的正常方式):logo

因此你应该有

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.

脱离于你 2024-09-02 22:14:23

您必须确保在添加帖子页面中创建了正确的
自定义字段...如果您将自定义字段称为“图像”,并且使用“图像”,则图像中的大写字母将通过此关闭...

并且在自定义字段内,我将粘贴完整的 URL,直接指向图像..
这样可以减少编码的麻烦,并且更容易链接到..

    <div id="rotateBanner">
      <?php $image = get_post_meta($post->ID, 'image', $single = true);?>
      <img class="mainImg" src="<?php echo $image; ?>" alt=""/> 
    </div>

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..

    <div id="rotateBanner">
      <?php $image = get_post_meta($post->ID, 'image', $single = true);?>
      <img class="mainImg" src="<?php echo $image; ?>" alt=""/> 
    </div>
無處可尋 2024-09-02 22:14:23

您的图像链接应该是这样的,正如 bvandrun 指出的那样,

<img class="mainImg" src="<?php bloginfo(template_url); ?>/<?php echo $image; ?>

我强烈建议使用 MagicFields wordpress 插件来自定义字段。一旦你设置了一个页面。

Your image link should be something like this as bvandrun points out

<img class="mainImg" src="<?php bloginfo(template_url); ?>/<?php echo $image; ?>

I would highly recommend using the MagicFields wordpress plugin for custom fields. Once you have it set up with a page.

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