开发者

Make WordPress homepage a post category?

I am trying to set my WordPress homepage to a category but it 开发者_C百科only allows me to set it to either the latest posts or a static page.

Is it possible to set your homepage as a post category?


I am hoping that you know about how to set static page. So first create an empty .php file and name it whatever you like and put it along the other files (index.php, arhive.php etc).

and then enter following code

    <?php
/*
 * Template Name: Category based Homepage
 */
?>

<?php get_header(); ?>
<div class="main">

    <?php
    $cat_ID = '1'; //it should be your category ID, you can get the id of the category by going to categories and edit and then in url you can find the tag_ID.
    $posts_to_show = '10'; // number of posts from the category you want to show on homepage
    //query_posts("cat=$cat_ID&showposts=$posts_to_show");
    $category_posts = new WP_Query("cat=$cat_ID&showposts=$posts_to_show");
    //if (have_posts()) 
    if ($category_posts->have_posts())
        : $first = true;
        ?>
        <ul class="post-list">
            <?php
            //while (have_posts()) : the_post();
            while ($category_posts->have_posts()) : $category_posts->the_post();
                if ($first)
                    $class = "first-in-row";
                else
                    $class = "";
                $first = !$first;
                ?>
                <!-- Start: Post -->
                <li <?php post_class($class); ?>>
                    <?php the_post_thumbnail(); ?>
                    <p class="categories"><?php the_category(", "); ?></p>
                    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php edit_post_link(__('Edit', 'your_theme_text_domain'), '', ''); ?></h2>
                    <p class="post-meta"><span class="date"><?php the_time(get_option('date_format')) ?></span> <?php if (comments_open()) : ?>, <span class="comments"><?php comments_popup_link(_x('0', 'comments number', 'your_theme_text_domain'), _x('1', 'comments number', 'your_theme_text_domain'), _x('%', 'comments number', 'your_theme_text_domain')); ?></span> <?php endif; ?> <span class="author"><?php the_author() ?></span></p>
                    <?php the_excerpt(); ?>
                    <p class="more"><a href="<?php the_permalink() ?>"><?php _e('Read More &raquo;&raquo; ', 'your_theme_text_domain'); ?></a></p>
                    <?php if (has_tag()): ?><p class="tags"><span><?php the_tags(""); ?></span></p><?php endif; ?>
                </li>
                <!-- End: Post -->
            <?php endwhile; ?>
        </ul>
    <?php else : ?>
        <h2 class="center"><?php _e('Not found', 'your_theme_text_domain'); ?></h2>
        <p class="center"><?php _e('Sorry, but you are looking for something that isn\'t here.', 'your_theme_text_domain'); ?></p>
    <?php
    endif;
    //wp_reset_query();
    wp_reset_postdata();
    ?>
</div>
<?php get_sidebar(); //optional?>
<?php get_footer(); ?>

and replace $cat_ID and $posts_to_show to your liking. And I have used both query methods adjust it to your needs.

Hope it helps somebody who is looking for similar solution.


You can create a custom template that mimics a category page using get_posts and set a page using that template to home, but it won't be perfectly dynamic in the sense that you have to hard code the category slug or ID into that query. Assuming that you don't want to change that category often, that shouldn't be an issue. Alternatively, you could use wp_safe_redirect in a template to redirect to the category page - that would be if you want the user to be put directly on the real category page, URL and all.


I'm not sure what you mean by having your home page as a category, you mean that in your home page posts that will be displayed will be only from a certain category ?


You only need to perform a WP_Query before the loop;

$query = new WP_Query("cat=10, paged=".get_query_var('paged'));

Then use use the WP_Query object to perform the loop;

if($the_query->have_posts()):
   while($the_query->have_posts()):
      the_title();
      the_content();
      //Use all the loop function normally
   endwhile;
endif;

The paged parameter is used to determine in which page you are, if you need paginantion.

Instead of using the category id, it is good to retrieve the id by the slug.

$home = get_category_by_slug('home-category-slug');

Then your query will be like this

$the_query = new WP_Query("cat=".$home->cat_ID.", paged=".get_query_var('paged'));


Yes this is possible Go to dashboard>>Setting>>Reading>>Static page Choose page from drop down and SAVE. On that page you can create your own stuff...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜