简体   繁体   English

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

[英]creating custom template in wordpress to show category posts

I am trying to show posts from category Recipes I am getting error ,I think ,I have done right depending on my knowledge ,below is the code ,I am using query to get posts from categories and looping it through the loop and showing it 我正在尝试显示类别食谱中的帖子我遇到了错误,我认为,根据我的知识,我做对了,下面是代码,我正在使用查询从类别中获取帖子,并将其循环遍历并显示

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

is it right to include ' $query = new WP_Query( 'category_name=recipes' ); 是否包含'$ query = new WP_Query('category_name = recipes'); ' inside php tags the way I am doing '在php中标记我的方式

` `

<?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 }  ?>

` `

Basically category_name uses for Category slug ( not category name) 基本上category_name用于类别子弹(不是类别名称)

So replace your line of code: 因此,替换您的代码行:

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

With this: 有了这个:

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

I am assuming category slug for "Recipes" should be "recipes" with small letter "r". 我假设“食谱”的类别标签应该是带有小写字母“ r”的“食谱”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM