开发者

Archives.php not working in wordpress

开发者 https://www.devze.com 2023-03-23 17:42 出处:网络
Its been a while since ive done wordpress templating and for some reason i cant get the archives.php to work. i have this:

Its been a while since ive done wordpress templating and for some reason i cant get the archives.php to work. i have this:

<?php
/* Template Name: Archives */
get_header(); ?>
<div id='content'>
<?php get_sidebar('left'); ?>

<div id='column2-wide'>
    <?php if (have_posts()): while (have_posts()): the_post(); ?>
        <?php if ( in_category(16) ) { ?>
            <h2><?php the_title(); ?></h2>
            <div class="post">
                <?php echo the_content(); ?>
            </div>
        <?php } ?>
    <?php endwhile; endif; ?>
</div><!-- column2 -->
<?php get_footer(); ?>

Then created a page in the admin and chosen the Archives template to be used from the dropdown.

However the posts just dont seem to show. Am i missing something? The very same code works in the index.php file. It seems its just not working when im trying to display posts in a page.

It could well be im missing a file as I started developing the theme using a skeleton theme by Kennethreitz which can be found here:

h开发者_开发技巧ttps://github.com/kennethreitz/wordpress-theme-skeleton/

Any help would be appreciated.

Thanks for reading.

fl3x7

EDIT--> Also ive removed the category check so it should just list all posts but instead what it does is just echo the title of the current page if that helps


I'm assuming that "16" is the category ID? According to the WordPress docs for in_category, if you're querying by ID it should be passed as an integer.

$category (mixed) (required) One or more categories specified by ID (integer), name or slug (string), or an array of these

With your current code, the in_category check is failing every time since it is checking for an association with the category named "16." Try this instead:

if ( in_category(16) ) {
    ...
}


Thanks to the help provided by Hans. This is what I did:

query_posts( array ( 'category_name' => 'foo', 'posts_per_page' => 10 ) );


if (have_posts()): while (have_posts()): the_post(); ?>
        <h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2>
        <div class="post">
            <?php echo the_excerpt(); ?>
        </div>
<?php endwhile; endif; ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消