問題描述
嘗試在我的 functions.php 中的帖子內容之前插入內容 – 我知道如何使用常規 wp 鈎子,但不確定如何插入到其他區域。
試過這個,但是它會殺死任何其他帖子類型的內容:
function property_slideshow( $content ) {
if ( is_single() && 'property' == get_post_type() ) {
$custom_content = '[portfolio_slideshow]';
$custom_content .= $content;
return $custom_content;
}
}
add_filter( 'the_content', 'property_slideshow' );
如何使這個條件?
最佳解決方案
只需使用 the_content 過濾器,例如:
<?php
function theme_slug_filter_the_content( $content ) {
$custom_content = 'YOUR CONTENT GOES HERE';
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'theme_slug_filter_the_content' );
?>
基本上,您在自定義內容之後附加帖子內容,然後返回結果。
Edit
正如 Franky @ bueltge 在他的評論中指出的,過程與帖子標題一樣; 只需向 the_title 鈎子添加一個過濾器:
<?php
function theme_slug_filter_the_title( $title ) {
$custom_title = 'YOUR CONTENT GOES HERE';
$title .= $custom_title;
return $title;
}
add_filter( 'the_title', 'theme_slug_filter_the_title' );
?>
請注意,在這種情況下,您會在標題後添加自定義內容。 (沒有關係,我剛剛跟你在你的問題中指定了什麼)
編輯 2
您的示例代碼不工作的原因是因為您在符合條件時才返回 $content 。您需要將未修改的 $content 作為 else 返回到您的條件。例如。:
function property_slideshow( $content ) {
if ( is_single() && 'property' == get_post_type() ) {
$custom_content = '[portfolio_slideshow]';
$custom_content .= $content;
return $custom_content;
} else {
return $content;
}
}
add_filter( 'the_content', 'property_slideshow' );
這樣,對於不是’property’ post-type 的帖子,返回 $content,un-modified 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。