Forums

Using get_children outside of wordpress (7 posts)

  1. noyz319
    Member
    Posted 4 years ago #

    Hi, I have the below piece of code that i use in my sidebar throughout the site without problem. I am trying to figure out why it does not work with i include it in a non-wordpress file.

    I have a web forum integrated with wordpress and i have the required call to wp-blog-header.php and all the other wordpress functions seem to work just fine except for get_children is giving me troubles.

    <?php query_posts("cat=3&showposts=5"); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    $files = get_children(
    array(
    	'post_parent' => $post->ID,
    	'post_status' => 'inherit',
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'order' => 'ASC',
    	'orderby' => 'menu_order'
    	)
    );
    if($files) {
    	$keys = array_keys($files);
        $num = $keys[0];
    	$firstImageSrc = wp_get_attachment_image_src($num, "medium", $icon);
    	if ($firstImageSrc <> null) {
    	echo "<img src=\"{$firstImageSrc[0]}\" width=\"120\" alt=\"$title\" />"; }
    <?php endwhile; ?>
    <?php else : ?>
    <h2>Not Found</h2>
    <?php endif; ?>

    The code is supposed to return the first image from each post and does so fine throughout the site, but when i use it in the forum it instead returns the first image from the first post i ever made (like 9 years ago).

    Does anybody have any idea why it is not referencing the correct post ID?

    Thanks!

  2. wp-api
    Member
    Posted 4 years ago #

    Hello, could you post the source code that you currently use to include the functions please?

  3. noyz319
    Member
    Posted 4 years ago #

    This is the code i use:
    require_once('../wp-blog-header.php');

    Its lets me call get_header() etc... and the loop works fine, only the above mentioned code with get_children is returning the incorrect thumbnail.

  4. noyz319
    Member
    Posted 4 years ago #

    Still having troubles with this, anybody have idea for a possible workaround?

  5. noyz319
    Member
    Posted 4 years ago #

    Pretty please! This is driving me nuts. Also discovered that when using the same code in my non-WP sidebar it really slows down the page load, although all WP pages with the same code load nice and quick.

  6. _timk
    Member
    Posted 4 years ago #

    <?php query_posts("cat=3&showposts=5"); ?>

    Maybe try explicitly defining the sort order? It's supposed to default to DESC, but maybe something's getting in the way.

    <?php query_posts("cat=3&showposts=5&orderby=date&order=desc"); ?>

  7. noyz319
    Member
    Posted 4 years ago #

    I finally have a workaround! For some reason the method describe here works fine outside of wordpress. Basically just putting the code as a function in the functions.php file made the ordering no longer an issue.

Topic Closed

This topic has been closed to new replies.

About this Topic