Archive for the ‘WordPress: Lessons Learned’ Category

Wordpress: Make home page show latest post (in the “single post” format)

Saturday, April 28th, 2007

In a custom wordpress theme design I was working on tonight, I was trying to do as the title describes - make the home page show only one post (yes I know there’s already a built in way to do this) but I wanted it to show up the same way the single view shows it - with the comments expanded and the comments form visible, etc. If you know of another/better way to do this, please say so in the comments. But this DID do the trick. I found this page in the support archives. The idea was to create your index.php to look like you wanted (ie. exactly like the “single.php” page) but add this before you start the loop:

<?php
$i=0; // Initialize to Zero;
if (have_posts()) :
while (have_posts()) : the_post();
if ($i==0) {$recentpostid = $post->ID; $i=$i+1;}
endwhile;
endif;
//get only the latest post
$posts = query_posts( 'p='.$recentpostid."'");
?>

and then you start your loop like you normally would…

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

Worked for me!

Update As Danny pointed out in the comments - there’s a simpler way to do this. Put the following code on the page (I put it before the loop and it worked)
<?php $wp_query->is_single = true; ?>
Much simpler :)

Overcoming the upgrade

Friday, December 22nd, 2006

I’ll make notes of issues I ran into and the solutions as I go along, but so far I’ve had two three issues come up when upgrading.

1) When I ran the upgrade, it sort of died before finishing… I refreshed and it told me it was all set. Checked on the main page, and everything seemed to be okay - but when I logged in to start tinkering around with the settings, I couldn’t log in completely. I got the dashboard with no navigation and then I clicked on something and all it wanted to do was send me to the profile page - but with nothing showing except “You do not have sufficient permissions to access this page”. Googled for a solution and found one on Mark’s site. Ran his script, and all was well again.

2) Then I discovered that clicking on the the “previous entries” link at the bottom sent me to a “page can not be found” error page. Google to the rescue - and found one on the Wordpress Codex… (scroll ALLLL the way down to “Paged Navigation doesn’t work” - There it will tell you that the error I was getting was due to the .htaccess file that WordPress generates. Delete the contents (or at least the wordpress stuff in there) and manually add the code back in from the Options -> Permalinks page. Again, all was well again.

3) This one was really weird. I had a post that was showing up in Firefox but not IE. It was like it didn’t exist at all on IE. Wasn’t in the source. Try to pull up the post page, and it gives you a 404 error not found. Turns out - and maybe this was because of the glitch during the upgrade - the post didn’t have any status indicated. It wasn’t marked as “Published”, “Draft”, or “Private”. (The weird thing is that, then WHY did Firefox show it at all?!?) Anyway, clicking the “Published” box and re-saving the post seemed to fix the problem.

If you find any other broken links or errors, etc. - please let me know!

Above The Fold

Wednesday, January 12th, 2005

I’ve been a little absent around here lately, and part of the reason is that I’ve been working on a project that I can now formally announce here! I’m starting up a little side business. You see, I’ve always loved doing origami - from the time that I was a kid. I can’t even remember when I started. And now I’ve decided to turn some of my creations into things I can share. Ok… sell. :D My little side business is called Above The Fold (AboveTheFold.org) and it’s an online store for (at the moment) origami earrings, cards, and magnets. (More things will be coming).

(As for the .org - instead of the .com - the squatter “owner” of the .com is willing to sell it… for a mere $7K! heh. I’ll be lucky if I sell 5 pairs of earrings this whole year!! LOL! I don’t think it’s in my budget!)

This may not seem very “scripty” related - but actually it is. When working on my ecommerce site, I briefly looked into some pre-made web-shops, but they were all a little “too big” for what I wanted to do. I wanted to add products easily. I wanted to have “add to cart” buttons automatically generated.. etc. etc. I thought I’d just start writing something on my own - but then realized that I could probably do A LOT of what I wanted with WordPress!! Not sure how many ecommerce sites are WordPress driven - but I did it quite easily. Now that it’s up and running, I can see more “ecommerce” type “plugins” I can add - but for now, I’m busy making origami.

But I thought, for those interested, I might share some of my little tricks that make the site run. Many of them are take-offs on what I did for Christine with Pixelog.
(more…)

WP security bug

Tuesday, December 7th, 2004

There’s a pretty serious security bug with WP 1.2.1 (and the current 1.3 alpha), one that can make it so your blog is basically unusable (not permanently, as far as I can tell) but still - if you’re using WordPress you should probably make this change.

In any case, here is how you fix the problem. It’s a very easy fix. If you can search for text on a page, you can fix this problem.

I hesitated posting this, because I don’t want to “start a panic” - nor do I want to give instructions on how to hack WP blogs. But I do think it’s important that people go ahead and make this change.

[brought to my attention by Christine]

Conditional text despite content not being echoed

Monday, November 22nd, 2004

One of the things Christine had asked me to add to pixelog, was, when no “previous” entry was available - to have the text still show up, but be “greyed out”. I remembered a post Alex King had made on the hackers mailing list about capturing the text that some functions typically echo into a variable. What he suggested was to do the following:

<?php
ob_start();
the_title();
$my_title = ob_get_contents();
ob_end_clean();
print($my_title);
?>

This was the trick I needed. Here’s the code I used do the “greyed out” effect. (The issue here, if you don’t know - is that next_post() will simply echo nothing if there is no next post)

<?php
//determine if there is a next post
ob_start();
next_post();
$nextpost = ob_get_contents();
ob_end_clean();
//now print the appropriate link or greyed out text
if ($nextpost) {
next_post(’&laquo; % | ‘,’previous’,'no’);
} else { ?>
<span class=”grey”>&laquo; previous</span> |
<?php } ?>

(Yes I know I’m using the text “previous” with the next_post function… that had to do with her preference of displaying the photos)

The redesign of Pixelog.org

Thursday, October 21st, 2004

One of the things that drove me to WordPress was the fact that it was PHP based. It leaves the doors wide open for you to do practically any kind of hack you could possibly think of.

I recently reworked Pixelog.org for Christine. The original was a table based design, powered by MT. Now it’s all CSS based, valid XHTML 1.0 Strict, and powered by WordPress!

However, to get everything to work just so I had to pull a few tricks….
(more…)

Limit drop down comments

Saturday, September 4th, 2004

I’m kind of surprised I didn’t post this already (maybe I did and I just can’t find it) - but on another blog, I have drop down comments set so it only does this for the first ‘x’ number of posts with comments. (Otherwise download times can get a little bogged down if there’s a lot of posts with comments on the page…) To do that I have this at the top of my page:

<?php
$showcommentmax = 3;
$currentshowcomment = 0;
?>

Then I have this code around the drop down comments call:

<?php
if ($showcommentmax > $currentshowcomment) {
include(ABSPATH . ‘wp-dropdown-comments.php’);
if (get_comments_count() > 0) {
$currentshowcomment++;
// only counting show comment if it there are comments to show
}
}
?>

Optionally show excerpt

Saturday, June 5th, 2004

There may be another way to do this, but I wanted to show the excerpt, but only if I had actually typed something in. Just doing the following, will show part of the post if there’s no excerpt (maybe I was doing it wrong?):

<?php the_excerpt(); ?>

In any case, I use the “excerpt” field for “Guest author” information. So I have it only display when there IS guest author information by doing the following:

<?php if ($post->post_excerpt != “” ) {
echo “<p><b>”.$post->post_excerpt.”</b></p>”;
}
?>

“Previous Post | Home | Next Post” links in wordpress

Sunday, May 23rd, 2004

Charlene just mentioned this in the comments of another post, so I thought I’d post how I’m doing it on my pages (or will be doing it - once I finish them all)

After the “WordPress Post loop” begins (which starts like this) :

<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>

Put this:

<?php if ($single) { ?>
<?php previous_post(’% |’,”,’yes’) ?> <a href=”<?php bloginfo(’url’); ?>”>Home</a> <?php next_post(’| %’,”,’yes’) ?>
<?php } ?>

post updated 5/29/04 to include a few of the suggestions in the comments.

Change allowed tags in comments

Saturday, May 22nd, 2004

There’s security reasons why the tags that are allowed are limited to the ones listed, but if you must have something like imgs allowed in your comments, here’s an explanation of how to change that.

(kses.php is in your wp-includes folder)