The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

get_children not working in category page (9 posts)

  1. jcowart
    Posted 5 years ago #

    I'm using get_children() in a function that displays all the images attached to a post.

    $images = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );

    It works great when called the function is called from single.php but it doesn't retrieve any images when called from category.php.

    Can anyone help me?

  2. jcowart
    Posted 5 years ago #

    Any ideas why get_children() would work on some pages and not on others?

    The same code is being called from both my single.php and my category.php.

  3. Michael
    Forum Moderator
    Posted 5 years ago #

    can you paste the full code of the category.php (and probably also single.php as a comparison) into a http://wordpress.pastebin.com/ and post the link to it here?

    isolated lines of code can behave totally different depending on the location they are embedded into a template.

  4. jcowart
    Posted 5 years ago #

    I pasted category.php and single.php. They both call a function named get_main_images() which contains the get_children call. I included the get_main_images function in the bin as well.


  5. jcowart
    Posted 5 years ago #

    HELP! I can't figure this out. The code for category.php and single.php are virtually identical. get_children() works in single.php and won't in category.php. Yet both are set to display the same post.

    Can someone please help me with this?

  6. Michael
    Forum Moderator
    Posted 5 years ago #

    try to add global $post; at the beginning of the function:

    function get_main_image($size = 'large', $limit = '1', $offset = '0', $post) {
            global $post;
            $images = get_children( a.......
  7. jcowart
    Posted 5 years ago #

    Thanks, but that didn't work.

    This is so weird. My two image functions that call get_children both work when they're on my blog and my archive and my single posts. It's only in category that they don't work. Is there something about category that would prevent get_children from retrieving attached images?

  8. jcowart
    Posted 5 years ago #

    Here's how we solved it. We added 'attachment" to our post_type for our category page. Too, too simple.

    post_type (string / array) - use post types. Retrieves posts by Post Types, default value is 'post';

    ■'any' - retrieves any type except revisions
    ■Custom Post Types (e.g. movies)

  9. lerizzle
    Posted 5 years ago #

    jcowart, can you explain what you did? I'm trying to acheive the same thing. Didn't you have post-type attachement in your initial call?

Topic Closed

This topic has been closed to new replies.

About this Topic