只需发布到 GitHub Gist 的链接,它就会被很好地嵌入。或者使用此代码片段提供的格式自己创建短代码。 适用于您的 functions.php 文件
<?php
// [gist id="ID" file="FILE"]
function gist_shortcode($atts) {
return sprintf(
'<script src="https://gist.github.com/%s.js%s"></script>',
$atts['id'],
$atts['file'] ? '?file=' . $atts['file'] : ''
);
} add_shortcode('gist','gist_shortcode');
// Remove this function if you don't want autoreplace gist links to shortcodes
function gist_shortcode_filter($content) {
return preg_replace('/https:\/\/gist.github.com\/([\d]+)[\.js\?]*[\#]*file[=|_]+([\w\.]+)(?![^<]*<\/a>)/i', '[gist id="${1}" file="${2}"]', $content );
} add_filter( 'the_content', 'gist_shortcode_filter', 9);
?>
以下任何格式都可以
- https://gist.github.com/1147076
- https://gist.github.com/1147076#file_annotated.js
- https://gist.github.com/1147076.js?file=annotated.js
- [gist id=1147076]
- [gist id=1147076 file=annotated.js]
它是 PHP。那飞行的 JS 呢?
我喜欢这种方法。我更喜欢用 php 做任何内容,并使用 js 来“使其变得花哨和酷炫”。
我在 WordPress 的 functions.php 中使用了它,但它不起作用。