• I’d like to have the recent posts displayed on this page https://199.119.123.133/index-3.php (see bottom of page). However, each section that I’d like to display recent posts has it’s own div section, and when I try to copy/paste the same code that I’m currently using for the recent post that’s working for “Introducing the Surety Cloud”, it causes a 500 error (likely due to the php code being used twice for the recent posts).

    I’m assuming you can’t have the code replicated, but I’m not sure how else to do this. Any thoughts?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Assuming the code just loops over an array, you need only put the <div> section inside that loop. For example:

    foreach ($recent_posts as $recent_post) {
    ?>
    <div>
    <?php
        echo $recent_post;
    ?>
    </div>
    <?php
    }

    For some reason it doesn’t look like you’re using WordPress.

    Thread Starter eweisbrot

    (@eweisbrot)

    WordPress is not installed on the dev server I linked to, but I’m calling the “wp-blog-header.php” from our live server where WP is installed at the top of the page.

    Let me post the code here so I can offer more detailed as to what my issue is (see below). The first div is getting the recent post properly. However, when I try to use the same code in the second div, it causes a 500 error and I’m not sure how to use multiple loops (I tried reading the WP documentation but couldn’t quite grasp it).

    <div class="col-md-6">
    	<article>
    		<?php
    			global $post;
    			$args = array( 'posts_per_page' => 1 );
    			$myposts = get_posts( $args );
    			foreach( $myposts as $post ) :	setup_postdata($post);
    		?>
    		<div class="date">
    			<span class="day"><?php the_time('jS'); ?></span>
    			<span class="month"><?php the_time('M, Y'); ?></span>
    		</div>
    		<h4>
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br />
    			<?php endforeach; ?>
    		</h4>
    		<p>
    			<?php
    				the_excerpt_max_charlength(140);
    
    				function the_excerpt_max_charlength($charlength) {
    					$excerpt = get_the_excerpt();
    					$charlength++;
    
    					if ( mb_strlen( $excerpt ) > $charlength ) {
    						$subex = mb_substr( $excerpt, 0, $charlength - 5 );
    						$exwords = explode( ' ', $subex );
    						$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
    						if ( $excut < 0 ) {
    							echo mb_substr( $subex, 0, $excut );
    						} else {
    							echo $subex;
    						}
    						echo '...';
    					} else {
    						echo $excerpt;
    					}
    				}
    			?>
    		</p>
    	</article>
    </div>
    <div class="col-md-6">
    	<article>
    		<div class="date">
    			<span class="day">15</span>
    			<span class="month">Jan</span>
    		</div>
    		<h4><a href="blog-post.html">Lorem ipsum dolor</a></h4>
    	</article>
    </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display recent posts in multiple separate div's on same page’ is closed to new replies.