• I have WP at <url>/blog/. I wish to be able to display a few posts on my homepage at <url>/index.php. Here’s the code I adapted from wp-admin\includes\dashboard.php:

    <?php
    require_once('/blog/wp-admin/admin.php');
    require_once('/blog/wp-admin/includes/dashboard.php');
    
    function wp_show_my_blog_posts() {
    	$drafts_query = new WP_Query( array(
    		'post_type' => 'post',
    		'post_status' => 'publish',
    		'author' => $GLOBALS['current_user']->ID,
    		'posts_per_page' => 3,
    		'orderby' => 'modified',
    		'order' => 'DESC'
    	) );
    
    	$drafts =& $drafts_query->posts;
    
    	if ( $drafts && is_array( $drafts ) ) {
    		$list = array();
    		foreach ( $drafts as $draft ) {
    			$url = get_permalink( $draft->ID );
    			$title = _draft_or_post_title( $draft->ID );
    			$item = "<h4><a href='$url' title='" . sprintf( __( 'Edit “%s”' ), esc_attr( $title ) ) . "'>" . esc_html($title) . "</a> <abbr title='" . get_the_time(__('Y/m/d g:i:s A'), $draft) . "'>" . get_the_time( get_option( 'date_format' ), $draft ) . '</abbr></h4>';
    			if ( $the_content = preg_split( '#\s#', strip_tags( $draft->post_content ), 21, PREG_SPLIT_NO_EMPTY ) )
    				$item .= '<p>' . join( ' ', array_slice( $the_content, 0, 20 ) ) . ( 20 < count( $the_content ) ? '&hellip;' : '' ) . '</p>';
    			$list[] = $item;
    		}
    ?>
    	<ul>
    		<li><?php echo join( "</li>\n<li>", $list ); ?></li>
    	</ul>
    	<p class="textright"><a href="/blog/"><?php _e('View all'); ?></a></p>
    <?php
    	} else {
    		_e('There are no drafts at the moment');
    	}
    }
    ?>

    However, it throws me onto the login-into-WP-admin page. How do I avoid that and show the posts to everyone?

Viewing 1 replies (of 1 total)
  • Thread Starter dchernik

    (@dchernik)

    OK, got it… getting acquainted with query_posts(). Could have saved myself some time with it rather than hacking my way through.

Viewing 1 replies (of 1 total)
  • The topic ‘Showing Several Posts on Homepage’ is closed to new replies.