I'm trying to use an ajax function in my custom template to retrieve a post's custom fields to show a post overview. Getting the custom fields is working, but the getting the permalink isn't. I'm getting the permalink so I can link to the post. All I get back when I use get_permalink() is the first letter. Also, be noted that I'm using AJAX to return an array. I don't know if that is making the problem though.

Thank you so so so much.



function ajax_action_stuff()
    $post_id = $_POST['post_id']; // getting variables from ajax post

    $get_plane_custom = get_post_custom($post_id);

    $condition = $get_plane_custom['Condition'];
    $rules = $get_plane_custom['Flight Rules'];
    $manufacturer = $get_plane_custom['Manufacturer'];
    $model = $get_plane_custom['Model'];
    $price = $get_plane_custom['Price'];
    $lease = $get_plane_custom['To Lease'];
    $time = $get_plane_custom['Total Time'];
    $year = $get_plane_custom['Year'];
    $permalink = get_permalink($post_id);

    $plane_details = array("condition" => $condition, "rules" => $rules, "manufacturer" => $manufacturer, "model" => $model, "price" => $price, "lease" => $lease, "time" => $time, "year" => $year, "link" => $permalink);

    foreach($plane_details as $key => $value)
        echo $value[0] . "|";


The value of index 'link' of the array $plane_details is a string, and thus $value[0] gives you the first letter only.

Just replace $value[0] by $value and all should be fine.

Why are you referencing the value this way, anyway?

|improve this answer|||||
  • $value wasn't working at all. All it returned when I was handeling it with javascript was Array – copilot0910 Mar 11 '13 at 20:22
  • Have you checked $post_id, $get_plane_custom and $permalink for their respective value? If there is everything working as expected until you set up the array, there is (normally) no way the echo is not working (as in you said you had no access to $value). – tfrommen Mar 11 '13 at 20:29
  • Also, I'm referencing the value because I'm making a way to grab the custom fields and show some quick facts about a post when you click on the post in the navigation bar. – copilot0910 Mar 13 '13 at 20:03
  • Well ... glad I could be of some help. :) But why is it working 'now' - and not before? What did you change (in your code or mine)? – tfrommen Mar 15 '13 at 15:38
  • All I changed was from $value[0] to $value. For some reason, it worked now but not before. After it worked, I tried to experiment and just echoed out the json encoded $plane_details array without the foreach. That worked as well. I don't know sometimes. As you may know, sometimes code breaks and fixes itself for no reason. Either way, your solution worked perfectly. Even though I didn't use it, you still taught me something, which definitely is commendable. – copilot0910 Mar 15 '13 at 18:19

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.