简体   繁体   English

get_post_meta - WordPress

[英]get_post_meta - WordPress

This code is designed to add a button to specific posts using the get_post_meta function.此代码旨在使用get_post_meta函数向特定帖子添加按钮。 How do I alter the get_post_meta function to display this button on a specific post?如何更改get_post_meta函数以在特定帖子上显示此按钮? I have already tried changing its $post->ID parameter to '1464', which is the post ID I want to use.我已经尝试将其$post->ID参数更改为“1464”,这是我要使用的帖子 ID。

function custom_listify_single_job_listing_actions_after() {
    global $post;

    $url = get_post_meta( $post->ID, 'your_custom_meta_key', true );

    echo '<a href="' . esc_url( $url ) . '" class="button">My Button</a>';
}
add_filter( 'listify_single_job_listing_actions_after', 'custom_listify_single_job_listing_actions_after' );

If you only want to run this code on a specific post, you need to add an if statement to check for that post ID.如果您只想在特定帖子上运行此代码,则需要添加if语句来检查该帖子 ID。

Your code would need to look similar to this:您的代码需要与此类似:

if($post->ID == 1464){
    $url = get_post_meta( $post->ID, 'your_custom_meta_key', true );

    echo '<a href="' . esc_url( $url ) . '" class="button">My Button</a>';
}

This simply wraps the get_post_meta() function and echo statement so that both of these only run on the post you want them to.这仅包装get_post_meta()函数和echo语句,使这两个只在你希望他们到后运行。 Any other post will ignore the code.任何其他帖子都将忽略该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM