0

Basically I have 2 custom columns in the 'edit.php' of my custom post type. What I need is when $item_type is set to 'page' but there is no page selected, 'No selection' is echoed (same for when 'media' is set but there is no media file selected). What happens is it's showing the title of the post instead of the proper echo. It works fine for when the page or media file variable is set. Just when it's not set. My code is below, thank you in advance to any help offered.

function my_show_columns($name) {
global $post;
$hidden_page = get_the_title(get_post_meta($post->ID, 'hidden_page', true));

$hidden_file =  get_the_title(get_post_meta($post->ID, 'media_file', true));

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

    switch ($name) {
        case 'hidden_item_type':
            if (empty(get_post_meta($post->ID, 'item_type', true))) {
                echo 'Please select type of item to hide';
            } else {
                echo $item_type;
            }
            break;
        case 'hidden_item_info':
            if(get_post_meta($post->ID, 'item_type', true) == 'page') {
                echo 'Title: ' . $hidden_page;
            } elseif(get_post_meta($post->ID, 'item_type', true) == 'media') {
                echo 'Title: ' . $hidden_file;
            } elseif ( empty(get_post_meta($post->ID, 'hidden_page', true)) ) {
                echo 'No selection';
            } elseif ( empty(get_post_meta($post->ID, 'media_file', true)) ) {
                echo 'No selection';
            }
            break;
    }
}

I'm hooking on at:

        add_action('manage_posts_custom_column',  array($this, 'my_show_columns'));

1 Answer 1

0

Hey so I was able to figure out something that worked. I don't know if this is the best way but it worked for me. Please post an answer if you have a better idea.

Basically I had to change the order in which the statements were in. The final code looked like this:

function my_show_columns($name) {
global $post;
$hidden_page = get_the_title(get_post_meta($post->ID, 'hidden_page', true));

$hidden_file =  get_the_title(get_post_meta($post->ID, 'media_file', true));

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

    switch ($name) {
        case 'hidden_item_type':
            if (empty(get_post_meta($post->ID, 'item_type', true))) {
                echo 'Please select type of item to hide';
            } else {
                echo $item_type;
            }
            break;
        case 'hidden_item_info':
            if ( (get_post_meta($post->ID, 'item_type', true) == 'page') && (empty(get_post_meta($post->ID, 'hidden_page', true))) ) {
                echo 'No selection';
            } elseif ( (get_post_meta($post->ID, 'item_type', true) == 'media') && (empty(get_post_meta($post->ID, 'media_file', true))) ) {
                echo 'No selection';
            } elseif (get_post_meta($post->ID, 'item_type', true) == 'page') {
                echo 'Title: ' . $hidden_page;
            } elseif(get_post_meta($post->ID, 'item_type', true) == 'media') {
                echo 'Title: ' . $hidden_file;
            }
            break;
    }
}

I have no clue as to why this worked and the other way didn't. I'm guessing it's the order in which they're evaluated. Either way, here it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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