0

I have a content type called blogs and in which I referred a Taxonomy term which have three different terms in it.

Now what I want to achieve is three blocks with two recent posts for each taxonomy term.

Which should look like :

||       TERM 1            ||          TERM 2           ||        TERM3        ||

         title1                        title1                     title1
          desc                          desc                       desc

         title2                        title2                     title2
          desc                          desc                       desc
   
         read more...                  read more...              read more... 

I know we can create using seperate blocks each.

I need this to be created in a single block in views if this is possible.

I've tried group by in field settings, but it yields me something like this:

TERM 1

title1

desc

TERM 2

title1

desc

title2

desc

title3

desc

TERM 3

title1

desc

Since I specified 6 nodes to be displayed it displays 6 recent post irrespective of the taxonomy term.

4
  • You must deliver the three-term-block in Views, or just in a single block?
    – tvl
    Commented Oct 30, 2014 at 13:24
  • I need all these to be rendered as a single block in grid view horizontally Commented Oct 30, 2014 at 13:53
  • The Term names are fixed? There will be more term maybe?
    – tvl
    Commented Oct 30, 2014 at 13:58
  • Let's say its fixed.. What should I do.. Commented Oct 30, 2014 at 14:09

2 Answers 2

0

I don't think a single View can you your job.

In the case you have fixed Term a non best practise approach will be to create 3 Views and a custom module

Views:

  • Create new view without add a page or a block
  • Add a filter to taxonomy term = 'Term 1'
  • Limit the results to the desired number, remove pager, add read more link.
  • Do the same for Term 2 and Term 3

Module:

term_display.info

name = 3 Term display
description = Display the articles of 3 terms
core = 7.x
package = My custom modules
version = 7.x-0.1


term_display.module

<?php
/**
 * Implements hook_block_info().
 */
function term_display_block_info() {
  $blocks['term_display'] = array(
    'info' => t('term display'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 *
 * Prepares the contents of the block.
 */
function term_display_block_view($delta = '') {
  // Initialize a block array.
  $block = array();
  // In case that we have more that one blocks the $delta will define witch one
  // to render.
  switch ($delta) {
    case 'term_display':

      // Give a block title.
      $block['subject'] = t('Term Display');

      // Tells the theme to render the content using ul list.
      $block['content'] = theme('item_list', array(
        'items' => term_display_create_items(),
        'type' => 'ul',
      ));
      break;
  }
  return $block;
}

/**
 * Create the items that will be render in the block.
 *
 * @return array
 *   Return an array of the block's items.
 */
function term_display_create_items() {
  // Initialize the items that will be rendered.
  $items[] = array(
    'data' =>  'Term 1' . views_embed_view('trem_view_1', 'default'),
  );
  $items[] = array(
    'data' =>  'Term 2' . views_embed_view('trem_view_2', 'default'),
  );
  return $items;
}

All the job is done in term_display_create_items() that embeds the View views_embed_view('VIEWS_MACHINE_NAME', 'DISPLAY_ID');

A better approach will be to give the Term as a parameter to the views_embed_view() and fetch it from the View. So you will have a single View that uses Contextual Filters to give you your results. But follow the above steps and you will find your way ;)

A third and more dynamically solution is using the Views Field Views but it will add a huge overhead to your site + will take more time to build it.

1
  • Hey thanks a lot for your quick response.. But check my answer and verify it whether its a right way of doing this.. Commented Oct 30, 2014 at 14:35
0

I found a way to display such view and render it as single block using attachments

Instead of creating separate blocks for each this seems to be a good idea. Just Create three different attachments in which you can override the term filter to get two recent posts each.

1. Create a block in views and add required fields in it

2. Add a filter criteria for a specific term and add a sort criteria for post date(desc)

3. Now add an attachment and just override the term filter for another term and repeat it for the other filters too.

To achieve the grid view I just applied CSS classes Since I use Bootstrap as my base theme by applying the class: col-lg-4 col-md-4 col-sm-4

Now you can render three different blocks as "one"....!!!!

4
  • Sounds good. Can you be more verbose by a step by step approach?
    – tvl
    Commented Oct 30, 2014 at 14:42
  • Can you also explain what is the advantage you gain by doing this via CSS rather than using the 3 block approach? Commented Oct 30, 2014 at 22:52
  • Even if you create three separate blocks you need to apply some style to make it looks like a grid. and It sound goods if we render it as a single block instead of rendering there different blocks on the same region Commented Oct 31, 2014 at 8:16
  • And it is also meaningful to create them as a single block since they serve the same purpose of displaying recent news Commented Oct 31, 2014 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.