Bento WordPress 嵌入
<head>
<meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <script type="module" async src="https://cdn.ampproject.org/bento.mjs" ></script> <script nomodule src="https://cdn.ampproject.org/bento.js"></script> <script type="module" async src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.mjs" ></script> <script nomodule async src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.js" ></script> <link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.css" /> <style></style>
</head>
<body>
<bento-wordpress-embed style="width: 300px; height: 400px" data-url="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/" ></bento-wordpress-embed>
</body>
显示 WordPress 帖子或页面的摘要的 iframe。
Web 组件
您必须包含每个 Bento 组件所需的 CSS 库,以保证正确加载,并在添加自定义样式之前。或使用内联提供的轻量级预升级样式。请参阅布局和样式。
通过 npm 导入
npm install @bentoproject/wordpress-embed
import {defineElement as defineBentoWordpressEmbed} from '@bentoproject/wordpress-embed';
defineBentoWordpressEmbed();
通过 <script>
包含
<script type="module" src="https://cdn.ampproject.org/bento.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/bento.js" crossorigin="anonymous"></script>
<script type="module" src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.mjs" crossorigin="anonymous"></script>
<script nomodule src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.css" crossorigin="anonymous">
示例
<head>
<script type="module" async src="https://cdn.ampproject.org/bento.mjs" ></script> <script nomodule src="https://cdn.ampproject.org/bento.js"></script> <script type="module" async src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.mjs" ></script> <script nomodule async src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.js" ></script> <link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.css" />
</head>
<body>
<bento-wordpress-embed id="my-embed" style="width: 300px; height: 400px" data-url="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/" ></bento-wordpress-embed>
</body>
布局和样式
每个 Bento 组件都有一个小型 CSS 库,您必须包含该库以保证正确加载,且不会出现内容偏移。由于基于顺序的特异性,您必须手动确保在任何自定义样式之前包含样式表。
<link
rel="stylesheet"
type="text/css"
href="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.css"
/>
或者,您还可以使轻量级预升级样式内联可用
<style>
bento-wordpress-embed {
display: block;
overflow: hidden;
position: relative;
}
</style>
容器类型
bento-wordpress-embed
组件具有定义的布局大小类型。为了确保组件正确渲染,请务必通过所需的 CSS 布局(例如使用 height
、width
、aspect-ratio
或其他此类属性定义的布局)将大小应用于组件及其直接子级(幻灯片)。
bento-wordpress-embed {
height: 100px;
width: 100%;
}
属性
data-url(必需)
要嵌入的帖子的 URL。以编程方式更改属性值将自动更新嵌入式内容。
<head>
<script type="module" async src="https://cdn.ampproject.org/bento.mjs" ></script> <script nomodule src="https://cdn.ampproject.org/bento.js"></script> <script type="module" async src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.mjs" ></script> <script nomodule async src="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.js" ></script> <link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-wordpress-embed-1.0.css" />
</head>
<body>
<bento-wordpress-embed id="my-embed" style="width: 300px; height: 400px" data-url="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/" ></bento-wordpress-embed> <div class="buttons" style="margin-top: 8px"> <button id="switch-button">Switch embed</button> </div> <script> (async () => { const embed = document.querySelector('#my-embed'); await customElements.whenDefined('bento-wordpress-embed'); // set up button actions document.querySelector('#switch-button').onclick = () => embed.setAttribute( 'data-url', 'https://make.wordpress.org/core/2021/09/09/core-editor-improvement-cascading-impact-of-improvements-to-featured-images/' ); })(); </script>
</body>
Preact/React 组件
通过 npm 导入
npm install @bentoproject/wordpress-embed
import React from 'react';
import {BentoWordPressEmbed} from '@bentoproject/wordpress-embed/react';
function App() {
return (
<BentoWordPressEmbed url="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/"></BentoWordPressEmbed>
);
}
布局和样式
容器类型
BentoWordPressEmbed
组件具有定义的布局大小类型。为了确保组件正确渲染,请务必通过所需的 CSS 布局(例如使用 height
、width
、aspect-ratio
或其他此类属性定义的布局)将大小应用于组件及其直接子级(幻灯片)。这些可以内联应用
<BentoWordPressEmbed
style={{width: '100%', height: '100px'}}
url="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/"
></BentoWordPressEmbed>
或通过 className
<BentoWordPressEmbed
className="custom-styles"
url="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/"
></BentoWordPressEmbed>
.custom-styles {
height: 100px;
width: 100%;
}
道具
url(必需)
要嵌入的帖子的 URL。