Forums

[resolved] How to add image to a page using custom field (8 posts)

  1. beradmin
    Member
    Posted 2 years ago #

    Hi guys, I have searched and googled but I couldn't figure out how to make an image custom field works in my page. I have added different codes and it doesn’t work or it adds the URL if it was a text instead of adding the image. I want to add images to my page using Custom Fields. Not a loop, just a simple single image. I am new to WordPress and not good at all in PHP. I am just taking over a website that is published already.

    Here it is my page template (I have 2 custom fields set up already: rss and extra):

    <?php get_header(); ?>

    <div id="content">
    <?
    $toplevelid = $post->ID;
    if ($post->post_parent)
    {
    $toplevelid = $post->post_parent;
    $toplevel = get_page($toplevelid);
    if($toplevel->post_parent)
    {
    $toplevelid = $toplevel->post_parent;
    }
    }
    ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <h1 class="page-title"><span class="page-title-span"><?php the_title(); ?></span><span class="title"><?php echo get_the_title($toplevelid); ?></span></h1>
    <div class="entry">

    <?php $key="extra"; echo get_post_meta($post->ID, $key, true); ?>

    <?php

    $key="rss";
    $feedURL = get_post_meta($post->ID, $key, true);

    if ($feedURL) {
    $feed = new SimplePie($feedURL);

    $item = $feed->get_item();
    print $item->get_content();
    } ?>

    <?php the_content('<p class="serif">Read the rest of this page »</p>'); ?>

    <?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

    </div><!-- close .entry -->
    <?php endwhile; endif; ?>
    <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    </div><!-- close .post -->
    </div>
    </div><!-- close #content -->

    <?php get_footer(); ?>

  2. alchymyth
    The Sweeper
    Posted 2 years ago #

    is your image url in the 'extra' custom field?

    and do you want to show the image where this line

    <?php $key="extra"; echo get_post_meta($post->ID, $key, true); ?>

    is?

    if so, try to change the line to:

    <img class="custom_image" src="<?php $key="extra"; echo get_post_meta($post->ID, $key, true); ?>" alt="" />

    you can then style the image in your style.css using

    .custom_image {
    border: 1px solid #999;
    margin: 5px 5px 5px 5px;
     and whatever style you want; }
  3. beradmin
    Member
    Posted 2 years ago #

    That is great! Thanks so much. I was getting really frustrated.

    Only one more thing. There is a little box showing up in all pages because of the custom_image class. What 'if' code should I add so the page displays the image only if I add that custom field? Thanks :-)

  4. alchymyth
    The Sweeper
    Posted 2 years ago #

    should work with this check to see if the custom field is used:
    (replaces the line suggested earlier)

    <?php $key="extra";
    if ( get_post_meta($post->ID, $key, true) ) { ?>
    <img class="custom_image" src="<?php echo get_post_meta($post->ID, $key, true); ?>" alt="" /><?php }; ?>
  5. beradmin
    Member
    Posted 2 years ago #

    You are great! THANKS!!!!!

  6. evanomics
    Member
    Posted 2 years ago #

    how would i do that if i just wanted to show text. imagine extra is just text. is it possible to put that in my sidebar?

  7. alchymyth
    The Sweeper
    Posted 2 years ago #

    if it is pure text without paragraph tags surroundung it:

    <?php $key="extra";
    if ( get_post_meta($post->ID, $key, true) ) { ?>
    <p><?php echo get_post_meta($post->ID, $key, true); ?><p>
    <?php }; ?>

    if it is text with paragraph tags and/or other formatting:

    <?php $key="extra";
    if ( get_post_meta($post->ID, $key, true) ) { ?>
    <div class="extra"><?php echo get_post_meta($post->ID, $key, true); ?></div>
    <?php }; ?>

    if you want to put it into the sidebar, you need to be sure where the $post->ID gets its value from.

  8. evanomics
    Member
    Posted 2 years ago #

    cool, I got it working in the page itself, but still couldn't get it working in the sidebar. How do I make sure where $post->ID gets it value from? I feel like its something simple I'm not getting...

Topic Closed

This topic has been closed to new replies.

About this Topic