Archive

Posts Tagged ‘stylesheet’

Enabling Image Formatting In Your WordPress Template

November 20th, 2008 No comments

One neat thing about WordPress is the ability to add images to your posts in a quick and easy manner. You can also create thumbnails of larger images and link to them using a captioned image. The only problem is that when you have sorted out how your images look in your post in the admin section they just don’t appear the same in your template once you have published it.

This is because WordPress creates a set of styles that are used in the admin section and the default WordPress templates. However, these styles are usually left out of custom template stylesheets. If you want to use the same sort of formatting that the WordPress admin section has then open up your stylesheet file in your template and put the following stylesheet rules in at the bottom.

.alignleft {
float:left;
margin:20px 20px 20px 0pt;
}
 
.alignright {
float:right;
margin:20px 0pt 20px 20px;
}
 
.aligncenter {
display:block;
margin-left:auto;
margin-right:auto;
}
 
.wp-caption {
-moz-border-radius-bottomleft:3px;
-moz-border-radius-bottomright:3px;
-moz-border-radius-topleft:3px;
-moz-border-radius-topright:3px;
background-color:#F3F3F3;
border:1px solid #DDDDDD;
margin:10px;
padding-top:4px;
text-align:center;
color:#000000;
}
 
.wp-caption img {
border:0pt none;
margin:0pt;
padding:0pt;
}
 
.wp-caption p.wp-caption-text {
font-size:11px;
line-height:17px;
margin:0pt;
padding:0pt 4px 5px;
}

With this simple fix you will now have much better looking image styles.

One note for blog writers out there is that if you want to float an image left or right then you need to place it before the text you need to wrap it around. When you align things to the center the text will not wrap around it.

Using PHP To Generate CSS

January 31st, 2008 No comments

Generating CSS with PHP has several benefits. For example, you can keep all of your colour declarations as PHP variables so if you need to change any colours it only takes a small edit and not a find/replace operation.

Getting PHP to generate CSS requires just two steps. The first thing to do is to open your CSS file and insert the following line at the top. This tells the browser that the file is CSS.
<?php header("Content-type: text/css"); ?>

The next step is to change the file extension on your CSS file from css to php. This will tell the server to parse the file as a PHP script and not a plain CSS file. This is a necessary step unless you have direct access to your web server and can set CSS files to be parsed as PHP, but I don’t suggest that you do this as it will slow down the server when it serves normal CSS files. Once you have changed the extension you need to change the link to have the same connection.

<link rel="stylesheet" type="text/css" media="screen" href="style.php">

You are now ready to start putting PHP into your CSS files. Take the following CSS file.
body{
  background:#fff;
  color:#333;
}
h1,h2,h3,h4{
  color:#00840;
}

The first thing we might do is to standardise the colour declarations.

<?php
  header("Content-type: text/css");
  $white = '#fff';
  $dkgray = '#333';
  $dkgreen = '#008400';
?>
body{
  background:<?php echo $white; ?>;
  color:<?php echo $dkgray; ?>;
}
h1,h2,h3,h4{
  color:<?php echo $dkgreen; ?>;
}

A better way to use colours is to call the variable by the function rather than the value of the colour. So using the previous example I will rename the colours so that we use the function.

<?php
  header("Content-type: text/css");
  $background = '#fff';
  $text = '#333';
  $heading = '#008400';
?>
body{
  background:<?php echo $background; ?>;
  color:<?php echo $text; ?>;
}
h1,h2,h3,h4{
  color:<?php echo $heading; ?>;
}

Categories: PHP Tags: , , , , ,