php sorting array(s)

jaredjared College Station, TX Icrontian
edited September 2009 in Internet & Media
Alright, I'm trying to do some hackery under the hood in a Wordpress site I'm working on.

Right now I have this array

[PHP]
$post_custom_fields =
array(
"news_thumbnail" => array(
"name" => "news_thumbnail",
"std" => "",
"title" => "NEWS Thumbnail Image:",
"description" => "Image path eg: /wp-content/uploads/texting.jpg<br />If the image is not 250x190 it will be resized.",
"cat"=> "news"
),
"news_pollid" => array(
"name" => "news_pollid",
"std" => "",
"title" => "NEWS Poll ID:",
"description" => "eg: 1522545",
"cat"=> "news"
)
);
[/PHP]

Right now I'm using a foreach with $post_custom_fields to just list out each entry. This works fine.

However what I want to do, is list out each one and group them by cat.

Example:
NEWS
   list arrays with cat=> news here
OTHER
   list arrays with cat=> other here


I am whipping up a custom write panel for wordpress. It actually is pretty easy, but there are going to be several extra fields (wp custom fields). I'd like to group them together to make it easier for the users who will be using them.

Not sure if this makes sense but maybe Linc will get what I'm saying :D

Comments

  • jaredjared College Station, TX Icrontian
    edited September 2009
    For any hardcore wordpress nerds, here is my code that is basically taking custom fields and making them easy to find/display for the user.

    A "custom write panel" as it's called.

    [php]
    // *** CUSTOM FIELDS FUN :D
    //*************************************************************************************/
    $post_custom_fields =
    array(
    "news_thumbnail" => array(
    "name" => "news_thumbnail",
    "std" => "",
    "title" => "NEWS Thumbnail Image:",
    "description" => "Image path eg: /wp-content/uploads/texting.jpg. If the image is not 250x190 it will be resized.",
    "cat"=> "news"
    ),
    "news_pollid" => array(
    "name" => "news_pollid",
    "std" => "",
    "title" => "NEWS Poll ID:",
    "description" => "eg: 1522545",
    "cat"=> "news"
    ),
    "tdser_thumbnail" => array(
    "name" => "tdser_thumbnail",
    "std" => "",
    "title" => "TDSer Thumbnail Image:",
    "description" => "Image path eg: /wp-content/uploads/texting.jpg. If the image is not 160x160 it will be resized.",
    "cat"=> "tdser"
    ),
    "tdser_location" => array(
    "name" => "tdser_location",
    "std" => "",
    "title" => "TDSer Location:",
    "description" => "",
    "cat"=> "tdser"
    )
    );

    function post_custom_fields() {
    global $post, $post_custom_fields;
    echo '';
    foreach($post_custom_fields as $meta_box) {
    $meta_box_value = stripslashes(get_post_meta($post->ID, $meta_box.'_value', true));

    if($meta_box_value == "")
    $meta_box_value = $meta_box;
    echo '';
    }
    echo '';
    echo '';
    echo''.$meta_box.'';
    echo '';
    echo'
    ';
    echo''.$meta_box.'';
    echo '';
    }

    function create_meta_box() {
    global $theme_name;
    if ( function_exists('add_meta_box') ) {
    add_meta_box( 'new-meta-boxes', 'Other Information', 'post_custom_fields', 'post', 'normal', 'high' );
    }
    }

    function save_postdata( $post_id ) {
    global $post, $post_custom_fields;

    foreach($post_custom_fields as $meta_box) {
    // Verify
    if ( !wp_verify_nonce( $_POST[$meta_box.'_noncename'], plugin_basename(__FILE__) )) {
    return $post_id;
    }

    if ( 'page' == $_POST ) {
    if ( !current_user_can( 'edit_page', $post_id ))
    return $post_id;
    } else {
    if ( !current_user_can( 'edit_post', $post_id ))
    return $post_id;
    }

    $data = $_POST[$meta_box.'_value'];

    if(get_post_meta($post_id, $meta_box.'_value') == "")
    add_post_meta($post_id, $meta_box.'_value', $data, true);
    elseif($data != get_post_meta($post_id, $meta_box.'_value', true))
    update_post_meta($post_id, $meta_box.'_value', $data);
    elseif($data == "")
    delete_post_meta($post_id, $meta_box.'_value', get_post_meta($post_id, $meta_box.'_value', true));
    }
    }

    add_action('admin_menu', 'create_meta_box');
    add_action('save_post', 'save_postdata');

    ?>
    [/php]which gives me this when you are in the admin

    attachment.php?attachmentid=27560&stc=1&d=1253121115

    I'm just trying to group those fields and tidy things up for the user. :D

    //edit: the code works wonderfully. im just anal for making things ascetically pleasing
  • LincLinc Owner Detroit Icrontian
    edited September 2009
    OK, so we're sorting an array by the value of a sub-element?

    This may be inelegant, but I'd create a new top-level array of categories and run through a foreach loop assigning each element (news_thumbnail, news_pollid, etc) to one of them.

    So you'd have array $cats with arrays news_thumbnail, news_pollid, etc. as elements, and do the same for each category. Every time it finds a new cat, it starts a new top-level element in the $cats array.

    It creates redundancy in the array but makes it a cinch to loop through as you want to here.
  • jaredjared College Station, TX Icrontian
    edited September 2009
    Yeah, I think you are right. It's going to be the most efficient way right now.

    Oh, if you ever do end up getting a Mac, you HAVE to get this for WP dev.
    http://top-frog.com/projects/wordpress-textmate-bundle/
    Started using it this week and it saves so much time :D

    Anyways, I'll try that out tonight and see how it works.

    /me back to writing custom widgets
Sign In or Register to comment.