1

I just created a post type called 'Movies', I just added a couple of movie posts using the name of the movie as title of the post. Like

Karate Kid as a title of one post.

Now i wanted to check if that custom post type title exists in page post type's content anywhere and replace it with a link. I have tried the following, but in vain.

<?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');

1 Answer 1

2

This is wrong (or at best very confusing).

$wp_posts = get_the_content('page'); // get content of pages.

Your note says "get content of pages" but what you've really done is passed a alternate more link text.

get_the_content( $more_link_text, $stripteaser )

But that is an unnecessary step as you have the post content passed into the callback as $content already.

Additionally, you are only setting $mv_title if ('movie_ronny' == get_post_type()){ but you are using it whether it is set or not. That is going to trigger Notices.

This should be more correct:

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');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.