WordPress.org

Ready to get started?Download WordPress

Forums

Adding Custom Field... (2 posts)

  1. CSSgirl
    Member
    Posted 6 years ago #

    I want to add a custom field value to a plugin I am using. The plugin is the Daily Top Ten Posts.

    So, I found the part of the code I need to edit to control the output of the list of entry titles. What I want to do is use a custom field that I have to display the image.

    So, this is the current code:

    if ($widgets_num) {
    	foreach ($widgets as $widget) {
    	$postnum = $widget->postnum;
    	$postcount = $widget->postcount;
      $id_post = $widget->ID;
      $title_post = $widget->post_title;
      if ($perma) {
      $home_url_perma = get_permalink($id_post);
      <strong>$postpic = get_post_custom_values("PostImage");
    </strong>  $todays_widget_temp .= "<strong><p class=\"news-img\"><img src=\"$postpic\" alt=\"$title_post\">
    <div class=\"news-txt\"><a href=\"$home_url/?p=$id_post\" title=\"$text\">$title_post</a> <span class=\"count\">($postcount$views)</span></div></strong>\n";
      } else {
      $todays_widget_temp .= "<strong><p class=\"news-img\"><img src=\"$postpic\" alt=\"$title_post\">
    <div class=\"news-txt\"><a href=\"$home_url/?p=$id_post\" title=\"$text\">$title_post</a> <span class=\"count\">($postcount$views)</span></div></strong>\n";
    	  }
    	 }
      }

    *Note: I've added this variable to try to get my custom field to display: $postpic = get_post_custom_values("PostImage");
    *

    Now, everything works fine, EXCEPT, I can't get the $postpic to display. Which is a custom variable I added myself to call the "PostImage" custom field.

    The only they that displays in the HTML output for the $postpic value is "Array". Not any actual values.

    I've also tried using

    get_post_meta($post->ID, "PostImage", true);

    in place of

    get_post_custom_values("PostImage")

    As you can see I'm not php expert, so I'm struggling trying to pull the "PostImage" custom value and placing it within this plugin.

    If anyone could give me some advice, or show me how to do it I would greatly, greatly appreciate it.

    Thanks,
    Lindsey

  2. cwoodside
    Member
    Posted 6 years ago #

    Hi lalindsey,
    I'm assuming that you are looking to show only one selected image with each post. If I'm correct you may want to try looking at this anther way, here is what I am thinking:

    1. Create a custom field called "wp_uploadname", this field will be used to enter the name of the image you would like to appear (including the extension).

    2. Update your theme so that you use code similar to this for each post:

    <img src="<?php bloginfo('wpurl'); ?>/wp-content/uploads/<?php the_time('Y/m/'); ?><?php $key ="wp_uploadname"; echo get_post_meta($post->ID, $key, true); ?>" alt="" width="" height="" />

    "wp_uploadname" represents the name of your custom field.

    3. Create a new post, upload an image in the standard way (without actually attaching it to the post body) then type the name of that image (including extension) in the custom field you had created.

    4. After doing this the code from step two will resolve to something like this:

    `<img src="http://yourwebsite.com/wp-contents/uploads/2007/10/myimage.jpg" alt="" height="" width="" />'

    I entered "2007/10" for example purpose the dates would actually change to reflect when each post was created. Also "myimage.jpg" would actually be the image name that you had entered in the custom field, which of course will have to actually match the uploaded image name and file extension.

    Hope this helps,
    Chet

Topic Closed

This topic has been closed to new replies.

About this Topic