0

I have lots of subpages each having some custom fields.

I am using 2 plugins, eMembers for the membership levels, and Types for my custom fields.

I am trying to show the custom field information on the parent page. I have my code working fine. But then I tried to pull in the same data again but only show the ones with a certain checkbox ticked. I have that working as well.

But my problem is when I try and run both scripts at the same time. I only get the first script results to show.

my code is http://pastebin.com/vgeZwPxM

<!-- 
/////
START USER LOGGED IN
///// 
-->
<?php if(wp_emember_is_member_logged_in()) { ?>
    <!-- 
    /////
    START USER LOGGED IN - LEVEL PAID
    ///// 
    -->
    <?php if(wp_emember_is_member_logged_in(2)) { ?>
    <h3 class="grey">Top 3 In <?php wp_title(''); ?></h3>
    <?php

        // Get the page's children
        $top3 = get_pages('child_of='.$post->ID.'&sort_column=menu_order&parent='.$post->ID);

        if (!empty($top3)) { //check if there are any children pages.

        //If there are....
            global $post;
            foreach($top3 as $post) {

                $url = get_permalink($child->ID);
                $title = get_the_title($child->ID);
                $topThreeInArea =  types_render_field("top-3-in-area", array("raw"=>"true","separator"=>";"));
                $address =  types_render_field("address", array("raw"=>"true","separator"=>";")); 

                if ( !empty( $topThreeInArea ) ) {

                echo "<div class='col_3'>";
                    echo "<h3><a href='".$url."'>".$title."</a></h3>";

                        //If Address 
                        if ( !empty( $address ) ) {
                            echo "<p><i class='icon-pin'></i>";
                            echo "<span>".$address."</span></p>";
                        }
 echo "<span>".$facilities."</span></p>";
                        }

                    echo "<a href='".$url."' class='button green'>Visit Website</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                echo "</div>";
                }            
            }
        } 

    ?> 

    <div class='clear'></div>

    <?php } ?>

    <!-- 
    /////
    END USER LOGGED IN - LEVEL PAID
    ///// 
    -->




<div id="county-nav" class="listNav"></div>
<ul class="county " id="county">
    <?php

    // Get the page's children
    $children = get_pages('child_of='.$post->ID.'&sort_column=menu_order&parent='.$post->ID);

    if (!empty($children)) { //check if there are any children pages.

    //If there are....
        global $post;
        foreach($children as $post) {

            $url = get_permalink($child->ID);
            $title = get_the_title($child->ID);
            $address =  types_render_field("address", array("raw"=>"true","separator"=>";"));

            echo "<li class=''>";
                echo "<h3><a href='".$url."'>".$title."</a></h3>";
                echo "<div class='col_2'>";
                    //If Address 
                    if ( !empty( $address ) ) {
                        echo "<p><i class='icon-pin'></i>";
                        echo "<span>".$address."</span></p>";
                    }
                echo "</div>";
                echo "<div class='clear'></div>";
                echo "<a href='".$url."' class='button green'>Visit Website</a>&nbsp;&nbsp;&nbsp;&nbsp;";
            echo "</li>";

        }
    }

?> 


</ul>
<?php } ?>
<!-- 
/////
END USER LOGGED IN 
///// 
-->

Cheers Jord

5
  • This is a lot of code. Please simplify the problem description/code and put the code in your question.
    – kraftner
    Commented Feb 19, 2014 at 13:51
  • I've updated the post to show the code. Sorry, though it would be easier to have it on pasteBin.
    – ltjfansite
    Commented Feb 19, 2014 at 13:52
  • Is that any better? i've took a lot of the custom fields out?
    – ltjfansite
    Commented Feb 19, 2014 at 13:57
  • @JordanSayner : If you code is on Pastebin the question becomes meaningless when that pastebin goes away. That is why the code should be posted here.
    – s_ha_dum
    Commented Feb 19, 2014 at 14:17
  • Good Point. Didn't think of it like that, I have copied a trimmed down version into the answer now, so hopefully that will help in the future.
    – ltjfansite
    Commented Feb 19, 2014 at 14:19

1 Answer 1

1

I had the same issue, and found three ways to solve it. Types is really hard to convince to read data out of a different post than the current one in the loop...

That's the first solution, just create a loop using query_posts() and get the data there via types_render_field() as usual. Just remember to reset the query afterwards if you're doing this before your actual loop; but i guess you know about that. http://codex.wordpress.org/Function_Reference/wp_reset_query Inside the main loop, this is probably an almost impossible thing to do I guess.

The second solution is, iirc, to use setup_postdata($post) in you foreach-loop. This does some magic that finally point types_render_field() into the post you want. Again, this messes with all the functions like the_ID() or the_content() and such, so you could store the old global $post to a tmp variable and re-set it back via setup_postdata($tmp).

As the last resort, which is sometimes the least complicated one, you could just read the data manually by using get_post_meta(). To find the field name, it's best to look directly into the wp_postmeta table. I found a couple of guides for the naming convention of the fields in the database, but after some trial and error I always ended up starting Adminer to look at the DB.

So, choose wisely :)

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.