我想将一个页面(名为“contact\\u text”)的内容包含到特定位置的另一个页面(这两个页面都是WordPress管理页面)。我跟踪了this Stack Overflow thread.
然而,我不相信我可以在WP页面内调用PHP函数,更不用说在我希望它出现的地方了。我是否需要为该函数设置一个快捷码才能使用它?
我想将一个页面(名为“contact\\u text”)的内容包含到特定位置的另一个页面(这两个页面都是WordPress管理页面)。我跟踪了this Stack Overflow thread.
然而,我不相信我可以在WP页面内调用PHP函数,更不用说在我希望它出现的地方了。我是否需要为该函数设置一个快捷码才能使用它?
另一个短代码选项(将以下内容添加到functions.php中)。有了这个,你有两个选择。您可以通过帖子/页面ID(适用于任何帖子类型)或页面名称/slug(仅适用于页面)进行查询。
// Add "Show Content" Shortcode
function wpse_120886_show_content( $atts ) {
// Attributes
$a = shortcode_atts(
array(
\'id\' => null,
\'page_name\' => null,
), $atts )
);
// Get Content & Return
if ( $a[\'id\'] ) {
$the_post = get_post( $a[\'id\'] );
$the_content = $the_post->post_content;
} elseif ( $a[\'page_name\'] ) {
$the_post = get_posts( array( \'name\' => $a[\'page_name\'], \'post_type\' => \'page\', \'post_status\' => \'publish\', \'posts_per_page\' => 1 ) );
$the_content = $the_post[0]->post_content;
}
return $the_content;
}
add_shortcode( \'show_content\', \'wpse_120886_show_content\' );
Usage: 按ID:[show_content id="ID_HERE"]
按名称/段塞:[show_content page_name="contact_text"]
如果您愿意,您可以完全使用该代码并将其应用到短代码中。
add_shortcode( \'show_post\', \'show_post_shortcode\' );
function show_posts_shortcode( $atts ) {
$defaults = array(
\'path\' => null
);
$attributes = shortcode_atts( $defaults, $atts );
if ( null === $attributes[\'path\'] )
return;
ob_start();
show_post( $attributes[\'path\'] );
$contents = ob_get_contents();
@ob_end_clean();
return $contents;
}
然后你只需键入[show_post path="about"]
获取关于页面内容。这很奇怪。突然,我无法创建更多的Pods页面!在编辑页面屏幕中,主按钮显示submit for review,而不是publish,当我按下该按钮时,WP会说:不允许编辑此帖子。嗯,我是管理员,所以我不应该有任何问题,事实上,我以前创建过其他页面。角色和能力对我来说似乎没问题。。。发生什么事了?编辑:我的函数上次升级时似乎出现问题。php代码。但我弄不到什么!