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

时间:2015-09-12 20:54:16

标签: php wordpress templates

我正在尝试显示来自食谱类别的帖子我收到错误,我认为,我已经做了正确的取决于我的知识,下面是代码,我使用查询从类别获取帖子并循环它通过循环和显示它

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

包括&#39;是否正确? $ query = new WP_Query(&#39; category_name = recipes&#39;);  &#39;里面的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 }  ?>

`

1 个答案:

答案 0 :(得分:0)

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

所以替换你的代码行:

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

有了这个:

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

我假设&#34; Recipes&#34;应该是&#34;食谱&#34;小写字母&#34; r&#34;。