使用类别URL自动添加自定义字段

时间:2013-08-05 14:25:38

标签: php wordpress

我找到了一些代码,可以自动为每个新帖子添加自定义字段:

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'cat', '' . get_category_link( $category->term_id ) . '', true);
    }
}

我已将此代码放入我的functions.php文件中,并且它中途运行。它会自动添加一个新的自定义字段,但如何回显作为posts类别slug的值?

由于自定义表单只会在发布后添加,我们已经为它选择了一个类别。那么我们可以回应这个吗?

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
        add_post_meta($post_ID, 'cat', 'CATEGORY LINK HERE', true);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要帖子的ID:

echo get_post_meta($post_ID, 'cat', true);

我很好奇,你想在哪里回应这个?

相关问题