Php 在wordpress中创建自定义模板以显示类别帖子

Php 在wordpress中创建自定义模板以显示类别帖子,php,wordpress,templates,Php,Wordpress,Templates,我试着从分类食谱中显示帖子,我发现了一个错误,我想,根据我的知识,我做得不错,下面是代码,我使用查询从分类中获取帖子,并在循环中循环显示 <?php /* Template Name: Recipes Page */ ?> <?php get_header('custom1'); ?> <?php $homepageLayout = get_theme_mod('homepageLayout', 'no-sidebar'); ?> <div c

我试着从分类食谱中显示帖子,我发现了一个错误,我想,根据我的知识,我做得不错,下面是代码,我使用查询从分类中获取帖子,并在循环中循环显示

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


基本上,类别名称用于类别slug(不是类别名称)

因此,请替换您的代码行:

    $query = new WP_Query( 'category_name=Recipes' );
为此:

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

我假设“Recipes”的类别slug应该是带有小写字母“r”的“Recipes”。

您好,谢谢您的回复,但是我在/home/alamweb/public_html/wp content/themes/medicalink theme/Recipes-page.php的第26行收到了错误
Great。很高兴听到这个消息。
    $query = new WP_Query( 'category_name=recipes' );