admin管理员组

文章数量:1122832

I have this add_filter function in functions.php:

function auto_link_post_titles( $content ) {
    // This is to get all published posts from all post types.
    $args = array(
        'post_type'      => 'any', // this is to get posts from all post types.
        'post_status'    => 'publish',
        'posts_per_page' => -1, // this is to retrieve all posts
    );

    $posts = get_posts( $args );

    // Here are looping through each post and replace the title in the content with a link.
    foreach ( $posts as $post ) {
        $post_title = esc_html( $post->post_title ); // Here we are getting the post title.
        $post_link  = esc_url( get_permalink( $post->ID ) ); // Here we are getting the post link.
        
        // Create a link element
        $link = '<a href="' . $post_link . '" target="_blank">' . $post_title . '</a>';
        
        // Here we are replacing the plain text title with the link.
        $content = str_replace( $post_title, $link, $content );
    }

    return $content;
}

// We have apply the function to the content of posts.
add_filter( 'acf/load_value', 'auto_link_post_titles' );

I want to apply this only to certain post types. It's possible? And if yes, how?

Thanks.

I have this add_filter function in functions.php:

function auto_link_post_titles( $content ) {
    // This is to get all published posts from all post types.
    $args = array(
        'post_type'      => 'any', // this is to get posts from all post types.
        'post_status'    => 'publish',
        'posts_per_page' => -1, // this is to retrieve all posts
    );

    $posts = get_posts( $args );

    // Here are looping through each post and replace the title in the content with a link.
    foreach ( $posts as $post ) {
        $post_title = esc_html( $post->post_title ); // Here we are getting the post title.
        $post_link  = esc_url( get_permalink( $post->ID ) ); // Here we are getting the post link.
        
        // Create a link element
        $link = '<a href="' . $post_link . '" target="_blank">' . $post_title . '</a>';
        
        // Here we are replacing the plain text title with the link.
        $content = str_replace( $post_title, $link, $content );
    }

    return $content;
}

// We have apply the function to the content of posts.
add_filter( 'acf/load_value', 'auto_link_post_titles' );

I want to apply this only to certain post types. It's possible? And if yes, how?

Thanks.

Share Improve this question edited Sep 23, 2024 at 6:55 Mapa asked Sep 22, 2024 at 7:01 MapaMapa 194 bronze badges 2
  • Please edit your question to provide more information. Especially helpful would be seeing the auto_link_post_titles() function. – Pat J Commented Sep 22, 2024 at 22:36
  • Done, edited, I added – Mapa Commented Sep 23, 2024 at 6:55
Add a comment  | 

1 Answer 1

Reset to default 0

You're using 'post_type' => 'any' in your get_post() arguments, which will get, well, any post type. Instead, specify which post type(s) you want to retrieve.

function auto_link_post_titles( $content ) {
    // Sets the post type(s) we'll retrieve.
    $desired_post_types = array( 'my_post_type', 'my_other_post_type' );
    // This is to get all published posts from all post types.
    $args = array(
        'post_type'      => $desired_post_types,
        'post_status'    => 'publish',
        'posts_per_page' => -1, // this is to retrieve all posts
    );

    $posts = get_posts( $args );

    // Here are looping through each post and replace the title in the content with a link.
    foreach ( $posts as $post ) {
        $post_title = esc_html( $post->post_title ); // Here we are getting the post title.
        $post_link  = esc_url( get_permalink( $post->ID ) ); // Here we are getting the post link.
        
        // Create a link element
        $link = '<a href="' . $post_link . '" target="_blank">' . $post_title . '</a>';
        
        // Here we are replacing the plain text title with the link.
        $content = str_replace( $post_title, $link, $content );
    }

    return $content;
}

// We have apply the function to the content of posts.
add_filter( 'acf/load_value', 'auto_link_post_titles' );

Replace array( 'my_post_type', 'my_other_post_type' ) with the post types you want to select.

本文标签: phpAddfilter just custom page types