Community Documentation

Display recent blog titles on the user profile page.

Last updated December 31, 2007. Created by v1nce on December 31, 2007.
Log in to edit this page.

Displays the most recent (x) blog titles on the user profile page. (x) is set in block configuration admin section. A 'View more' link will appear at the bottom of the block that links back to the users blog page (Ex. /blog/3). Copy and paste the following code to the specified files within the custom_site_specific module folder.

custom_site_specific.info

; $Id$
name = Custom Site Specific
description = Custom site specific code
package = Custom

custom_site_specific.module

<?php
// $Id$
/**
* Implementation of hook_block().
*
* Displays the most recent (x) blog titles on the user profile page. (x) is set in block configuration admin section.
*/
function custom_site_specific_block($op = 'list', $delta = 0, $edit = array()) {
  global
$user;
  switch (
$op) {
    case
'list':
     
$block[0]['info'] = t('Custom recent blog posts');
     
$block[0]['title'] = t('Recent Blog Posts');
      return
$block;
     
    case
'configure':
     
$form['custom_site_specific_blog_num_posts'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Number of blog posts to display'),
       
'#default_value' => variable_get('custom_site_specific_blog_num_posts', 5),
      );
      return
$form;
     
    case
'save':
      if (
$delta == 0) {
       
variable_set('custom_site_specific_blog_num_posts', (int) $edit['custom_site_specific_blog_num_posts']);
      }
      return;
     
    case
'view':
      if (
user_access('access content') && $delta == 0) {
       
$num_posts = variable_get('custom_site_specific_blog_num_posts', 5);
       
$current_user_profile = arg(1);
       
$result = db_query_range("SELECT n.nid, n.title FROM {node} n WHERE n.type = 'blog' AND n.status = 1 AND n.uid = %d ORDER BY n.created DESC", $current_user_profile, 0, $num_posts);
        if (
db_num_rows($result)) {
         
$items = array();
          while (
$blog_posts = db_fetch_object($result)) {
           
$items[] = l($blog_posts->title, 'node/'. $blog_posts->nid, array(), NULL, NULL, FALSE, FALSE);
          }
         
$block['subject'] = t('Recent Blog Posts');
         
// Theme our array of links as an unordered list
         
$block['content'] = theme('item_list', $items);
          if (
db_num_rows($result) == $num_posts) {
           
$block['content'] .= '<div class="block-view-more">'. l(t('View more'), 'blog/'. $current_user_profile, array(), NULL, NULL, FALSE, FALSE) .'</div>';
          }        
        }
      }
      return
$block;            
  }
}
?>

Now we need to override the default blog.module items that get added to the user profile page.

template.php

<?php
/**
* user.module - theme_user_profile override
*
* Theme a user page
* @param $account the user object
* @param $fields a multidimensional array for the fields, in the form of array (
*   'category1' => array(item_array1, item_array2), 'category2' => array(item_array3,
*    .. etc.). Item arrays are formatted as array(array('title' => 'item title',
* 'value' => 'item value', 'class' => 'class-name'), ... etc.). Module names are incorporated
* into the CSS class.
*
* @ingroup themeable
*/
function phptemplate_user_profile($account, $fields) {
 
$output = '<div class="profile">';
 
$output .= theme('user_picture', $account);
  foreach (
$fields as $category => $items) {
   
// Don't display the History category on user profile pages
   
if (strlen($category) > 0) {
     
$output .= '<h2 class="title">'. check_plain($category) .'</h2>';
    }
   
$output .= '<dl>';
    foreach (
$items as $item) {
     
// Don't display blog.module Blog category items
     
if ($item['title'] != 'Blog') {
        if (isset(
$item['title'])) {
         
$output .= '<dt class="'. $item['class'] .'">'. $item['title'] .'</dt>';
        }
       
$output .= '<dd class="'. $item['class'] .'">'. $item['value'] .'</dd>';
      }
    }
   
$output .= '</dl>';
  }
 
$output .= '</div>';

  return
$output;
}
?>

Finally, after you enable the custom_site_specific.module go to admin/build/block and configure the block settings for 'Custom recent blog posts' by clicking on the 'configure' link. Scroll to the bottom and click the radio button with: Show if the following PHP code returns TRUE (PHP-mode, experts only).

Add the following code to the Pages: section:

if (arg(0) == user && is_numeric(arg(1)) {
  return TRUE;
}

Enjoy.
v1nce

About this page

Drupal version
Drupal 5.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.