typecho上一篇下一篇内获取图片封面实例

常规来说:主要记录上一篇/下一篇的代码

/**
* 显示下一篇
*
* @access public
* @param string $default 如果没有下一篇,显示的默认文字
* @return void
*/
function theNext($widget, $default = NULL)
{
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created > ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1);
$content = $db->fetchRow($sql);
 
if ($content) {
$content = $widget->filter($content);
$link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">下一篇</a>';
echo $link;
} else {
echo $default;
}
} 
/**
* 显示上一篇
*
* @access public
* @param string $default 如果没有下一篇,显示的默认文字
* @return void
*/
function thePrev($widget, $default = NULL)
{
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created < ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_DESC)
->limit(1);
$content = $db->fetchRow($sql); 
if ($content) {
$content = $widget->filter($content);
$link = '<a href="' . $content['permalink'] . '" title="' . $content['title'] . '">上一篇</a>';
echo $link;
} else {
echo $default;
}
}

将以上代码写入functions.php

调用代码如下:

<?php thePrev($this); ?> 和 <?php theNext($this); ?>

typecho上一篇下一篇内获取图片封面实例,直接上代码参考把

/**
* 显示下一篇
*
* @access public
* @param string $default 如果没有下一篇,显示的默认文字
* @return void
*/
function theNext($widget, $default = NULL)
{
$db = Typecho_Db::get();
$sql = $db->select()->from('table.contents')
->where('table.contents.created > ?', $widget->created)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', $widget->type)
->where('table.contents.password IS NULL')
->order('table.contents.created', Typecho_Db::SORT_ASC)
->limit(1);
$content = $db->fetchRow($sql);

if ($content) {

$img =  $db->fetchAll($db->select()->from('table.fields')->where('name = ? AND cid = ?','img',$result['cid']));
                if(count($img) !=0){
                    //var_dump($img);
                    $img=$img['0']['str_value'];                        
                    if($img){}
                    else{
                     $img="/usr/themes/spimes/images/thumbs/other_thumbnail.png";
                    }                                                 
                }
                                
                // var_dump($img);
                // if($img == ""){
                //     $img = "wu";
                // }

$content = $widget->filter($content);
$link = '<div class="entry-page-next j-lazy" style="background-image: url(' . $img . ')"><a href="' . $content['permalink'] . '"><span>' . $content['title'] . '</span> </a><div class="entry-page-info"> <span class="pull-right">下一篇  »</span></div></div>';
echo $link;
} else {
echo $default;
}
}

如果想获取时间的话,这里就需要注意

评论时间就是$content['created'],这是unix时间戳,转换成人类看得懂的时间

date('Y-m-d H:i:s', $content['created_at'])
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/76
0 评论
3.7k

发表评论

!