Last updated on September 22nd, 2014 at 03:26 pm.
I’ve been asked by a few people how I did the latest news block at the bottom of every page on the TerraMedia website. It’s actually really simple when you get down to it, so here you go, all you need to know to do your own latest posts block, it could be used for anything from latest blog posts, latest images in an image gallery, new content, virtually anything.
What you need:
- Drupal 6.x -You can probably do it in 5.x as well, but I haven’t tried it. Let me know if it works for you in 5.x
- Views Module -I did it using version 6.x-2.0-b3, I have also tried it with the newer 6.x-2.0-rc1 and it works fine.
What you need to do:
Once you have the Views module installed and enabled, you’re all set and ready to get into it. Now, I could just export my view for you to import, but where’s the fun in that?
- Navigate to the Views administration area under “Site Building” and click “Add” to create a new view.
- Give it whatever name and description you think is suitable and make sure that the “View Type” is set to “Node” then hit next.
- Just below “Defaults” select “Block” from the drop down menu (the default selection is “Page”) and click the “Add Display” button. You should now have “Block” listed below “Defaults”
- Activate the block options by clicking on “Block”.
Deciding what to display
Now, you need to change a couple of configuration options. You can do this by clicking on the link of the option you want to change. See the screenshot on the right for exactly what my Views configuration looks like.
- Style: This should be set to “Table”.
- Items to display: This controls how many items you want to be shown. Mine is set to 6.
That should be all you need to change in the basic settings. So all of your basic settings should look like this:
- Name: Block
- Title: None
- Style: Table
- Use AJAX: No
- Use pager: No
- Items to display: 6
- More link: No
- Distinct: No
- Access: Unrestricted
- Header: None
- Footer: None
- Empty text: None
- Theme: Information
You can change other things here if you like such as giving it a title, restricting access and so on. I assigned my title from the blocks menu instead of here.
You shouldn’t need to change anything in the block settings. For reference though, they should look like this:
- Admin: None
- Caching: Do not cache
Relationships and Arguments
These aren’t used at all so don’t worry about defining any. Feel free to play around with them though to get some different functionality in.
This is a very important one, the fields you select are what will be displayed. Click the little plus symbol to add fields. I used the following fields for mine:
- Node: Post date
- Node: Title
After you select the fields you want to add, you can then also choose whether to make them links. I made my title field a link to the blog it is for. This is why I didn’t use a more link as specified in the basic settings. I have also used the post date and I have used this to sort my blog posts as I will explain soon. When adding the date field, you can specify how you would like the date displayed either from a series of pre-defined options, or using the PHP code to create a custom date display. Because I didn’t want the time, just the date, I used the custom format – F j, Y. This displays the month in full word format (F), the day of the month (j) and the year (Y). Full information on the time displays is available in the PHP docs.
Of course you can select any other fields you like to display, this is just what I used.
I sorted mine by one thing, as I mentioned earlier, the date, and they are sorted in descending order. Click the little plus to add a sort criteria. Mine is:
- Node: Post date desc
You can sort by other fields as well, so you may prefer to do that depending on what you are trying to do.
Filters are really useful, this is where you get to control just what type of content to display. Mine is displaying blog posts only, so it is set as:
- Node: Type = Blog entry
This is set, like with the other options, by clicking the little plus symbol. Again you can filter by just about anything, so this is really where you control just what sort of things you want to display. Go nuts with it for all sorts of useful blocks.
Make sure you click save down the bottom when you’re done, there’s nothing more frustrating then getting it just how you want it then forgetting to save it before navigating to another page.
Where to display it
Of course what good is it if you can’t use it anywhere. We gave it a block display, this means that it will be added to the list of blocks in the site building section of administer.
Navigate to the blocks and it will be there. If you gave it a title, it will be listed with that title. If you didn’t, it will have the machine-readable name that you defined when you first created the view. Go to configure and give it whatever title you like and set whatever other settings you like, then set what region you would like it located in.
Voila, you’ve got yourself a latest something block! It may need a little styling with CSS to get it looking just how you want it to, but there you go, as quick and easy as that!
Let me know what you do with it if you find this useful. Drop me a link in the comments or leave a trackback, I don’t mind. No spam though please, it will get deleted.
If you get it working in older versions of Drupal as well, let me know. If you have any problems or questions, don’t hesitate to ask, I’ll help you out as soon as I can. Enjoy.