Home > Article > Backend Development > Innovative skills in Typecho: application cases of PHP programming
Innovative skills in Typecho: Application cases of PHP programming
Typecho is a simple and efficient open source blog system. It is loved by the majority of users for its flexibility and scalability. As a developer, we can not only use Typecho's existing functions to build a blog, but also add custom functions to Typecho through PHP programming to meet more personalized needs. This article will introduce some innovative techniques and application cases to help readers better develop Typecho themes.
In Typecho, we can extend its functionality through custom functions. Here is a simple example that shows how to create a custom function to get the number of views of an article:
function getPostViews($archive) { $cid = $archive->cid; $db = Typecho_Db::get(); $prefix = $db->getPrefix(); if (!array_key_exists('views', $db->fetchAll($db->select()->from('table.contents')))) { $db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;'); echo 0; return; } $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid)); if ($archive->is('single')) { $views = Typecho_Cookie::get('extend_contents_views'); if (empty($views)) { $views = array(); } else { $views = explode(',', $views); } if (!in_array($cid, $views)) { $db->query($db->update('table.contents')->rows(array('views' => (int)$row['views'] + 1))->where('cid = ?', $cid)); array_push($views, $cid); $views = implode(',', $views); Typecho_Cookie::set('extend_contents_views', $views); } } echo $row['views']; }
Shortcode is a common function to insert specific content into the article. Here is an example showing how to add a custom shortcode for Typecho to insert into the audio player:
function audioPlayer($atts,$content=null, $code="") { extract(shortcode_atts(array( 'url' => null, ), $atts)); if(!$url) return ''; $html = '<audio src="'.$url.'" controls="controls" autoplay="autoplay">'.esc_html($content).'</audio>'; return $html; } add_shortcode('audio', 'audioPlayer');
Use shortcode in article[audio url="audio.mp3"]
Just Audio player can be plugged in.
Typecho provides some page templates by default, but in some cases we may need to create custom page templates according to specific needs. The following is an example that shows how to create a custom page template to display a list of articles under a certain category:
<?php /** * Template Name: 分类页面模板 * * 用于显示某个分类下的文章列表 */ if (!defined('__TYPECHO_ROOT_DIR__')) exit; $this->need('header.php'); ?> <div class="content"> <h1 class="archive-title"><?php $this->archiveTitle(array( 'category' => _t('分类 %s 下的文章'), ), '', ''); ?></h1> <?php while($this->next()): ?> <article class="post"> <h2 class="post-title"><a href="<?php $this->permalink() ?>"><?php $this->title() ?></a></h2> <p class="post-meta"> <?php _e('发布时间:'); ?> <time datetime="<?php $this->date('c'); ?>"><?php $this->date(); ?></time> </p> </article> <?php endwhile; ?> </div> <?php $this->need('footer.php'); ?>
After adding the custom page template, we can create a page in the Typecho background and select the Template can be used to display a list of articles under a specific category.
This article introduces some innovative techniques and application cases of Typecho. Through custom functions, custom short codes and custom page templates, we can add more functions and personalized needs to Typecho. I hope this article will be helpful to readers in PHP programming and inspire more creativity and innovative thinking.
The above is the detailed content of Innovative skills in Typecho: application cases of PHP programming. For more information, please follow other related articles on the PHP Chinese website!