在wordpress中创建自定义模板来显示分类帖子


creating custom template in wordpress to show category posts

我试图从类别食谱中显示帖子,我得到错误,我认为,我已经根据我的知识做对了,下面是代码,我正在使用查询从类别中获取帖子,并通过循环循环并显示它

<?php /* Template Name: Recipes Page */ ?>
<?php get_header('custom1'); ?>
<?php 
$homepageLayout = get_theme_mod('homepageLayout', 'no-sidebar');
?>
<div class="container pm-containerPadding-top-110 pm-containerPadding-bottom-90">
    <div class="row">
        <?php if($homepageLayout === 'no-sidebar') { ?>
            <div class="col-lg-12 col-md-12 col-sm-12">

                <?php

$query = new WP_Query( 'category_name=Recipes' );

 if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post()); ?>
                    <?php get_template_part( 'content', 'post' ); ?>
                <?php }//end of posts ?>
                <?php } else { ?>
                     <p><?php _e('No posts were found.', 'medicallinktheme'); ?></p>
                <?php } ?> 
                <?php get_template_part( 'content', 'pagination' ); ?>
            </div>
        <?php } else if($homepageLayout === 'right-sidebar') {?>
            <!-- Retrive right sidebar post template -->
            <div class="col-lg-8 col-md-8 col-sm-12">


                <?php
$query = new WP_Query( 'category_name=Recipes' );


 if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post()); ?>
                    <?php get_template_part( 'content', 'post' ); ?>
                <?php }//end of posts ?>
                <?php } else { ?>
                     <p><?php _e('No posts were found.', 'medicallinktheme'); ?></p>
                <?php } ?> 
                <?php get_template_part( 'content', 'pagination' ); ?>
            </div>
             <!-- Right Sidebar -->
             <?php get_sidebar('home'); ?>
             <!-- /Right Sidebar -->
        <?php } else if($homepageLayout === 'left-sidebar') { ?>
             <!-- Left Sidebar -->
             <?php get_sidebar('home'); ?>
             <!-- /Left Sidebar -->
            <!-- Retrive right sidebar post template -->
            <div class="col-lg-8 col-md-8 col-sm-12">

                <?php

$query = new WP_Query( 'category_name=Recipes' );

 if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post()); ?>
                    <?php get_template_part( 'content', 'post' ); ?>
                <?php }//end of posts ?>
                <?php } else { ?>
                     <p><?php _e('No posts were found.', 'medicallinktheme'); ?></p>
                <?php } ?> 
                <?php get_template_part( 'content', 'pagination' ); ?>
            </div>
        <?php } else {//default full width layout ?>
            <div class="col-lg-12 col-md-12 col-sm-12">



                <?php
$query = new WP_Query( 'category_name=Recipes' );

 if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post()); ?>
                    <?php get_template_part( 'content', 'post' ); ?>
                <?php }//end of posts ?>
                <?php } else { ?>
                     <p><?php _e('No posts were found.', 'medicallinktheme'); ?></p>
                <?php } ?> 
                <?php get_template_part( 'content', 'pagination' ); ?>
            </div>
        <?php }  ?>
    </div> <!-- /row -->
</div> <!-- /container -->
<?php get_footer(); ?>

是否包含' $query = new WP_Query('category_name=recipes');

<?php
$query = new WP_Query( 'category_name=recipes' );

 if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post()); ?>
                    <?php get_template_part( 'content', 'post' ); ?>
                <?php }//end of posts ?>
                <?php } else { ?>
                     <p><?php _e('No posts were found.', 'medicallinktheme'); ?></p>
                <?php } ?> 
                <?php get_template_part( 'content', 'pagination' ); ?>
            </div>
        <?php }  ?>

"

基本上category_name用于类别段符(不是类别名称)

所以替换你的代码行:

    $query = new WP_Query( 'category_name=Recipes' );
与这个:

    $query = new WP_Query( 'category_name=recipes' );

我假设"食谱"的类别段符应该是"食谱"加上小写字母"r"。