0

I want to display 4 featured posts on home page. 2 from the content type 1 and 2 from the content type 2. For this the straight forward way is to create two different views blocks. What I want to know if it is possible to display all 4 posts through a single view ??

  • 1
    why don't you create a view with the first content-type and then embed it as a header view in the main view with the second content type? This way you'll have one view only to display in your page, if it's what you are looking for – Kojo Nov 29 '13 at 22:50
1

I have a multipart part answer to this question:

  1. No, there's no way to do show exactly two nodes of one content type and two of another type using only 1 view if you want them grouped in a specific order.
  2. If you don't care about that nodes of different content types are mixed up together, you can change your content-type filter to include multiple content types. This only uses one view but may not be exactly what you want.
  3. If you give up on using only 1 view, Kojo's comment lays out an excellent and quick way to embed views inside of another view to create the desired effect. Just to recap: create one view for each content type, and put one of the views in the header of the other view. This gives the desired effect, but gives up on using only 1 view.
  4. You could use a SQL query like this to create a custom theme hook that would have the intended effect without using Views at all. It would require SQL knowledge to maintain it, but it would also have a lot less overhead. Here's what you'd need to do:

Inside template.php in your theme:

<?php
/**
 * Implements hook_theme().
 *
 * This registers a custom function for use with the theme() function.
 * To make your featured nodes show up in a template, add this code:
 *
 *   print theme('featured_nodes');
 *
 * Whenever theme() is called with that argument, a certain preprocess function
 * will be called. I like to write SQL in those preprocess functions to 
 * find raw data from the database. Then a template will be used to display 
 * the information looked up in the preprocess function. Finally, the template
 * is returned as a string wherever theme() was originally called.
 *
 * Look up hook_theme in the API docs at http://api.drupal.org/ for more info.
 */
function mytheme_theme() {
    return array(
        'features_nodes'] = array(
            // Here I define which template should be used. 
            // In this case, the template will be called featured-nodes.tpl.php
            'template' => 'featured-nodes',
        )
    );
}

/**
 * Implements hook_preprocess_hook().
 *
 * This function is triggered before the template is rendered. You can
 * pass variables into the template simply by adding them to $vars.
 */
function mytheme_preprocess_features_nodes(&$vars) {
    // This is a really simple query that looks up all the fields
    // on the node table for the 2 most recent nodes of type 1,
    // then it adds all the fields of the 2 most recent nodes of type 2.
    // Learning SQL is bigger than the scope of this question.
    $results = db_query('
        SELECT *
          FROM {node}
         WHERE type = "machine_name_of_content_type_1"
         LIMIT 2
      ORDER BY created DESC
         UNION
        SELECT *
          FROM {node}
         WHERE type = "machine_name_of_content_type_2"
      ORDER BY created DESC
         LIMIT 2
    ');
    $featured_nodes = array();

    // We use db_fetch_object() repeatedly to read out each result row.
    while ($row = db_fetch_object($result)) {
        $featured_nodes[] = $row;
    }

    // Anything in $vars will be added to the templates when they are rendered.
    $vars['featured_nodes'] = $featured_nodes;
} 

Then, inside featured-nodes.tpl you'll have access the $featured_nodes variable and you can display some very basic information about the nodes that you want (nid, title, author, and created date).

Here's an example of what should go inside featured-nodes.tpl.php:

<ul class="featured-nodes">
    <?php foreach ($featured_nodes as $node) : ?>
    <li><?php print l($node->title, "node/{$node->nid}"); ?></li>
    <?php endforeach; ?>
</ul>

NOTE: None of the above code is tested and it may require some tweaking.

Here's an in-depth blog post I wrote with more information on how and why to do version 4.

| improve this answer | |
  • Well this is a nice and full documented answer, but isn't solution 4 an overkill when you could do it and maintain it so simply with the third one? – Kojo Nov 30 '13 at 5:30
  • @Kojo, yes this is way overkill for the OP's question. However, I believe it's important to learn SQL especially when it avoids debugging bitfalls in Views based on my own experience. Feel free to ignore #4. – Rustavore Dec 2 '13 at 21:09
0
  1. Create a view for your first content type. Limit the number of entries to two. Create the view as a page or a block, depending on your needs.

  2. Add an attachment to the view by clicking 'add' next to 'Page' and/or 'Block' depending on step 1. Filter for your second content type.

  3. In 'attachment settings' (second column), set 'attach to' to Page or Block, depending on 1. Set attachment position to 'after'.

That should do it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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