详情页

Z-Blog添加相关文章 带缩略图 根据文章图片数量输出不同格式

时间:2023年05月21日

编辑:佚名

昨天给博客增加了个相关文章,在此记录一下过程,给需要的人。
本文添加缩略图以及根据图片数量输出不同内容的方法同样适用于文章列表页,但是要略做修改。
按照TAG输出相关文章

点击复制代码 PHP
{foreach $article.RelatedList as $item}
    标题:<h2><a href="{$item.Url}" class="list-title"> {$item.Title}</a></h2>
    简介:<p>{$item.Intro}</p></br>
    分类:<a href="{$item.Category.Url}" target="_blank">{$item.Category.Name}</a></br>
    时间:{$item.Time('Y-m-d')}
{/foreach}
进阶
点击复制代码 PHP
{if count($article.RelatedList) > 0}
    你可能感兴趣的:
    {foreach $article.RelatedList as $item}
        标题:<h2><a href="{$item.Url}" class="list-title"> {$item.Title}</a></h2>
        简介:<p>{$item.Intro}</p></br>
        分类:<a href="{$item.Category.Url}" target="_blank">{$item.Category.Name}</a></br>
        时间:{$item.Time('Y-m-d')}
    {/foreach}
{/if}
进阶 添加缩略图
点击复制代码 PHP
{if count($article.RelatedList) > 0}
    你可能感兴趣的:
    {foreach $article.RelatedList as $item}
        $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
        $content = $item->Content;
        preg_match_all($pattern,$content,$matchContent);
        标题:<h2><a href="{$item.Url}" class="list-title"> {$item.Title}</a></h2>
        简介:<p>{$item.Intro}</p></br>
        {if isset($matchContent[1][0])}图片:<img src="{$matchContent[1][0]}" >{/if}
        分类:<a href="{$item.Category.Url}" target="_blank">{$item.Category.Name}</a></br>
        时间:{$item.Time('Y-m-d')}
    {/foreach}
{/if}
进阶 根据文章内图片数量输出不同的内容(无图,3张以下,3张以上)
点击复制代码 PHP
{if count($article.RelatedList) > 0}
        你可能感兴趣的:
    {foreach $article.RelatedList as $item}
        $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
        $content = $item->Content;
        preg_match_all($pattern,$content,$matchContent);
        {if isset($matchContent[1][0])}
            {if count($matchContent[1]) > 2}
                标题:<h2><a href="{$item.Url}" class="list-title"> {$item.Title}</a></h2>
                简介:<p>{$item.Intro}</p></br>
                图片1:<img src="{$matchContent[1][0]}" >
                图片2:<img src="{$matchContent[1][1]}" >
                图片3:<img src="{$matchContent[1][2]}" >
                分类:<a href="{$item.Category.Url}" target="_blank">{$item.Category.Name}</a></br>
                时间:{$item.Time('Y-m-d')}
            {else}
                标题:<h2><a href="{$item.Url}" class="list-title"> {$item.Title}</a></h2>
                简介:<p>{$item.Intro}</p></br>
                图片:<img src="{$matchContent[1][0]}" >
                分类:<a href="{$item.Category.Url}" target="_blank">{$item.Category.Name}</a></br>
                时间:{$item.Time('Y-m-d')}
            {/if}
        {else}
                标题:<h2><a href="{$item.Url}" class="list-title"> {$item.Title}</a></h2>
                简介:<p>{$item.Intro}</p></br>
                分类:<a href="{$item.Category.Url}" target="_blank">{$item.Category.Name}</a></br>
                时间:{$item.Time('Y-m-d')}
        {/if}
    {/foreach}
{/if}
关于文章列表调用缩略图:
点击复制代码 PHP
{php}
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$content = $article->Content;//此处不同
preg_match_all($pattern,$content,$matchContent);
{/php}
相关文章
猜你需要