I'm designing a site which will, ideally, feature three distinct blogs.
What I would like to do is give each blog a representative block on the home page containing the author's pic, the name of the blog, a teaser to their most recent entry, and a link to the full post.

So far, the best thing I've found to do this is the Views module, but it's really WAY more than seems necessary. Surely someone has done this before and can give me some pointers?

Thanks in advance!

Comments

avadhutp’s picture

Plain SQL queries! Faster and more lightwieght. If you could tell us how the blogs are diffrentiated (by author, content type, categories, etc.) we could write the queries for you. :-)

cdybdahl’s picture

Thanks so much for the speedy response and particularly for the extremely generous offer!

I'm new to the "under the hood" portions of drupal, so I can't tell you how much of a help that would be.
The blogs will be differentiated only by author. So if I have Jim, Bob, and Mary all blogging on the site, ideally each one would have a block of content on the home page with their photo, the name of the blog, the teaser from the most recent entry, and a link to the full text.

As far as the easiest way to incorporate this on a page: would it best be included in a custom block type, or is there some other (fancier) way of doing it?

icecreamyou’s picture

For one block with all authors:

$authors = array(1, 2, 3); //replace 1, 2, and 3 with the user IDs of the users who own the blogs
$limit = 5; //set to the number of recent blog posts you want
$output = array();
$i = 0;
foreach ($authors as $uid) {
  //show author's name and picture
  $author = db_fetch_array(db_query("SELECT uid, name, picture FROM {users} WHERE uid = %d", $uid));
  print "<p><img src='". $author['picture'] ."' /></p><p>". l($author['name'], "user/". $author['uid']) ."</p>";
  //get the nodes
  $query = db_query_range("SELECT nid, title, created FROM {node} WHERE uid = %d AND type = 'blog' ORDER BY created DESC", $uid, 0, $limit);
  while ($block = db_fetch_array($query)) {
    print "<p>". l($block['title'], "node/". $block['nid']) ." <span class='submitted'>" format_interval($block['created'], 1) ." ago</span></p>";
  }
  $i++;
  print "<hr />";
}

If you want a separate block for each author, set $authors to the UID of just one author and remove the foreach() and corresponding curly braces.

cdybdahl’s picture

But how can I get this code to work? I have tried pasting it into the block as text and the code shows up in the resulting block instead of the results.

icecreamyou’s picture

You need to change the input filter to PHP. If you're User 1 or if you have permission to use the PHP filter, you'll see an expandable fieldset below the textarea where you can change the input filter. If you're using a WYSIWYG editor, you'll need to disable it in most cases.

And by the way, when I say "change $authors to the UID of just one author" I mean remove the array() part too.

imkamalkumar’s picture

Thank you so much for this script. It was exactly what I was looking for. But I got following error in drupal 7.. (I already fixed line 13)
Fatal error: Call to undefined function db_fetch_array() in /home/content/k/a/t/kathamitho/html/main/modules/php/php.module(75) : eval()'d code on line 8

http://drupal.org/node/1020760#comment-3924556 This link says:

"One of your blocks has PHP code in it which tries to iterate on a database result using old database functions. You'll have to update that block. Setting to normal, might become a won't fix eventually."

Please can you provide a fix for drupal 7. That would be really helpful for a beginner like me.

cdybdahl’s picture

I am learning quite a lot about Drupal just troubleshooting this little issue, which is exactly what I wanted.
Thanks to everyone who has helped so far.

When I plug the code given above in, I get the following error:
Parse error: syntax error, unexpected T_STRING in /home/content/h/a/p/happyeyeballs/html/includes/common.inc(1537) : eval()'d code on line 13

I also have no idea how to go about finding the uid for a given user. This information does not appear to exist anywhere but in the database -- is this correct? So the only way to get a user's uid is through a query? Is there a way to pull the information based on username instead, or is that a bad idea since the user can change their name at any time?

icecreamyou’s picture

Line 13 should read

    print "<p>". l($block['title'], "node/". $block['nid']) ." <span class='submitted'>". format_interval(time() - $block['created'], 1) ." ago</span></p>";

--I forgot the "." after the span, and I forgot the time() in format_interval.

The user's UID is the number that appears after the word "user" in the URL when you're looking at a user's profile.

And, just FYI, you can get the current user's UID at any time like this:

global $user;
$uid = $user->uid;

...not that that's very useful here.

cdybdahl’s picture

Replacing that line of code works beautifully. This is exactly what I needed.

What a great community this is.
I look forward to reverse-engineering this code so I can learn how it works.

Thanks again!

icecreamyou’s picture

If you need any help or explanation of that code, feel free to ask. I can help with anything you don't understand--it certainly took me long enough to figure some things out, and it's such a pain to have to go back and change old code that you really want to get it right the first time. :D

SteelerE’s picture

How can I add a specified amount of the actual blog to the result set. For example, I would like to use this as a teaser in the right rail. I would like to show the most recent blog and say 250 characters of the content with a link the /blog page. Any feedback you can provide will be helpful.

adam_b’s picture

Just to avoid confusion, I think what you're trying to do is create a block in the right sidebar? Unless you're *really* into writing your own SQL code, Views is the way to go for this.

SteelerE’s picture

am trying to achieve. I am absolutely new to Drupal and PHP (C# developer by trade) for that matter so I must apologize for my ignorance. That being said I found the Views module a bit limiting and in many ways somewhat frustrating. Then again I have to assume I was doing something wrong :(. In the end I decided to write custom SQL for this. I was forced to join "node" on "node_revisions" and "url_alias" to achieve what I wanted and I couldn't seem to pull it off with the Views module. I will ultimately turn this code into a module like "Latest Blog Post" (may only fit in house needs). I really appreciate you getting back to me. The Drupal community is very encouraging and I can honestly say I will stay with it. Best.

adam_b’s picture

Well I'm not a developer at all, so I always go for a solution which doesn't require me to write code. If you're happy with it, good for you & good luck.