ページの前後のノード リンク/サムネイルを取得し、タイトル、ファイル URI、またはファイル名に従って結果を並べ替えようとしています...
コードはデータベースにクエリを実行し、ノード ID (nid、n.nid) に従って前と次のノード リンクを出力します。ノード タイトル (title、n.title)、ファイル名 (filename、f.filename)、またはファイル URI (uri、f.uri) に従って結果を並べ替えたいと考えています。
ただし、この行を変更すると:
->orderBy('n.nid', $order)
に:
->orderBy('n.title', $order)
うまくいきません。唯一の違いは、ある画像ギャラリーの最後のページから別のページに移動すると順序がわずかに変わることですが、ギャラリー内ではすべて同じです。問題は、ギャラリーが 1 つしかなく、しばらくしてから新しい画像を挿入することにした場合です。ノード ID は他のものとはまったく異なり、このコードはそれを取得しません。
表示される他の部分も変更してみnid
ましたが、うまくいきません。MySQL と PHP をよく理解している人にとっては些細なことだと思いますが、私は何時間もそれで立ち往生しており、助けていただければ幸いです。
コード全体は次のとおりです (Vlad Stratulat から、最初に見つかったのはhere ):
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_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 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' => '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', 0); ?>
<?php print dad_prev_next($node->nid, 'n', 0); ?>
編集2:
strcmp 関数を使用しようとしています:
function dad_prev_next($title = NULL, $op = 'p', $start = 0) {
if ($op == 'p') {
$strcmp = '1';
$order = 'ASC';
}
elseif ($op == 'n') {
$strcmp = '2';
$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_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 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.title', $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' => '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;
}
現在ログに記録されているエラーはありませんが、常に同じ写真が表示されます。アルファベット順のリストの最初の 2 枚と最後の 2 枚です。