Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Have a specific page act as an archive? WordPress

I am looking to make specific pages behave as archive pages. I have information already populated in an wp_option field, so, for example there is an array:

$settings[123] = array(
array(4) {
["post_type"]=>
string(4) "post"
["category_name"]=>
string(9) "computers"
["posts_per_page"]=>
int(15)
["paged"]=>
bool(false)
}

);

and

$settings[124] = array(
array(4) {
["post_type"]=>
string(4) "post"
["category_name"]=>
string(7) "tablets"
["posts_per_page"]=>
int(10)
["paged"]=>
bool(true)
}
);


for the first example I would want post with the ID of 123 (which happens to be a page) to have it's query output the last 15 posts with the category "computers" --- that is if I had a loop that ran through <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

I would be able to output information as if it were an archive page.

The second example would be almost the same, post with the ID 124 (it happens to be a page) to act as an archive for posts that have the category tablets and output 10 and allow it to be paginated... so that if I ran through <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> it would act as if it were an archive page...


I tried hooking into pre_get_posts a function (pre_get_posts_123($query)) but when I add $query->set("post_type", $settings[123]["post_type"]) [the latter variable essentially is "post" ] -- the page itself returns a 404! if I remove that line it does not.


Any help would be greatly appreciated...

Thanks,
MR

Answers (2)

2012-09-30

John Cotton answers:

I think you're making it too complicated.

All you need to do is create templates (eg page-124.php) in your theme for each of the pages you want to act in a special way and then code in there the output you want.

No need for options...


mackrider comments:

No, I am afraid I am not. This is going to be part of a plugin that will be extensible, so I will need the archives to be generated on the fly. Essentially as part of the plugin's architecture I will have a subdirectory /addons/ that I will drop a template file and an array file, the template file is going to be built as if it were already accepting the archive query. The array file sets up the page to use template_redirect for the custom template and also contains the logic for the archive page's query...


John Cotton comments:

Hi Matt

How did you get on with that change?

I'm still not clear why you need a page created (other than that the client wants one!). I had a look at the code this morning and can't see any particular reason in there. I could understand if your client wanted to make use of the page editing (for some editable content at the top of the archive), or perhaps even a featured image that could easily be managed through a page.

But from the look of the code, it's pure archive stuff. If it were me, I'd add some custom rewrites so that I get me urls (which the pages are currently providing) and then rewrite to post or taxonomy archives.


mackrider comments:

John, you are brilliant. That is the right way to accomplish this!

2012-10-01

Arnav Joy answers:

you can try this

<?php query_posts('category_name=computers&post_type=post&postsperpage=15');?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>


and for second template where pagination is required try this

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('category_name=tablets&post_type=post&postsperpage=10&paged='.$paged);?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'theme' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'theme' ) ); ?></div>


<?php endif; ?>