Support » Themes and Templates » Need help with get_posts on index.php!

  • Resolved tylermenezes

    (@tylermenezes)


    I need some help with using get_posts on index.php! When I use it at the top of the page (before anything else) things like the_permalink() give the permalink of the first post in The Loop. In fact everything but the_excerpt() does. Could anyone help? I’ve included the code below.

    <div id="featured">
    <h1>Featured Story</h1>
    <?php $posts2 = get_posts( "numberposts=1&orderby=post_date&category=7" ); ?>
    <?php if( $posts2 ) : ?>
    <?php foreach( $posts2 as $post2 ) : setup_postdata( $post2 ); ?>
    <h2><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => null,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, true, array(250,200));
    		break;
    	}
    }
    ?>
    <?php the_excerpt(); ?>
    <hr />
    <div style="text-align: right"><strong><a href="<?php the_permalink() ?>">Continue reading '<?php the_title(); ?>' &raquo;</a></strong></div>
    <?php endforeach; ?>
    <?php endif; ?>
    </div>

    Addendum: I already tried putting it in a separate file and including that. No luck.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use the Wp-sticky plugin and you will have your “featured” post on the top.

    That does not work for my needs at all. Could someone just explain the problem with the code?

    If I’m interpreting the codex properly, you may need to globalize $post2.

    I tried, for example, 'post_parent' => $GLOBALS['post2']->ID and $GLOBALS['post2']->the_title; as well as $GLOBALS['post2']->title; but that doesn’t output anything. Ideas?

    Addendum: It does work for pictures though. Ex:

    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => null,
    	'post_status' => null,
    	'post_parent' => $GLOBALS['post2']->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, true, array(250,200));
    		break;
    	}
    }
    ?>

    Addendum Addendum: $GLOBALS['post2']->ID does work (echoing it to the browser produces 11, the expected value), but nothing else seems to (echo($GLOBALS['post2']->title); = null). Am I even going the right way with this?

    More Thoughts: I’m able to get get_permalink($GLOBALS['post2']->ID) to work, does anyone know of a similar tag for title? That’s all I’m missing now.

    Okay, finished with my code. Because WordPress apparently doesn’t have a function for what I needed, I hacked one with SQL. Here’s my final code if it helps anyone:

    <?php $posts2 = get_posts( "numberposts=1&orderby=post_date&category=7" ); ?>
    <?php if( $posts2 ) : ?>
    <?php foreach( $posts2 as $post2 ) : setup_postdata( $post2 ); ?>
    <h2><a href="<?php echo get_permalink($GLOBALS['post2']->ID) ?>" title="Permanent Link to <?php echo $GLOBALS['wpdb']->get_var("SELECT <code>post_title</code> FROM <code>&quot; . $GLOBALS['wpdb']->prefix . &quot;posts</code> WHERE id=" . $GLOBALS['post2']->ID); ?>"><?php echo $GLOBALS['wpdb']->get_var("SELECT <code>post_title</code> FROM <code>&quot; . $GLOBALS['wpdb']->prefix . &quot;posts</code> WHERE id=" . $GLOBALS['post2']->ID); ?></a></h2>
    <?php
    
    $args = array(
    	'post_type' => 'attachment',
    	'numberposts' => null,
    	'post_status' => null,
    	'post_parent' => $GLOBALS['post2']->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    		echo apply_filters('the_title', $attachment->post_title);
    		the_attachment_link($attachment->ID, true, array(250,200));
    		break;
    	}
    }
    ?>
    <?php the_excerpt(); ?>
    <hr />
    <div style="text-align: right"><strong><a href="<?php echo get_permalink($GLOBALS['post2']->ID) ?>">Continue reading '<?php echo $GLOBALS['wpdb']->get_var("SELECT <code>post_title</code> FROM <code>&quot; . $GLOBALS['wpdb']->prefix . &quot;posts</code> WHERE id=" . $GLOBALS['post2']->ID); ?>' &raquo;</a></strong></div>
    <?php endforeach; ?>
    <?php endif; ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Need help with get_posts on index.php!’ is closed to new replies.