Jump to content

need to apply an if/else statement to Tim Thumb script


b

Recommended Posts

Not sure how to work this.  I essentially want to call a variety of image sizes based on which stylesheet a user chooses from the administration backend of a wordpress theme.  The sizes of the images would be different depending on which stylesheet is selected.  My idea is to write an if/else statement to accomplish this, but don't know how. 

 

The script looks like this:

 

<?php // This will show the image and link the image to the post. Alter the width and height (in both places) to your needs. ?>
		<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
		<div class="postthumb">
		<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
		<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=180&w=200&zc=1" alt="<?php the_title(); ?>" width="200" height="180" /></a>
		</div>
		<?php } ?>

 

If the user selects a layout utilizing a 200px wide image this script would work, but if they choose a layout using a 300px width image the Tim Thumb script would have to be obviously different. 

 

Basically, I would like to offer a different Tim Thumb script based on whatever layout/stylesheet the user selects.  And I think the best way is to offer an if/else statement, but I don't know how to do this...

 

Any help would be greatly appreciated...

 

Thanks!

Link to comment
Share on other sites

if I am correct, you appear to be sending the size variable for your images to timthumb.php

h=180&w=200

if u change it to

h=$newHeight&w=$=$newWidth

and delete  width="200" height="180" as I believe the script does the image sizing for you

you can then define $newWidth and $newHeight then based on the style sheet selected and no need to change timthumb.php at all

Link to comment
Share on other sites

you could append them directly to the link that changes the stylesheet itself, if you use a form make them hidden variables, if you use a href link put them on the end

 

depends how you r changing the stylesheet, then its just a matter of declaring the sizes you want for each style sheet type

Link to comment
Share on other sites

Thanks...I'm calling the css stylesheet in the header like this:

 

<?php if ($mp_960 == "true") {?>
	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/960.css" type="text/css" media="screen,projection" />
	<?php }?>

 

This is if the user selects the 960px width option.  Not sure where to put that variable, however

 

Sorry for the noob questions

Link to comment
Share on other sites

I am assuming you have an if statement for each of those style sheets, just add the 2 variables there in each section for the coresponding size so if mp_960 is supposed to have image 200w and 180h would be

<?php if ($mp_960 == "true") { 
     $newHeight="180";
     $newWidth="200"; ?>
      <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/960.css" type="text/css" media="screen,projection" />
      <?php }?>

 

so when that if statement is true so to are the settings for your timthumb script

Link to comment
Share on other sites

Seems like that should work, but it doesn't. 

 

Here's what I have in the header:

 

<?php if ($mp_960 == "true") { 
     			  $newHeight="180";
     			  $newWidth="470"; ?>
      <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/960.css" type="text/css" media="screen,projection" />
      <?php }?>

 

And here is what I have in the index.php

 

<!-- Start Tim Thumb -->

	<?php // This will show the image and link the image to the post. Alter the width and height (in both places) to your needs. ?>
	<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
	<div class="postthumb">
	<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
	<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=$newHeight&w=$=$newWidth&zc=1" alt="<?php the_title(); ?>" /></a>
	</div>
	<?php } ?>

	<!-- End Tim Thumb -->

 

And here is what I believe is the relevant code in Tim Thumb

 

// get properties
$new_width 		= preg_replace("/[^0-9]+/", "", get_request("w", 0));
$new_height 	= preg_replace("/[^0-9]+/", "", get_request("h", 0));
$zoom_crop 		= preg_replace("/[^0-9]+/", "", get_request("zc", 1));
$quality 		= preg_replace("/[^0-9]+/", "", get_request("q", 80));
$filters		= get_request("f", "");

if ($new_width == 0 && $new_height == 0) {
$new_width = 200;
$new_height = 180;
}

 

Confused why that didn't work...Seems like you were right, but something must be missing.

 

Thanks again

Link to comment
Share on other sites

The variable need to be in the php for them to change from the variable name to the variable, also you have a typo.

 

you have

<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&h=$newHeight&w=$=$newWidth&zc=1" alt="<?php the_title(); ?>" />

 

it should be like this

<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&<?php echo "h=".$newHeight."&w=".$newWidth; ?>&zc=1" alt="<?php the_title(); ?>" />

 

See how that goes

Link to comment
Share on other sites

well...I'm clearly a nuisance at this point.  but it doesn't seem to work.  however, the tim thumb script does seem to be resizing the image to be 200px wide and 180px height.  how it's doing this is, to me, unclear.  here is the testing url I'm using to figure this out:

 

http://www.minimlpress.com/

 

the idea is to allow the user the option to choose a different style sheet for their site.  if they choose the 960px width style sheet I wanted the images to resize to 470px, thus showing two columns. 

 

I'm calling the tim thumb script like this:

 

<!-- Start Tim Thumb -->
	<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
	<div class="postthumb">
	<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
	<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&<?php echo "h=".$newHeight."&w=".$newWidth; ?>&zc=1" alt="<?php the_title(); ?>" /></a></div>
	<?php } ?>
	<!-- End Tim Thumb -->

 

And in the header it looks like this:

 

<?php if ($mp_960 == "true") { 
     			  $newHeight="180";
     			  $newWidth="470"; ?>
      <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/960.css" type="text/css" media="screen,projection" />
      <?php }?>

 

It is calling properly the style sheet, but the image is not resizing to 470px.  Not sure why.

 

If I'm being annoying, I apologize. 

 

thanks again

Link to comment
Share on other sites

<img src="http://www.minimlpress.com/wp-content/themes/_minpress/scripts/timthumb.php?src=http://www.minimlpress.com/i/jasmine.jpg&h=&w=&zc=1" alt="jasmine" />

 

is what it is printing in the browser on loading the page which has no variable set for h & w so the timthumb script sets it to 200 & 180

if ($new_width == 0 && $new_height == 0) {
   $new_width = 200;
   $new_height = 180;
}

 

I cant see where your changing the style sheet for this page but looking at that url

 

echo the $newWidth & $newHeight variable tpwards the top of your page that changes the style sheet and see if the variables are getting set

Link to comment
Share on other sites

I echoed:

 

<?php echo $newHeight ?>

 

<?php echo $newWidth ?>

 

and got: 

 

180 & 470

 

does it matter that the variables in the tim thumb script are :  $new_width and $new_height and not $newWidth and $newHeight? 

 

I tried it but it didn't make a difference...just wondering

 

 

Link to comment
Share on other sites

putting this in your url works

but the code your page is generating is this with no values for w and h

 

so the problem is happening somehow sending the variables to your image links

 

the $new_width variable is independent of $newWidth variable and does not affect each other directly

 

 

Link to comment
Share on other sites

try changing

$new_width 		= preg_replace("/[^0-9]+/", "", get_request("w", 0));$new_height 	= preg_replace("/[^0-9]+/", "", get_request("h", 0));

 

to

$new_width 		= preg_replace("/[^0-9]+/", "", get_request("w", ""));$new_height 	= preg_replace("/[^0-9]+/", "", get_request("h", ""));

Link to comment
Share on other sites

Sure. 

 

Here's the index.php

 

<?php get_header(); ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div class="gallery">

	<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

	<!-- the title -->
	<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

	<!-- the date -->
	<p><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></p>


	<!-- Start Tim Thumb -->
	<?php if ( get_post_meta($post->ID, 'thumb', true) ) { ?>
	<div class="postthumb">
	<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
	<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "thumb", $single = true); ?>&<?php echo "h=".$newHeight."&w=".$newWidth; ?>&zc=1" alt="<?php the_title(); ?>" /></a></div>
	<?php } ?>
	<!-- End Tim Thumb -->

	<!-- the Content -->
	<?php the_content('Read the rest of this entry »'); ?>

	<!-- the Tags & Comments -->
	<p><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>
	</div>

</div>

<?php endwhile; ?>

<!-- else -->
<?php else : ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
<!-- end else -->


<?php get_footer(); ?>

 

Here's the head section:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

<!-- Admin Options -->
<?
global $options;
foreach ($options as $value) {
    if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
?>

<head profile="http://gmpg.org/xfn/11">
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
	<title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title>
	<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
	<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?>" href="<?php echo $mp_feedburner; ?>" />
	<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/global.css" type="text/css" media="screen,projection" />

	<!-- Layout Options -->
	<?php if ($mp_720 == "true") {?>
	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/720.css" type="text/css" media="screen,projection" />
	<?php }?>

	<?php if ($mp_840 == "true") {?>
	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/840.css" type="text/css" media="screen,projection" />
	<?php }?>

	<?php if ($mp_960 == "true") { 
     			  $newHeight="180px";
     			  $newWidth="470px"; ?>
      <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/960.css" type="text/css" media="screen,projection" />
      <?php }?>

	<?php if ($mp_dark == "true") {?>
	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/dark.css" type="text/css" media="screen,projection" />
	<?php }?>
	<?php if ($mp_two_column == "true") {?>
	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/two-column.css" media="screen,projection" />
	<?php }?>
	<?php if ($mp_two_column_wide == "true") {?>
	<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/two-column-wide.css" type="text/css" media="screen,projection" />
	<?php }?>

	<!-- Comments Script Thanks to Chris Coyier from CSS-Tricks -->

	<SCRIPT TYPE="text/javascript">
	function setbg(color)
	{
	document.getElementById("comment").style.background=color
	}
	</SCRIPT>

	<?php wp_head(); ?>
</head>

 

Not sure if would help to see this code but here is the theme option in the functions.php:

 

array(	"name" => __('960px'),
					"desc" => __("960px Width"),
					"id" => $shortname."_960",
					"std" => "false",
					"type" => "checkbox"),

Link to comment
Share on other sites

It only seems to be echoing in the header section from this:

 

<?php if ($mp_960 == "true") { 
     			  $newHeight="180";
     			  $newWidth="470"; ?>
      <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/960.css" type="text/css" media="screen,projection" />
      <?php }?>

 

When I echo <?php echo $newHeight ?> and <?php echo $newWidth ?> it doesn't appear anywhere - perhaps I'm doing that incorrectly?

Link to comment
Share on other sites

try this in the head section

<?php if ($mp_960 == "true") { 
                $newHeight="180";
                $newWidth="470"; 
                global $newHeight, $newWidth; ?>
      <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/960.css" type="text/css" media="screen,projection" />
      <?php }?>

 

Link to comment
Share on other sites

Whats happening is the variable is being declared inside the function, it needs to be declared as a global variable for use outside the header function

I might of had it the wrong way around but try putting the global line above the 2 variables and see if that changes it, may need to declare them as globals before assigning them.

Link to comment
Share on other sites

Brilliant!  It works great...http://www.minimlpress.com/ - (just a testing ground)

 

You've been very helpful, and I'm going to give you full credit in the development of this theme!  :)

 

I've learned a lot in this process.  Very much appreciated.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.