پست تصادفی در آپادانا
با سلام. به درخواست کاربر گرامی این آموزش (نحوه نمایش یک پست به صورت تصادفی در سایت) قرار گرفت .
ابتدا در پوشه engine سایت یک فایل جدید با نام custom-file.php ایجاد کنید کد زیر را در آن بگذارید و ذخیره کتید :
<?php
// author: Mohamad Zangane
// email: info@tikbar.ir
// link: http://www.tikbar.ir
function block_posts_custom($op = null, $id = null, $position = null)
{
if ($op == 'remove-cache')
{
remove_cache('module-posts-block-last-custom-'.$id);
return true;
}
if (!$rows = get_cache('module-posts-block-last-custom-'.$id))
{
$where = null;
$op['total'] = !isset($op['total']) || intval($op['total']) <= 1? 1 : intval($op['total']);
$op['cat'] = !isset($op['cat']) || intval($op['cat']) <= 0? 0 : intval($op['cat']);
$op['hits'] = !isset($op['hits']) || strtolower($op['hits']) != 'true'? false : true;
$op['order'] = !isset($op['order'])? 'RAND' : strtoupper($op['order']);
$op['order'] = $op['order'] != 'RAND' && $op['order'] != 'ASC' && $op['order'] != 'RAND'? 'DESC' : $op['order'];
if ($op['cat'] > 0)
{
$categories = &posts_categories();
$where = 'FIND_IN_SET('.$op['cat'].', post_categories)';
if (isset($categories[$op['cat']]['term_parent']) && $categories[$op['cat']]['term_parent'] == 0)
{
foreach ($categories as $c)
{
if ($c['term_parent'] != $op['cat']) continue;
$where .= ' OR FIND_IN_SET('.$c['term_id'].', post_categories)';
}
}
$where = 'AND ('.$where.')';
}
global $d;
$query = "SELECT post_title,post_image,post_tags, post_text, post_name, post_id, post_hits FROM `#__posts` WHERE post_approve='1' AND post_date <= '".time_now."' ".$where." ORDER BY ".($op['order'] == 'RAND'? 'RAND()' : ($op['hits'] == 1? "post_hits {$op['order']}" : "post_date {$op['order']}, post_id {$op['order']}"))." LIMIT {$op['total']}";
$rows = $d->get_row($query);
if ($op['order'] != 'RAND')
{
set_cache('module-posts-block-last-custom-'.$id, $rows);
}
}
if (is_array($rows) && count($rows))
{
global $options;
$html = '<ul class="apadana-block-last-posts-custom'.(isset($op['cat']) && intval($op['cat']) > 0? ' apadana-block-last-posts-cat-'.intval($op['cat']) : null).'">'.n;
foreach ($rows as $row)
{
$row['post_title'] = nohtml($row['post_title']);
if (apadana_strlen($row['post_title']) > 30)
{
$row['post_title'] = apadana_substr($row['post_title'], 0, 27);
$row['post_title'] .= ' ...';
}
if (apadana_strlen($row['post_text'])> 400)
{
$row['post_text'] = apadana_substr($row['post_text'], 0, 400);
$row['post_text'] .= ' ...';
}
$html .= '<li><img src="'.$row['post_image'].'" style="float: left"><a href="'.url('posts/'.($options['rewrite'] == 1? $row['post_name'] : $row['post_id'])).'">» '.$row['post_title'].'</a><Hr>'.$row['post_text'].'<hr><span> '.$row['post_hits'].' بازدید</span>| برچسب ها : '.$row['post_tags'].' | <a href="'.url('posts/'.($options['rewrite'] == 1? $row['post_name'] : $row['post_id'])).'">مشاهده مطلب ...</a></li>'.n;
}
$html .= '</ul>';
}
else
{
$html = 'هیچ پستی وجود ندارد!';
}
return $html;
}
?>
حال می توانید یک بلوک با تابع posts_custom ایجاد کنید و یا در صورتی که می خواهید از لینک یا تصویر جهت این کار استفاده کنید می توانید این گونه عمل کنید : (فقط در فایل های tpl قالب)
متن :
<a onclick="$('#postcu').slideToggle('fast');">مطلب تصادفی</a><br>
<div id="postcu" style="display: none;">{function name="block_posts_custom" args="array('total'=> 1, 'hits'=> false, 'order'=> rand)|ok"}</div>
عکس :
<a onclick="$('#postcu').slideToggle('fast');"><img src="Your Link Image"></a><br>
<div id="postcu" style="display: none;">{function name="block_posts_custom" args="array('total'=> 1, 'hits'=> false, 'order'=> rand)|ok"}</div>
موفق و پیروز باشید
هرگونه سوالی بود از طریق بخش نظرات مطرح نمایید.
نویسنده : محمد زنگنه یوسف آبادی
منبع : تیک بار