您基本上需要在当前主题之外创建一个区域,在其中创建一些可以粘贴到iframe中的内容。
您只需向用户提供如下内容:
<iframe src="http://yoursite.com/iframe/"></iframe>
他们的网站上有你的帖子!
第一步是为iframe创建URL端点。首先,您需要为iframe添加重写规则,然后过滤查询变量,以确保WordPress能够识别新的iframe查询变量,而不会将其删除。
<?php
add_action( \'init\', \'wpse32725_add_rewrite\' );
/**
* Adds the rewrite rule for our iframe
*
* @uses add_rewrite_rule
*/
function wpse32725_add_rewrite()
{
add_rewrite_rule(
\'^iframe$\',
\'index.php?iframe=true\',
\'top\'
);
}
add_filter( \'query_vars\', \'wpse32725_filter_vars\' );
/**
* adds our iframe query variable so WP knows what it is and doesn\'t
* just strip it out
*/
function wpse32725_filter_vars( $vars )
{
$vars[] = \'iframe\';
return $vars;
}
接下来,只要存在iframe查询变量,就钩住template\\u重定向和“catch”。如果是,你可以做任何你想做的事。获取帖子列表并显示它们。
<?php
add_action( \'template_redirect\', \'wpse32725_catch_iframe\' );
/**
* Catches our iframe query variable. If it\'s there, we\'ll stop the
* rest of WP from loading and do our thing. If not, everything will
* continue on its merry way.
*
* @uses get_query_var
* @uses get_posts
*/
function wpse32725_catch_iframe()
{
// no iframe? bail
if( ! get_query_var( \'iframe\' ) ) return;
// Here we can do whatever need to do to display our iframe.
// this is a quick example, but maybe a better idea would be to include
// a file that contains your template for this?
$posts = get_posts( array( \'numberposts\' => 5 ) );
?>
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
<?php /* stylesheets and such here */ ?>
</head>
<body>
<ul>
<?php foreach( $posts as $p ): ?>
<li>
<a href="<?php echo esc_url( get_permalink( $p ) ); ?>"><?php echo esc_html( $p->post_title ); ?></a>
</li>
<?php endforeach; ?>
<ul>
</body>
</html>
<?php
// finally, call exit(); and stop wp from finishing (eg. loading the
// templates
exit();
}
剩下的就是为用户创建一些地方来获取iframe代码。您可以使用一个短代码来实现这一点,也可以创建一个函数(如下面的函数)将主题粘贴到某个地方。
<?php
function wpse32725_iframe_code()
{
return sprintf(
\'<code><iframe src="%s"></iframe></code>\',
esc_url( home_url(\'/iframe/\') )
);
}
这些都是
as a plugin.