我刚刚创建了一个名为“Movies”的帖子类型,我刚刚添加了两个以电影名称作为帖子标题的电影帖子。喜欢
空手道小子作为一个帖子的标题。
现在,我想检查页面帖子类型的内容中是否存在该自定义帖子类型标题,并将其替换为链接。我试过以下方法,但没有成功。
<?php
// createa a custom post type named Keywords
function create_post_types() {
register_post_type( \'movies_ronny\',
array(
\'labels\' => array(
\'name\' => __( \'Movies\' ),
\'singular_name\' => __( \'Movie\' )
),
\'public\' => true,
\'has_archive\' => true,
\'publicly_queryable\' => true
)
);
}
add_action( \'init\', \'create_post_types\' );
//loop throught to get the title and and check if it exists on page.
function get_post_type_title($content){
if(\'movie_ronny\' == get_post_type()){
$mv_title = get_the_title(); //get title of movie post type
}
$wp_posts = get_the_content(\'page\');// get content of pages.
$content = str_ireplace($mv_title,\'<a href="http://mylink.com">\'.$mv_title.\'</a>\',$wp_posts); //perform a search and replace.
return $content;
}
add_filter(\'the_content\',\'get_post_type_title\');
最合适的回答,由SO网友:s_ha_dum 整理而成
这是错误的(或者充其量是非常令人困惑的)。
$wp_posts = get_the_content(\'page\'); // get content of pages.
你的便条上写着“获取页面内容”,但你真正做的是
passed a alternate more link text.
get_the_content( $more_link_text, $stripteaser )
但这是一个不必要的步骤,因为您将post内容作为
$content
已经
此外,您只需设置$mv_title
如果(\'movie_ronny\' == get_post_type()){
但无论设置与否,您都在使用它。这会触发Notice
s
这应该更正确:
function get_post_type_title($content){
if(\'movie_ronny\' == get_post_type()){
$mv_title = get_the_title(); //get title of movie post type
$content = str_ireplace(
$mv_title,
\'<a href="http://mylink.com">\'.$mv_title.\'</a>\',
$content
); //perform a search and replace.
}
return $content;
}
add_filter(\'the_content\',\'get_post_type_title\');