Php 在WordPress短代码中切换到blog()
使用WordPress多站点网络,我需要在一个短代码内从我们的主站点访问自定义帖子类型 例如,我们的主站点(Php 在WordPress短代码中切换到blog(),php,wordpress,custom-post-type,shortcode,multisite,Php,Wordpress,Custom Post Type,Shortcode,Multisite,使用WordPress多站点网络,我需要在一个短代码内从我们的主站点访问自定义帖子类型 例如,我们的主站点(ID1)就是存储自定义帖子类型(案例研究)的地方。在functions.php中,我有以下内容: //[casestudy] add_shortcode( 'casestudy', 'casestudy_shortcode' ); function casestudy_shortcode( $atts ) { $a = shortcode_atts( array(
ID1
)就是存储自定义帖子类型(案例研究)的地方。在functions.php中,我有以下内容:
//[casestudy]
add_shortcode( 'casestudy', 'casestudy_shortcode' );
function casestudy_shortcode( $atts ) {
$a = shortcode_atts( array(
'id' => ''
), $atts );
switch_to_blog(1);
//Get fields from custom post type with Advanced Custom Fields Pro
//and return HTML output with them
restore_current_blog();
}
然后用[casestudy id=“123”]
调用短代码,其中id是案例研究的post id
问题是,这样做会很好地返回案例研究HTML,但会破坏页面的某些功能,还会用主站点的博客帖子填充“最近的帖子”小部件
有什么问题吗?谢谢。添加我的评论作为回答:
阅读操作码注释时,似乎从未调用过restore\u current\u blog()
,因为在调用之前会返回HTML
请确保
return
部分是其中的最后一行。在快捷码中的restore\u current\u blog()
之前是否确实调用return?(读代码注释时,听起来像是这样)。返回html应该是最后一行。@birgire我想这就是OP问题的答案。你应该把它贴出来。非常感谢,我没注意到!我切换到博客只是为了获取变量。