admin管理员组

文章数量:1122846

I have the following code:

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

I would like to ask for help with two problems:

  1. I would like to exclude this function from certain pages. It is possible to add a piece of code that excludes that add_filter for custom pages?

  2. I would like to exclude this function from certain ACF fields. It is possible to add a piece of code that excludes that add_filter for custom ACF fields?

Thanks!

I have the following code:

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

I would like to ask for help with two problems:

  1. I would like to exclude this function from certain pages. It is possible to add a piece of code that excludes that add_filter for custom pages?

  2. I would like to exclude this function from certain ACF fields. It is possible to add a piece of code that excludes that add_filter for custom ACF fields?

Thanks!

Share Improve this question asked Sep 23, 2024 at 14:48 MapaMapa 194 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yes for both 1 and 2.

The filter acf/load_value can accept 3 parameters: $value, $post_id, $field.

$value is the default value of the field, here you call it $content.

$post_id is the post ID for this value. If you want to exclude some pages, you can use this.

$field is all the information of the field, including name and key. You can exclude some fields using name or key.

Let me write all the the conditions together in the following function:

function auto_link_post_titles( $content, $post_id, $field ) {
    $excluded_ids = array(); // Put the IDs of the pages you want to exclude here
    $excluded_field_names = array(); // Put the names of the fields you want to exclude here
    
    if(!in_array($post_id, $excluded_ids) && !in_array($field['name'], $excluded_field_names)) {
        // 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.
// Note the number of parameters is set to 3 here
add_filter( 'acf/load_value', 'auto_link_post_titles', 10, 3 );

本文标签: phpExclude function for custom pagesexclude custom ACF fields