Pulling custom fields from everywhere – not working
-
Hello everyone, thanks for taking the time to read through this.
I am making my own template using a mixture of tutorials, code snippets and trial and error. Yes, it’s as confusing as it sounds.
Anyway, for my home.php I have a features section where I want to place snippets from four featured Pages (not Posts) into pretty boxes.
Each box will have a small image, which links to the article in question, a snippet from the article and a Read More button that also links to the page.
The snippet and the image are both set as custom fields in each of the pages.
Each of the Pages is a child page of the Products page (ID 31)
The products page has a Custom Field called products, which contains the valueproduct1|27,product2|36|product3|45,product4|54
– i.e. the page IDs of each of the featured pages.
The plan is, based on a glossed-over snippet of tutorial I had seen to use the explode command to first pull the names and IDs of the child pages from the main Product page by exploding the products custom field, then explode the results of that allowing me to pull the blurblogo and featureblurb custom fields from each of the child pages to dynamically fill my boxes.
With me so far?
Ok, here is the code I used for this…<?php $productsCF = get_post_meta(31, "products", true); // example value = "pagename1|92,pagename2|94" $allProducts = explode(",", $productsCF); // $allCategories[0] = "pagename1|92" // $allCategories[1] = "pagename2|94" foreach ($allProducts as $product) { $pieces = explode("|", $product); // $pieces[0] = "pagename" // $pieces[1] = 92 $link = get_permalink('$pieces[1]'); $blurbpic = get_post_meta($pieces[1], "blurblogo", true); $blurb = get_post_meta($pieces[1], "featureblurb", true); echo "<div class='prodbox'>"; echo "<a href='$link'>"; echo "<img class='blurblogo' src='$blurbpic'></a>"; echo "<p> $featureblurb </p>"; echo "<a class='readmore' href='$link'>Read More...</a>"; echo "</div>"; query_posts("posts_per_page=-1&post_type=page&post_parent=$pieces[1]"); while (have_posts()) : the_post(); ?> <?php endwhile; wp_reset_query(); echo "</div>"; }; ?>
Unfortunately, it seems to be only half working. It’s placing my four boxes (they are CSS styled, so it’s obviously picking up the div class) and it is finding the correct links, but the boxes themselves are empty. If I pull up the source code I just get this for that particular section…
<div class='prodbox'> <a href=''> <img class='blurblogo' src=''> </a> <p> </p> <a class='readmore' href=''>Read More...</a> </div> </div> <div class='prodbox'> <a href='https://localhost/mywebsite/products/product2/'> <img class='blurblogo' src=''> </a> <p> </p> <a class='readmore' href='https://localhost/mywebsite/products/product2/'>Read More...</a> </div>< /div> <div class='prodbox'> <a href='https://localhost/mywebsite/products/product3/'> <img class='blurblogo' src=''> </a> <p> </p> <a class='readmore' href='https://localhost/mywebsite/products/product3/'>Read More...</a> </div> </div> <div class='prodbox'> <a href='https://localhost/mywebsite/products/product4/'> <img class='blurblogo' src=''> </a> <p> </p> <a class='readmore' href='https://localhost/mywebsite/products/product4/'>Read More...</a> </div>
Note the empty first $link and readmore $link, the empty P tags where the $featureblurb should be and the weird closing div tags popping up everywhere.
I know it’s not WordPress’s fault, it’s me, missing somethign or doing something wrong. But what is it?
Am I going about this all wrong? is there a better way to achieve what I want to do? (and let’s face it, you can’t get much worse than “not working”)
Thanks for taking the time to read through all of this, any help would be greatly appreciated.Matt
- The topic ‘Pulling custom fields from everywhere – not working’ is closed to new replies.