【问题标题】:Make WordPress homepage a post category?将 WordPress 主页设为帖子类别?
【发布时间】:2014-10-27 00:16:02
【问题描述】:

我正在尝试将我的 WordPress 主页设置为一个类别,但它只允许我将其设置为最新帖子或静态页面。

是否可以将您的主页设置为帖子类别?

【问题讨论】:

  • 请注意,有一个新网站只专注于 Wordpress,这让我和 StackOverflow 的人一样。 wordpress.stackexchange.com你会在这里得到很好的答案。

标签: wordpress categories


【解决方案1】:

我希望您知道如何设置静态页面。因此,首先创建一个空的 .php 文件,并将其命名为您喜欢的任何名称,并将其放在其他文件(index.php、arhive.php 等)中。

然后输入以下代码

    <?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(); ?>

并根据自己的喜好替换 $cat_ID 和 $posts_to_show。我已经使用了这两种查询方法来调整它以适应您的需求。

希望它对正在寻找类似解决方案的人有所帮助。

【讨论】:

    【解决方案2】:

    您可以使用get_posts 创建一个模仿类别页面的自定义模板,并将使用该模板的页面设置为主页,但在您必须对类别标签或 ID 进行硬编码的意义上,它不会是完全动态的进入该查询。假设您不想经常更改该类别,那应该不是问题。或者,您可以在模板中使用 wp_safe_redirect 重定向到类别页面 - 如果您希望用户直接进入真正的类别页面、URL 和所有页面。

    【讨论】:

      【解决方案3】:

      我不确定您将主页作为一个类别是什么意思,您的意思是在您的主页中显示的帖子将仅来自某个类别?

      【讨论】:

        【解决方案4】:

        你只需要在循环之前执行一个WP_Query;

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

        然后使用 WP_Query 对象执行循环;

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

        paged参数用于判断你在哪个页面,如果需要分页。

        不要使用类别id,而是通过slug检索id。

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

        那么你的查询会是这样的

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

        【讨论】:

          【解决方案5】:

          是的,这是可能的 转到仪表板>>设置>>阅读>>静态页面从下拉列表中选择页面并保存。在该页面上,您可以创建自己的东西...

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-02-23
            • 1970-01-01
            • 1970-01-01
            • 2011-08-17
            • 1970-01-01
            相关资源
            最近更新 更多