admin管理员组

文章数量:1287803

I've added a Wordpress hook:

add_filter('post_type_link', 'custom_post_type_link_venue', 11, 3);

Everything in that hook prints twice. I've simplified it to its minimum.

function custom_post_type_link_venue ($urlsub2, $post) {
    if ($post->post_type == 'product') {
      echo "test";
    }     
}

When I fire that function, "test" is shown twice.

I've added a Wordpress hook:

add_filter('post_type_link', 'custom_post_type_link_venue', 11, 3);

Everything in that hook prints twice. I've simplified it to its minimum.

function custom_post_type_link_venue ($urlsub2, $post) {
    if ($post->post_type == 'product') {
      echo "test";
    }     
}

When I fire that function, "test" is shown twice.

Share Improve this question edited Sep 9, 2021 at 14:15 Dennis asked Sep 9, 2021 at 14:02 DennisDennis 1358 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

When you add a filter to post_type_link, you are telling WordPress to run the hooked function on the result of the get_post_permalink() function. So any time that function runs, so does yours.

When I tested the filter myself, I only saw a printed message once for each link, but if you have something like this:

if ( get_permalink() ) {
    the_permalink();
}

Then you will see a message printed twice. This is because get_permalink() calls get_post_permalink() internally, and so does the_permalink(). Your custom_post_type_link_venue() function will run each time.

Since post_type_link is a filter, not an action, it should only return a value, and not output anything. If your function does produce any output you may get unexpected results, like you are experiencing.

本文标签: phpWhy does the posttypelink hook everything twice