dplm47571 2016-01-21 22:23
浏览 19
已采纳

当类别中没有帖子时,让Wordpress显示消息

I am trying to get my blog to show a message to visitors when they visit the category main page (ex: www.website.com/category-slug-here/), but can't seem to figure out the Wordpress Loop quite right. Here is what I started with in my theme's index.php file:

get_header(); ?>

<div id="page-wrap" class="blog-page blog-fullwidth container">

    <div id="content" class="blog-wrap <?php echo esc_attr($sidebarlayout); ?> columns">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <?php get_template_part( 'framework/inc/post-format/entry', get_post_format() ); ?>

        <?php endwhile; endif; ?>

        <?php get_template_part( 'framework/inc/nav' ); ?>

    </div>

    <?php if($sidebar != 'no-sidebar'){ ?>
    <div id="sidebar" class="<?php echo esc_attr($sidebarorientation); ?> alt">
        <?php get_sidebar(); ?>
    </div>
    <?php } ?>

</div>

<?php get_footer(); ?>

And here is how I tried to alter it:

get_header(); ?>

<div id="page-wrap" class="blog-page blog-fullwidth container">

    <div id="content" class="blog-wrap <?php echo esc_attr($sidebarlayout); ?> columns">

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <?php get_template_part( 'framework/inc/post-format/entry', get_post_format() ); ?>

        <?php endwhile; else: ?>
        <p>Sorry, looks like this category is empty!</p>

        <?php endif; ?>

        <?php get_template_part( 'framework/inc/nav' ); ?>

    </div>

    <?php if($sidebar != 'no-sidebar'){ ?>
    <div id="sidebar" class="<?php echo esc_attr($sidebarorientation); ?> alt">
        <?php get_sidebar(); ?>
    </div>
    <?php } ?>

</div>

<?php get_footer(); ?>

However, making this change doesn't seem to yield any results. Am I missing something here? Seems like this should work (but I'm also not a total PHP guru, which is why I came here to consult with the masses). ;)

  • 写回答

1条回答 默认 最新

  • dongpo5207 2016-01-22 04:36
    关注

    In wordpress the posts in category are shown in archive.php page .

    Try to write the else condition in archive page instead of index.php page

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算