• Hi guys, a little help please. I have a blog which will consist mostly of single-photo articles. On my front page I have 10 articles, each of them with one full-size picture, but I cant seem to make the photos link to their “article”, only to themselves.

    For instance, I add a new article, write its title, click insert new image. The only option i get for image`s url are “none” “file” and “attachment”, as in the pic bellow, but no “post url”. Why?

    https://img88.imageshack.us/img88/4630/plmf.jpg

    As i said, by clicking on an image on the front page would like to bring the reader to that image’s own article, where they can comment and all that. Maybe there is a way to let the images have no urls but somehow force the front page full-size images to link to their respective post permalinks, the same way thumbnails and feature images work?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    There is no easy way to do this as your photo’s are already inserted into the posts. Have you thought about using post thumbnails

    Moderator keesiemeijer

    (@keesiemeijer)

    Or add a filter to the post content that alters the link address. Put this in your theme’s functions.php

    add_filter('the_content', 'link_images_to_post');
    
    function link_images_to_post($text){
    
    	$upload_dir = wp_upload_dir();
    	$upload_url = preg_quote($upload_dir['baseurl'], '/');
    
    	global $post;
    	if(empty($post->ID))
    		return $text;
    
    	// here starts the regex craziness!!
    	if(preg_match('/<a(.*?)href="'.$upload_url.'(.*?)"(.*?)><img(.*?)src="'.$upload_url.'(.*?)"(.*?)><\/a>/', $text)){
    		//there is a linked image attachement in the post_content
    
    		$patterns = array(
    		  '/<a(.*?)href="'.$upload_url.'(.*?)"(.*?)><img(.*?)src="'.$upload_url.'(.*?)"(.*?)><\/a>/'
    		);
    
    		$replacements = array(
    		  '<a\1href="' . get_permalink($post->ID) . '"\3><img\4src="'.$upload_dir['baseurl'].'\5"\6></a>'
    		);
    
    		$text = preg_replace($patterns,$replacements,$text);
    	}
    
    	return $text;
    }

    Images will be linked to the post on the front end of your site. Only images that are linked to the file url will be altered to link to the post.

    Thread Starter Glisse

    (@glisse)

    thanks for the reply..did not manage to make it work so far (string errors, brakes the site when c/p the above code) but it is a start…

    I also found the following code

    /**
     * Add post permalink to image post format
     */
    function tab13_image_permalink($content){
    
    	$format = get_post_format();
    	$searchfor = '/(<img[^>]*\/>)/';
    	$replacewith = '<a href="'.get_permalink().'">$1</a>';
    
    	if (is_single() === FALSE AND $format == 'image'){
    		$content = preg_replace($searchfor, $replacewith, $content, 1);
    	}
    	return $content;
    }
    add_filter('the_content', 'tab13_image_permalink');

    this function is said to make the images on homepage to link to the post url, but so far also it is not working for me.. not even for the default theme.. well, in the end it will work, somehow, although i am not a php literate ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Link front-page full-size images to their post url’ is closed to new replies.