0

What I want is to display the value of the custom field called 'page_template'. I use the code below but it displays nothing under Page Template column although I have already defined a value for the page_template custom field of the page

add_filter('manage_pages_columns', 'column_page');
function column_page($defaults) {
    $defaults['column_template'] = 'Page Template';
    return $defaults;
}


function page_template_field() {
    $custom_field_id = get_post_meta($post_ID);
    if ($custom_field_id) {
        $custom_field_value = get_post_meta($custom_field_id,'page_template');
        return $custom_field_value;
    }
}  

add_action('manage_pages_custom_column','column_page_content',10,2);
function column_page_content($column_name, $post_ID) {
    if ($column_name == 'column_template') {
        $column_template_list = page_template_field($post_ID);    
        if ($column_template_list) {
            echo '<p> '. $column_template_list .' </p>';
        }
    }
}
1

The main errors where the missing of the $post_ID parameter in page_template_field and using $custom_field_id instead of $post_ID.

Anyway, this works and is simpler:

add_filter('manage_pages_columns', 'column_page');
function column_page($defaults) {
    $defaults['column_template'] = 'Page Template';
    return $defaults;
}

add_action('manage_pages_custom_column','column_page_template',10,2);
function column_page_template($column_name, $post_ID) {
    if ($column_name == 'column_template') {
        $custom_field_values = get_post_meta($post_ID,'page_template');
        if (!empty($custom_field_values)) {
            echo '<p> '. join(', ',$custom_field_values) .' </p>';
        }
    }
}
0

I think your function declaration should have an argument:

function page_template_field($post_ID) {           //Here $post_ID is the argument you are passing
        $custom_field_id = get_post_meta($post_ID);
        if ($custom_field_id) {
            $custom_field_value = get_post_meta($custom_field_id,'page_template');
            return $custom_field_value;
        }
}

With out the argument page_template_field wouldn't know what $post_ID is!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.