在 Blogger 中将博客 Post 标题或标签存储为字符串

Storing the Blog Post Title Or Lables as a String in Blogger

我只需要一个代码来获取 post 的标题或标签,将其存储为字符串变量形式。

我试过类似下面的方法但没有用:

<script>
function myFunction() {
   var getTitle=data:post.title;
   getTitle=getTitle.substring(0, getTitle.lastIndexOf(" Chapter"));
}
</script>

我使用 w3schools 的 TryEditor 执行了这段代码:

<!DOCTYPE html>
<html>
<body>
<p>Click the button to locate the last occurance of a specified value.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var getTitle="Comic Title! Chapter 1";
getTitle=getTitle.substring(0, getTitle.lastIndexOf(" Chapter"));
document.getElementById("demo").innerHTML = getTitle;
}
</script>
</body>
</html>

我成功地只获得了我想要的部分,并将其存储在 getTitle 中,如下所示:
Chuuko demo Koi ga Shitai!

当我将上面的相同代码放入 Blogger HTML 小工具并按原样工作时,这甚至可以正常工作。

撇开这个不谈,我想要的不是下面突出显示的部分,

var getTitle="Comic Title! Chapter 1";

我试过了,

var getTitle=data:post.title;

但是没有成功,我应该怎么做才能获取标题并将其存储在getTitle变量中

试试这个

var getTitle = "<data:post.title>";

或者

var getTitle = &quot;<data:post.title>&quot;;

但是您只能在博客 post、热门 post 和特色 post 小部件中获得 data:post.title,因此它不适用于 HTML 小部件。

尝试使用以下数据标记 -

var getTitle = "<data:view.title/>";

这将 return 正在查看的页面的标题(不同的页面会有所不同。有关详细信息,请参阅此 - http://template-data.blogspot.com/2016/04/title_58.html

title

Title of the current view.

The title of the view is different for different views. For item pages, it will be the post's title. For feed pages, it will be the blog's title.

title has data type string

Strings are quote-wrapped sequences of characters.

Metadata

Metadata are implicit properties belonging to a data type. If your data has the name 'foo', you reference it with 'data:foo'. If the metadata is 'bar', you can reference the value of foo's metadata bar with 'data:foo.bar'.

length: Provides the number of characters in the string.

size: Same as length.

escaped: Provides the HTML escaped equivalent of the string. This is useful when the string may represent user input, to avoid HTML injection attacks

jsEscaped: Provides the JS escaped equivalent of the string. This is useful when the string may represent user input, to avoid JS errors.

jsonEscaped: Provides the JSON escaped equivalent of the string. This is useful when the string may represent user input, to avoid JSON errors.

cssEscaped: Provides the CSS escaped equivalent of the string.

使用data:view.title标签的好处是可以在主题代码内的任何地方使用,不会出现未定义命名空间的问题。也就是说,它不能直接从小部件内容(又名 HTML/JavaScript、文本等)

中引用