【问题标题】:Prev/Next node navigation with a thumbnail in a full node上一个/下一个节点导航,带有完整节点中的缩略图
【发布时间】:2012-01-17 20:22:46
【问题描述】:

我一直在努力解决这个问题并让它发挥作用,但由于我不是程序员,只是一个编码知识有限的设计师,所以我一直在碰壁。

我正在制作我自己的照片库,我想将两张上一张或下一张图片显示为链接的缩略图。我发现这个脚本可以工作并显示文本链接,但不知道如何适应我的需要。

template.php

<?php
function dad_prev_next($current_node = NULL, $op = 'p') {
    if ($op == 'p') {
        $sql_op = '<';
        $order = 'DESC';
    } elseif ($op == 'n') {
        $sql_op = '>';
        $order = 'ASC';
    } else {
        return NULL;
    }

    $output = NULL;

    $sql = "SELECT n.nid, n.title
             FROM {node} n
             WHERE n.nid $sql_op :nid
             AND type IN ('photos')
             AND status = 1
             ORDER BY nid $order
             LIMIT 1";
    $result = db_query($sql, array(':nid' => $current_node -> nid));
    foreach ($result as $data) {
    }
    if (isset($data)) {            


        if ($op == 'n')            
            return l("Next", "node/$data->nid", array('html' => TRUE));
        else if ($op == 'p')
            return l("Previous", "node/$data->nid", array('html' => TRUE));
    }
}
?>

node.tpl.php

<?php print dad_prev_next($node,'p') . " / " . dad_prev_next($node,'n'); ?>

工作示例

这个显示了设计时的两张上一张/下一张图片

template.php

function dad_prev_next($nid = NULL, $op = 'p', $start = 0) {
    if ($op == 'p') {
        $sql_op = '>';
        $order = 'ASC';
    }
    elseif ($op == 'n') {
        $sql_op = '<';
        $order = 'DESC';
    }
    else {
        return NULL;
    }

    $output = '';

    // your node must have an image type field
    // let's say it's name is IMAGEFIELD
    // select from node table
    $query = db_select('node', 'n');
    // join node table with image field table
    $query->leftJoin('field_data_field_photo', 'i', 'i.entity_id = n.nid');
    // join file managed table where all data about managed files stored
    $query->leftJoin('file_managed', 'f', 'f.fid = i.field_photo_fid');
    $query
        // select nid and title from node
        ->fields('n', array('nid', 'title'))
        // select uri from file_managed (image path)
        ->fields('f', array('uri'))
        // select image alt and title
        ->fields('i', array('field_photo_alt', 'field_photo_title'))
        // where nid "greater than"/"lower than" our current node nid
        ->condition('n.nid', $nid, $sql_op)
        // where node type in array('your content types')
        ->condition('n.type', array('photos'), 'IN')
        // where node is published
        ->condition('n.status', 1)
        // where requested node has image to display (if you want thumbnail)
        ->condition('f.uri', '', '!=')
        // order by nid
        ->orderBy('n.nid', $order)
        // limit result to 1
        ->range($start, 1);     

    // make query
    $result = $query->execute()->fetchAll();

    foreach ($result as $node) {
        // theme your thumbnail image
        $variables = array(
            // default image style name `thumbnail`
            // you can use your own by following
            // admin/config/media/image-styles on your site
            'style_name' => 'square_small',
            'path' => $node->uri,
            'alt' => $node->field_photo_alt,
            'title' => $node->field_photo_title
        );
        $image = theme('image_style', $variables);

        $options = array(
            'html' => TRUE,
            'attributes' => array(
                'title' => $node->title
            )
        );
        $output = l($image, "node/{$node->nid}", $options);
    }

    return $output;
}

node.tpl.php

<?php print dad_prev_next($node->nid, 'p', 1); ?>
<?php print dad_prev_next($node->nid, 'p', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 1); ?>

【问题讨论】:

    标签: drupal-7 drupal-theming


    【解决方案1】:

    template.php

    function dad_prev_next($nid = NULL, $op = 'p', $qty = 1) {
        if ($op == 'p') {
            $sql_op = '<';
            $order = 'DESC';
        }
        elseif ($op == 'n') {
            $sql_op = '>';
            $order = 'ASC';
        }
        else {
            return NULL;
        }
    
        $output = '';
    
        // your node must have an image type field
        // let's say it's name is IMAGEFIELD
        // select from node table
        $query = db_select('node', 'n');
        // join node table with image field table
        $query->leftJoin('field_data_field_IMAGEFIELD', 'i', 'i.entity_id = n.nid');
        // join file managed table where all data about managed files stored
        $query->leftJoin('file_managed', 'f', 'f.fid = i.field_IMAGEFIELD_fid');
        $query
            // select nid and title from node
            ->fields('n', array('nid', 'title'))
            // select uri from file_managed (image path)
            ->fields('f', array('uri'))
            // select image alt and title
            ->fields('i', array('field_IMAGEFIELD_alt', 'field_IMAGEFIELD_title'))
            // where nid "greater than"/"lower than" our current node nid
            ->condition('n.nid', $nid, $sql_op)
            // where node type in array('your content types')
            ->condition('n.type', array('photos'), 'IN')
            // where node is published
            ->condition('n.status', 1)
            // where requested node has image to display (if you want thumbnail)
            ->condition('f.uri', '', '!=')
            // order by nid
            ->orderBy('n.nid', $order)
            // limit result to $qty
            ->range(0, $qty);
    
        // make query
        $result = $query->execute()->fetchAll();
    
        foreach ($result as $node) {
            // theme your thumbnail image
            $variables = array(
                // default image style name `thumbnail`
                // you can use your own by following
                // admin/config/media/image-styles on your site
                'style_name' => 'thumbnail',
                'path' => $node->uri,
                'alt' => $node->field_IMAGEFIELD_alt,
                'title' => $node->field_IMAGEFIELD_title
            );
            $image = theme('image_style', $variables);
    
            $options = array(
                'html' => TRUE,
                'attributes' => array(
                    'title' => $node->title
                )
            );
            $output = l($image, "node/{$node->nid}", $options);
        }
    
        return $output;
    }
    

    node.tpl.php

    <?php print dad_prev_next($node->nid, 'p', 2); ?>
    <?php print dad_prev_next($node->nid, 'n', 2); ?>
    

    【讨论】:

    • 感谢它的工作,它的神经保护。在我兄弟的帮助下,我设法修改了脚本,以便按照我的设计显示两张上一张/下一张照片。希望对其他人也有帮助。
    • 我修改了你的代码。无需为每个节点调用 2 次,您只需增加对节点的限制即可。注意$qty 变量而不是你拥有的startnode.tpl.php。希望对你更好;]
    • 嗯...我没有看到修改。
    • 我对您的文字进行了更改,但似乎没有应用。查看我的更新答案。
    • @VladStratulat 我已经发布了一个与您的代码相关的问题,您介意检查一下吗?先感谢您! stackoverflow.com/questions/13120363/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2018-10-22
    相关资源
    最近更新 更多