• Resolved bipolargravity

    (@bipolargravity)


    Sorry to be a bother, good people – I’ve been trying my darndest to figure things out on my own when something goes wrong, but this one has me stumped! Probably got a real simple solution, one that will make me look like a complete novice, but I’m just not seeing it here. I think my brain’s pretty readily given up.

    I’ll try to keep this as short as possible, but I’m afraid I’m a bit rubbish with words…

    Basically, I’m using the wonderful function created by Shelley Cole, used to allow child categories to inherit the template of their parent. It’s absolutely wonderful and works a treat, but unfortunately, it seems to have caused a wee bit of a problem.

    I have a category that, for argument’s sake, we’ll call Gallery X. The template for Gallery X does not contain any post data, and simply pulls a list of its subcategories using get_categories. The output looks something like this when you go to mysite.com/gallery-x

    GALLERY X
    — Child 1
    — Child 2
    — Child 3

    etc…

    The child categories are full of posts that use custom fields to generate a thumbnail image and a link to, say, a video on my server, which opens in a Shadowbox window. This worked absolutely perfectly until I set up Shelley Cole’s function – for some reason, WordPress is no longer getting the value of the custom fields ‘gallery_thumbnail’ and ‘gallery_file’. The link to the gallery_file and thumbnail are remaining, quite stubbornly, the URL of the current child category.

    Example

    You’re in Child 1 of Gallery X. WordPress is displaying what SHOULD be the thumbnail, but instead of mysite.com/url/or/path/to/file-or-thumbnail.mov, both links are mysite.com/gallery-x/child-1, so all you get is a broken image with the alt text and a link to the category you’re already in, helpfully opening in Shadowbox.

    I’m pretty stumped here. Using Shelley’s function isn’t VITAL to the inner workings of my website; I could do away with it without losing any functionality, but it makes my life SO much easier than having to assign exactly the same template to the countless child categories over and over again. I would imagine a possible solution would be to somehow modify the function to exclude certain categories, or to work out exactly WHY it’s not pulling the custom fields when it should.

    Here’s the markup I’m using on gallery x’s template file to call the list of child categories:

    <ul class="list-cat"><?php $categories = get_categories('child_of=68&orderby=ID&order=asc');
    	 foreach ($categories as $cat) { ?>
    	    <li class="cat-item"><h2><a href="<?= get_category_link( $cat->cat_ID ); ?>"><?= $cat->cat_name; ?></a></h2> <h4><?= ''.$cat->category_count.' items'; ?></h4></li>
       <?php } ?>
    	</ul>

    And here’s me trying to get the custom fields on one of the child categories:

    <li><a class="galthumbs" href="<?php $key="gallery_file"; echo get_post_meta($post->ID, $key, true); ?>" rel="shadowbox;width=500;height=376" title="<?php the_title(); ?>"><img src="<?php $key='gallery_thumbnail'; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title(); ?>" /></a></li>
    The URL for the function I’m using is above, but to save your scrollwheels, here it is again. https://brassblogs.com/blog/making-child-categories-recognize-parent-displays

    I’m sorry for the super-long message. I wish I knew what was going on here! I thank you in advance for taking the time to read and attempt to understand my mindless babble.

Viewing 3 replies - 1 through 3 (of 3 total)
  • MichaelH

    (@michaelh)

    Custom fields are kept at the post level–you assign the custom field values to a post, correct? It would seem you are not querying any post(s) for each of those ‘child categories’.

    So you need to use something like this inside your foreach ($categories as $cat) { and before the get_post_meta. :

    $myposts=get_posts('showposts=1&cat='.$cat->cat_ID);
    if ($myposts) {
    $postid = $myposts[0]->ID;
    }

    Also change $post->ID to $postid in you get_post_meta code.

    So without seeing your whole template, guessing you could do this:

    <ul class="list-cat"><?php $categories = get_categories('child_of=68&orderby=ID&order=asc');
    foreach ($categories as $cat) { ?>
      <li class="cat-item"><h2><a href="<?= get_category_link( $cat->cat_ID ); ?>"><?= $cat->cat_name; ?></a></h2> <h4><?= ''.$cat->category_count.' items'; ?></h4></li>
      <?php
      $myposts=get_posts('showposts=1&cat='.$cat->cat_ID);
      if ($myposts) {
        $postid = $myposts[0]->ID;
      }
      ?>
      <li><a class="galthumbs" href="<?php $key="gallery_file"; echo get_post_meta($post->ID, $key, true); ?>" rel="shadowbox;width=500;height=376" title="<?php the_title(); ?>"><img src="<?php $key='gallery_thumbnail'; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title(); ?>" /></a></li>
    <?php } ?>
    	</ul>

    Thread Starter bipolargravity

    (@bipolargravity)

    Thanks ever so much for the response!

    The situation has been resolved now and everything works perfectly, thank you for putting me on the right track! I really, really appreciate the help. You guys are great ??

    Wow – I just found this, thanks to someone who emailed me about this issue! I thank you for the compliment, bipolar, (and thanks, Michael, for your awesomeness, yet again!) but I should let you know that the code you’re using is pretty old. (As a side note, I didn’t invent it either – I found it here on the forums, and I’ve searched for it to see who to give proper credit to but haven’t been able to find it again – it may well have been MichaelH!) I know the post is dated this year, but I do indicate in the content that I got the code a long time ago – which is true. I initially found it for a WP 2.0 site I was developing… I think about 2 years ago? Anyway, I’ve since found out that it messes with your RSS feed, and I still haven’t been able to figure out how to fix that yet. The “custom fields” thing is new to me too – especially since the original site I was using it on used a ton of custom fields and worked fine.

    But again, that was 2.0.

    I know there has been several changes since then. ??

    I’m going to update my original post to reflect these issues. But I can say this: because of how WP has developed, this code is pretty much pointless. There’s a lot of functions that are now within WP that pretty much make this code obsolete and unnecessary to use – especially with the issues it presents.

    Like I said, I’ll update the post to make note of the issues that are coming about – but I will also post something that might be able to help those of you who want to use something that has the same end result, but works a LOT better than this old stuff.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WordPress not getting custom field values’ is closed to new replies.