在为Tumblr创建自定义主题时区分博客标题和帖子标题?

时间:2012-08-20 21:09:04

标签: tumblr

长时间潜伏,第一次问问。

我目前正在为Tumblr博客编写一个自定义主题,以便在每个帖子后嵌入一个小部件,无论其类型如何。这个小部件需要帖子的标题,如果没有,则需要博客的标题。

根据Tumblr,{Title}指的是博客标题。但是,如果我们有文章帖子或聊天帖子,{Title}会引用帖子标题。

这是我的代码:

var title;
if ('{PostType}' === 'text' || '{PostType}' === 'chat') 
    title = '{Title}';
else if ('{PostType}' === 'photo' || '{PostType}' === 'photoset' || '{PostType}' === 'audio' || '{PostType}' === 'video')
    title = '{PlaintextCaption}';
else if ('{PostType}' === 'quote')
    title = '{PlaintextQuote}';
else if ('{PostType}' === 'link')
    title = '{PlaintextName}';
else if ('{PostType}' === 'answer')
    title = '{PlaintextQuestion}';

if (title === '')
    title = '{Title}';

如果我有一个没有标题的照片帖子,那么标题将被正确设置为博客标题。但如果我有一个没有标题的文章帖子,那么标题将设置为[空字符串]而不是博客标题。

所以我的问题是:当我在文字或聊天帖子里面时,如何获得博客标题?

1 个答案:

答案 0 :(得分:2)

您可以做的是在进入{block:Posts}之前将博客标题设置为变量。因此,在<head>中,您可以执行此操作:

var blogTitle = '{Title}';

然后像这样修改你的代码:

var title;
if ('{PostType}' === 'text' || '{PostType}' === 'chat') 
    title = blogTitle;
else if ('{PostType}' === 'photo' || '{PostType}' === 'photoset' || '{PostType}' === 'audio' || '{PostType}' === 'video')
    title = '{PlaintextCaption}';
else if ('{PostType}' === 'quote')
    title = '{PlaintextQuote}';
else if ('{PostType}' === 'link')
    title = '{PlaintextName}';
else if ('{PostType}' === 'answer')
    title = '{PlaintextQuestion}';

if (title === '')
    title = '{Title}';

另外,请注意,因为{PostType}永远不会== 'photoset'。 Photoset帖子类型总是以“照片”的形式出现。在制作类名时我通常采取的措施是:

class="type-{PostType}{block:Photoset}set{/block:Photoset}

...输出将为class="type-photoset"

相关问题