Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to write a function that will output a set of posts on a page. Because I want to display more than just the post titles, wp_get_archives() won't do (I think). The function to add the post list parses the page meta to find out what kind of list it's supposed to output. Then it runs a query for those post types and outputs them using the same template fragment used for in archive-{posttype}.php. At least that's how it's supposed to work. Here's the function which lives in my functions.php file:

function ASH_archive(){
    global $posts;
    $postholder=posts;//so I can reset to original after archives done
    if(!$ptype=get_post_meta('ASHarchivetype')) return;//no archive requested
    $path="layouts/layout-blog.php";
    $pt='post';
    switch($ptype){
        case 'equipment':
        case 'dyeing':
        case 'spinning':
        case 'weaving':
            $pt='ash_'.$ptype;
            $path="layouts/{$ptype}/layout-blog.php";
            break;
        case 'loomsong':
            $pt='ash_loomsong';
            break;
    }
    $posts=get_posts(array('post_type'=>$pt));
    get_template_part($path);
    $posts=$postholder; //reset to original value
}

I want to use the function in this template:

<?php
/*
Template Name: Archives with Content
*/

get_header(); ?>
<div id="main-col">
<?php
suffusion_page_navigation();
suffusion_before_begin_content();
?>
    <div id="content">
<?php
global $post;
if (have_posts()) {
    while (have_posts()) {
        the_post();
        $original_post = $post;
?>
        <div <?php post_class('fix'); ?> id="post-<?php the_ID(); ?>">
<?php suffusion_after_begin_post(); ?>
            <div class="entry-container fix">
                <div class="entry fix">
                    <?php suffusion_content(); ?>
                </div><!--/entry -->
            <?php
                // Due to the inclusion of Ad Hoc Widgets the global variable $post might have got changed. We will reset it to the original value.
                $post = $original_post;
                suffusion_after_content();
            ?>
            </div><!-- .entry-container -->
<?php

ASH_archive(); //NOTHING HAPPENS. WHY????

comments_template();
?>
    </div><!--/post -->
<?php
    }
} 
?>
</div></div>
<?php get_footer(); ?>

The function doesn't seem to do anything. I don't know if this is because the function is written improperly or if I need to attach it to a hook of some kind. I've read about filters and actions and I can't figure out which hook I should use for my function or even if that's the right direction. Help?

ps, what might be even nicer, is a shortcode to call the function whereever I wanted to.

share|improve this question
    
Can you also add the code of your layouts/layout-blog.php page? Maybe there is an error there with the way you read the $posts variable. –  Jan Fabry Sep 10 '11 at 19:39

1 Answer 1

up vote 1 down vote accepted

I'm not sure I have enough info for this to work as you expect. I can tell you that what you are using won't work for a few, regardless of the filters you use.

First error: You are using brackets that aren't closed

Second, you are using if have posts... but then you reset it at the very end of the code.

Here is what I've put together that fixes the above errors, but I'm not sure it will work the way you want (at least the syntax is correct).

<?php
/*
Template Name: Archives with Content
*/

get_header(); ?>
<div id="main-col">
<?php
    suffusion_page_navigation();
    suffusion_before_begin_content();
?>
<div id="content">
<?php
    global $post; 
    if (have_posts()) : while (have_posts()) : the_post();
    $original_post = $post;
    ?>
<div <?php post_class('fix'); ?> id="post-<?php the_ID(); ?>">
    <?php suffusion_after_begin_post(); ?>
    <div class="entry-container fix">
        <div class="entry fix">
            <?php suffusion_content(); ?>
        </div><!--/entry -->
        <?php endwhile; ?>
<?php else : // Due to the inclusion of Ad Hoc Widgets the global variable $post might have got changed. We will reset it to the original value.
    $post = $original_post;
    suffusion_after_content();
?>
        <?php endif; ?>
    </div><!-- .entry-container -->
</div>
</div>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.