使用WordPress多站點網絡,我需要從短代碼中訪問我們主站點的自定義帖子類型。switch_to_blog()在WordPress短代碼
例如,我們的主站點(ID 1
)是存儲自定義帖子類型(案例研究)的地方。在我的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爲案例研究的帖子ID。
問題是,這樣做會返回案例研究HTML的罰款,但會破壞頁面的某些功能,並且還會在主站點的博客文章中填充「最近的帖子」窗口小部件。
關於發生什麼問題的任何想法?謝謝。
你真的會在你的短代碼中'restore_current_blog()'之前調用return嗎? (這聽起來像是,閱讀你的代碼評論)。返回html應該是最後一行。 – birgire
@birgire我認爲這是OP問題的答案。你應該發佈它。 –