WordPress 元框构造中的 $post_id 或 $post->ID

问题描述 投票:0回答:2

有人可以解释为什么在创建元框时回调需要通过 $post->ID 传递帖子 id,但是使用操作钩子“save_post”,该函数可以传递 $post_id。关于何时使用哪个的一般解释将有助于解决我一直遇到的一些问题,谢谢。

例如:

function show_custom_meta_box($post) {
    $meta = get_post_meta($post->ID, 'custom_meta_class', true); 
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

    // Begin the field table and loop
    echo '<table class="form-table">';

        echo '<tr>
                <th><label for="custom-meta-class">Custom Meta Box</label></th>
                <td>
                <input class="widefat" type="text" name="custom-meta-class" id="custom-meta-class" value="'.$meta.'" size="50" />';

        echo '</td></tr>';

    echo '</table>'; // end table
}

以及 $post_id

function save_custom_meta($post) {

    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST['custom_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['custom_meta_box_nonce'], basename( __FILE__ ) ) )
        return $post_id;
    $post_type = get_post_type_object( $post->post_type );
    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;
    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST['custom-meta-class'] ) ? sanitize_html_class( $_POST['custom-meta-class'] ) : '' );
    /* Get the meta key. */
    $meta_key = 'custom_meta_class';
    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );

    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );
}
wordpress meta
2个回答
0
投票

我没有答案,但我有完全相同的问题。我很惊讶在过去 7 年里没有人插话。

这是我的代码,它可以工作(在functions.php中)

add_action('save_post','extract_citations',100,1);
function extract_citations($post_id){
$the_post = get_post($post_id);
$content = $the_post->post_content;
preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i',$content,$citation_array);
$citation_urls = $citation_array[1];
update_post_meta($post_id,'citations',serialize($citation_urls));
}

这里的代码不起作用,尽管理论上是正确的:

add_action('save_post','extract_citations',100,1);
function extract_citations($post){
$the_post = get_post($post->ID);
$content = $the_post->post_content;
preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i',$content,$citation_array);
$citation_urls = $citation_array[1];
update_post_meta($post->ID,'citations',serialize($citation_urls));
}

0
投票

我知道这是一个非常古老的线程,但我想添加我的想法。
假设帖子 ID 是 1724

在第一个示例中,您将 1724 直接插入到函数中,因此第 3 行将变为

$the_post = get_post(1724);

在第二个示例中,我们要求提供邮政号码的 ID,因此第 3 行将变为

$the_post = get_post(1703->ID);

要记住的是,当您在站点内调用该函数时,会添加数字,该数字将成为 $post_id,实际上您在函数括号中放入什么单词并不重要。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.