• Resolved Maiskolben

    (@maiskolben)


    Hello,

    If there’re keys (or a single key is) added from the custom-fields my dynamic sidebar should hide. It should only appear when there’s no custom-fields used in posts. But there’s something wrong with my if/else block, I think.

    First I couldn’t found a template tag which allowes me to proof if there’s a custom-field-key is set. get_post_custom_keys($post->ID) returns an array, so far. Converted to string I thought I can handle it:

    <?php $keys = implode("", array_splice(get_post_custom_keys($post->ID), 0, -2));
     if ($keys == '') :
      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(2) ) : endif;
     endif; ?>

    It is functioning as long as the keys from custom-fields is more then three. But if I have only one custom field added to my posts – this won’t work…

    Do anyone know a code-snippet or a template tag for getting this runnin’? I cannot figured it out…

    thx, maiskolben

Viewing 4 replies - 1 through 4 (of 4 total)
  • How about:

    <?php if ( count( get_post_custom_keys($post->ID) )  >= 1 ):
    if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(2) ) :
    endif;
    endif; ?>
    Thread Starter Maiskolben

    (@maiskolben)

    Hey esmi,

    this isn’t work for me. get_post_custom_keys($post->ID) gives (in the german version of wp) back when custom fields comes with a post:

    Array (
    [0] => customfield1
    [1] => customfield2
    [2] => _edit_lock
    [3] => _edit_last
    [4] => _mini_post
    [5] => customfield3
    [6] => customfield4
    )

    Even if there no customfield(s), key 2-4 appear. And sometimes (in some articles) _mini_post is missing (for whatever it stands for), and the indexes of the array output in unordered way. This is the reason why the code from my first post isn’t functioning. What is this for a _mini_post? ^^

    Hope anybody can help me with this…

    Maiskolben

    There an if-else example on https://codex.www.ads-software.com/Function_Reference/get_post_custom_keys that strips out the WP internally maintained custom keys.

    Thread Starter Maiskolben

    (@maiskolben)

    Thx esmi! This is exactly what I’m searching for!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get_post_custom_keys($post->ID);’ is closed to new replies.