I'm working on a new Wordpress theme; the default index view displays the excerpts of recent posts. Some posts will be regarding file downloads, and include an image, description, and link to t开发者_StackOverflowhe location where the described files are hosted. The images for these types of posts will be anchored with links(other types of posts may contain images that are not linked).
For these types of posts, I would like the images to link to their entry's full post views(single.php) when displayed in excerpts, but for the same images to link to an external download link when displayed as part of the full post view.
I'm not sure how exactly I would accomplish that. Any help would be greatly appreciated!
Because I don't have post thumbnails defined, and since posts may or may not contain multiple images, I ended up doing it this way:
I turned off tags in excerpts(I had an excerpt plugin enabling tags in excerpt), then I added the following to the function.php file:
function catch_that_image() {
global $post, $posts;
$first_img = ''; ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
$post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img = "/images/default.jpg"; }
return $first_img;}
Then I added the following to the main index template file in between the posts title and the excerpt/content:
<p><a href="<?php the_permalink() ?>" alt="<?php _e('Read full article', 'theme');?>" title="<?php _e('Read full article', 'theme');?>"><img src="<?php echo catch_that_image() ?>"></a></p>
Random note: It's wrapped in a
for styling reasons.
Thanx again for guiding me in the right direction.
if your theme uses 'the_excerpt()' for the front page, I think you can add a filter in functions.php, and with a regexp change the link href from the download link to the permalink.
something like,
function replace_link($content) {
if (is_home())
return preg_replace('regular_expression', get_permalink(), $content);
else
return $content;
}
add_filter('the_excerpt', 'replace_link');
I can't create an actual regular expression without knowing what your download link looks like
精彩评论