Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Adding Permalink To Image In RSS Feed - Includes partial code. WordPress

  • SOLVED

Hi,

I'm using this functions.php snippet to pull in a post image into the feed.

add_filter('the_excerpt_rss','insert_custom_image');
function insert_custom_image($output) {

// I'm assuming that this function gives you back all you need

$image = cp_get_image_url();


// Do more manipulation if not, eg sizing...

// Very simplistic - you could preg_replace if you want it somewhere else

$output = $image . $output;

return $output;

}


But the image links to the uploads folder, I need to have the image include the permalink so it links to the content url.

Your help is much appreciated.

Thanks!

Answers (2)

2011-03-01

John Cotton answers:

Edwin

You need to show us the contents of this function:

cp_get_image_url()

It's that that's giving you the wrong URL...

JC


Edwin comments:

Hi John,

Here is some documentation that is part of the original theme:


<blockquote>/*******************************************************************************************/
/* We are beginning to allow some of our functions to use overrides from the child themes. */
/* To make it easier for you to create your functions.php file, we will list the functions */
/* here as we allow you to override them. */
/*******************************************************************************************/

/* includes/theme-functions.php functions that can be child theme modded */
// display the login message in the header
function cp_login_head()

// get the date/time ad was posted
function appthemes_date_posted($m_time)

// display all the custom fields on the single ad page
function cp_get_ad_details($postid, $catid)

// get the first medium image associated to the ad
// used on the home page, search, category, etc
<strong>function cp_get_image</strong>($post_id = '', $size = 'medium', $num = 1)

// get the image associated to the ad used on the single page
function cp_get_image_url($post_id = '', $size = 'medium', $class = '', $num = 1)

// get the image associated to the ad used on the home page
function cp_get_image_url_feat($post_id = '', $size = 'medium', $class = '', $num = 1)

// get all the small images for the ad and lightbox href
// important and used on the single page
function cp_get_image_url_single($post_id = '', $size = 'medium', $title = '', $num = 1)

// used for getting the single ad image thumbnails
function cp_get_image_thumbs($postID, $height, $width, $lheight, $lwidth, $num=-1, $order='ASC', $orderby='menu_order', $mime='image')

// get the ad price and position the currency symbol
function cp_get_price($postid)

// get all the images associated to the ad and display the
// thumbnail with checkboxes for deleting them
// used on the ad edit page
function cp_get_ad_images($ad_id)

// show category with price dropdown
function cp_dropdown_categories_prices( $args = '' )

// category menu drop-down display
function cp_cat_menu_drop_down($cols = 3, $subs = 0)

// directory home page category display
function cp_directory_cat_columns($cols)


/* includes/forms/step-functions.php functions that can be child theme modded */

// loops through the custom fields and builds the custom ad form
function cp_formbuilder($results)

// queries the db for the custom ad form based on the cat id
function cp_show_form($catid)

// if no custom forms exist, just call the default form fields
function cp_show_default_form()

// determine what the ad post status should be
function cp_set_post_status($advals)


/* includes/theme-login.php functions that can be child theme modded */

// show the custom login page if on wp-login.php
function cp_show_login()

// show the new user registration page
function cp_show_register()


/* includes/theme-profile.php functions that can be child theme modded */

function cp_profile_fields($user)

function cp_profile_fields_save($user_id)</blockquote>


This is a snippet that references the cp_get_image_url() part;

// get the main image associated to the ad used on the single page
if (!function_exists('cp_get_image_url')) {
function cp_get_image_url() {
global $post, $wpdb;

// go see if any images are associated with the ad
$images = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'ID') );

if ($images) {

// move over bacon
$image = array_shift($images);

// see if this v3.0.5+ image size exists
//$adthumbarray = wp_get_attachment_image($image->ID, 'medium');
$adthumbarray = wp_get_attachment_image_src($image->ID, 'medium');
$img_medium_url_raw = $adthumbarray[0];

// grab the large image for onhover preview
$adlargearray = wp_get_attachment_image_src($image->ID, 'large');
$img_large_url_raw = $adlargearray[0];

// must be a v3.0.5+ created ad
if($adthumbarray)
echo '<a href="'. $img_large_url_raw .'" class="img-main" rel="colorbox"><img src="'. $img_medium_url_raw .'" title="'. the_title_attribute('echo=0') .'" alt="'. the_title_attribute('echo=0') .'" /></a>';


// wow, must be really old v2.9.3 or earlier
} elseif(get_post_meta($post->ID, 'images', true)) {

cp_single_image_legacy($post->ID, get_option('medium_size_w'), get_option('medium_size_h'));

// no image so return the placeholder thumbnail
} else {
echo '<img class="attachment-medium" alt="" title="" src="'. get_bloginfo('template_url') .'/images/no-thumb.jpg" />';
}

}
}


Let me know if this helps?


John Cotton comments:

Which image do you want? The featured image? Or just the first attached image?

Also, why isn't the image in the upload directory?


Edwin comments:

The featured image, the images in the RSS feed are the same as used on the home page thumbnails. So the featured image would be great.

<blockquote>Also, why isn't the image in the upload directory?</blockquote>

The current RSS feed images reference the uploads directory, I haven't made any alterations to the theme for image upload destinations, the above code is original theme code.


John Cotton comments:

<blockquote>The featured image
</blockquote>

In which case, this will do it:

add_filter('the_excerpt_rss','insert_custom_image');
function insert_custom_image($output) {
global $post;

$image = get_the_post_thumbnail($post->ID, 'thumbnail');

// Very simplistic - you could preg_replace if you want it somewhere else
$output = $image . $output;

return $output;
}

2011-03-01

rilwis answers:

Here's the code with image linked to post content:

add_filter('the_excerpt_rss','insert_custom_image');
function insert_custom_image($output) {
global $post;
$image = get_the_post_thumbnail($post->ID, 'medium');

$output = '<a href="' . get_permalink($post->ID) . '">' . $image . '</a>' . $output;
return $output;
}


You can change 'medium' to 'thumbnail' or 'large', or 'full' to change the image size.