Modifying Image size for the home page – WordPress
I have done some modification for the current theme home page. I am showing the content till the Read More.. The problem what I was facing was the size of the images. I have written a function which resizes the image according to the image page design. We do not need to bother about the size of the image when we do the post.
First We need some function to find the height and width of the posted image and then resize the image according the size ratio so that images don’t look odd.
|
1 |
function getImageInfo($str, $strfindwhat) { |
|
1 |
$widthPos = strpos($str, $strfindwhat); |
|
1 |
<span style="color: #008000">//echo "Width Position : ".$widthPos;</span> |
|
1 |
$strAfterWidth = substr($str, $widthPos); |
|
1 |
<span style="color: #008000">//echo "After Width : ".$strAfterWidth;</span> |
|
1 |
$afterWidthSpace = strpos($strAfterWidth, <span style="color: #006080">" "</span> ); |
|
1 |
$withInfo = substr($strAfterWidth,0, $afterWidthSpace); |
|
1 |
<span style="color: #008000">//echo " Width Info : ".$withInfo;</span> |
|
1 |
$finalWidth= preg_replace(<span style="color: #006080">"#[^0-9]#"</span>, <span style="color: #006080">""</span>, $withInfo); |
|
1 |
<span style="color: #008000">//" ,"" , $withInfo); </span> |
|
1 |
<span style="color: #0000ff">return</span> $finalWidth; |
|
1 |
} |
Then we modify the content using the following function
|
1 |
function the_content_limit($max_char, $more_link_text = <span style="color: #006080">'(more...)'</span>, $stripteaser = 0, $more_file = <span style="color: #006080">''</span>) { |
|
1 |
$content = get_the_content(<span style="color: #006080">''</span>); |
|
1 |
$content = apply_filters(<span style="color: #006080">'the_content'</span>, $content); |
|
1 |
<span style="color: #008000">//$content = the_content_limit(100, "Read more");</span> |
|
1 |
$thisimageheight = getImageInfo($content, <span style="color: #006080">"height"</span>); |
|
1 |
$thisimagewidth = getImageInfo($content, <span style="color: #006080">"width"</span>); |
|
1 |
<span style="color: #0000ff">if</span> ($thisimagewidth>100) { |
|
1 |
$fwidth = 100; |
|
1 |
$adjuster = $thisimagewidth / $fwidth; |
|
1 |
$fheight = $thisimageheight / $adjuster; |
|
1 |
$thisimageheight = $fheight; |
|
1 |
$thisimagewidth = $fwidth; |
|
1 |
} |
|
1 |
$content = str_replace(<span style="color: #006080">']]>'</span>, <span style="color: #006080">']]&gt;'</span>, $content); |
|
1 |
$content = str_replace(<span style="color: #006080">'img'</span>, <span style="color: #006080">'img width='</span>.$thisimagewidth.<span style="color: #006080">'px height = '</span>.$thisimageheight.<span style="color: #006080">'px '</span>, $content); |
|
1 |
<span style="color: #008000">//$content = strip_tags($content, "<img><b><strong><i><em><a><li><div>");</span> |
|
1 |
echo $content; |
|
1 |
} |
Now we call this function to print the modified content
|
1 |
<div <span style="color: #0000ff">class</span>=<span style="color: #006080">"grid_5"</span>> |
|
1 |
<?php query_posts(<span style="color: #006080">'cat=12,-29,-44&showposts=5'</span>); ?> |
|
1 |
<?php <span style="color: #0000ff">if</span> (have_posts()) : <span style="color: #0000ff">while</span> (have_posts()) : the_post(); ?> |
|
1 |
<div <span style="color: #0000ff">class</span>=<span style="color: #006080">"moreinfo"</span>> |
|
1 |
<h3><?php the_title(); ?></h3> |
|
1 |
<p style=<span style="color: #006080">"font-size:9px; font-weight:200;"</span>><?php the_time(<span style="color: #006080">'F j, Y'</span>); ?> by <?php the_author_posts_link(); ?> &middot; <?php comments_popup_link(<span style="color: #006080">'Leave a Comment'</span>, <span style="color: #006080">'1 Comment'</span>, <span style="color: #006080">'% Comments'</span>); ?>&nbsp;<?php edit_post_link(<span style="color: #006080">'(Edit)'</span>, <span style="color: #006080">''</span>, <span style="color: #006080">''</span>); ?></p> |
|
1 |
<?php the_content_limit(150, <span style="color: #006080">""</span>); ?> |
|
1 |
<div style=<span style="color: #006080">"font-size:9px; font-weight:200;"</span>> |
|
1 |
Filed Under: <?php the_category(<span style="color: #006080">', '</span>) ?><br />Tagged: <?php the_tags(<span style="color: #006080">''</span>) ?> |
|
1 |
</div> |
|
1 |
<p <span style="color: #0000ff">class</span>=<span style="color: #006080">"more"</span>><a title=<span style="color: #006080">"Permanent Link to <?php the_title(); ?>"</span> href=<span style="color: #006080">"<?php the_permalink() ?>"</span> rel=<span style="color: #006080">"bookmark"</span>>Read More..</a></p> |
|
1 |
</div><!-- /moreinfo --> |
|
1 |
<?php endwhile; endif; ?> |
|
1 |
</div> |
I am using the VibrantCMS theme
