• Hi there,

    I’ve gone round and round in circles all day with this, so hopefully someone can help!

    I’m setting up a completely different (from the main site) template for the blog for a client, and the theme was designed by someone else some time ago.

    The blog page that was already set up was very simple, and the blog template that existed was (strangely..) a copy of the custom home page, so I’ve tried to replace it (the blog template page) with a simple loop, just to get going.

    Loop:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    		<h1><?php the_category(); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    		<?php the_content(); ?>
    	</div>
    
    	<div class="navigation">
    		<div class="next-posts"><?php next_posts_link(); ?></div>
    		<div class="prev-posts"><?php previous_posts_link(); ?></div>
    	</div>
    
        <?php endwhile; else: ?>
        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
        <?php endif; ?>

    However, the previous designer did something I’ve never seen before: they have this code in functions.php:

    if (!function_exists('show_blog')){
        function show_blog(){
            $args = array(
                'numberposts'     => 3,
                'offset'          => 0,
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'post_type'       => 'post',
                'post_status'     => 'publish' );
    
            $posts = get_posts($args);
            foreach($posts as $key=>$value):
    ?>
        <div style="width: 700px;">
            <h2><a href="<?=get_bloginfo('wpurl');?>/<?=$value->post_name;?>"><?=$value->post_title?></a></h2>
    
            <?=nl2br($value->post_content)?>
    
            <br/>
            <br/>
            <br/>
            <br/>
        </div>
    <?php
            endforeach;
        }
        add_shortcode('showblog', 'show_blog');
    }

    To get the posts to show up as expected under /blog, I need to:

    – keep that code in functions.php
    – have the loop on the blog template (eg template-blog.php)
    – have the shortcode [showblog] on the page ‘Blog’

    But this isn’t giving me much freedom with styling and arranging how the posts are shown – I need to do this on the blog template (so I can include comments, categories, navigation etc etc).

    Does anyone know what the previous designer has done here, and how I can take out this code from the functions file, and just have the normal loop show the posts in the blog template.

    Am I being completely stupid?!

  • The topic ‘Code for posts in functions.php so can't change loop’ is closed to new replies.