【问题标题】:Custom Template for Wordpress PostsWordPress 帖子的自定义模板
【发布时间】:2013-12-16 08:40:24
【问题描述】:

在 WordPress 上,我在“帖子”下创建了“类别”。所以我正在做的是在他们各自的类别下添加新的“帖子”。某些类别的示例是“照片库、新闻稿、视频库”。

现在,我正在尝试从类别 slug 中显示“新闻稿”。所以我的 URL 结构将是 www.example.com/press-releases/xyz

我已经阅读了有关此的文档和其他一些博客,但似乎无法真正理解。说到做到这一点,我有点困惑。谁能帮帮我。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    可能有几种方法可以做到这一点,我想说最简单的方法是创建一个这样的模板页面:

    PHP 代码

    <?php /* Template Name: Available Lots */?>
    <?php get_header();?>
    
    <?php // The Query
          // Replace here-goes-the-slug with what you are trying to find
          query_posts( array ( 'category_name' => 'here-goes-the-slug', 'posts_per_page' => -1 ) );?>
    
    <?php if(have_posts()):?>
    <?php // The Loop
          while ( have_posts() ) : the_post();?>
    
    // Here goes the code if there is posts found
    
    <?php endwhile; ?>
    <?php else: ?>
    
    // Here goes the code if there is no posts in this category
    
    
    // This code is very important it resets the query for new use
    <?php // Reset Query
          wp_reset_query(); ?>
    
    <?php get_footer();?>
    

    这将为您创建一个模板页面,现在在您的 WP 中创建一个新页面。 随便你怎么称呼它 在右侧,您将看到“模板”下拉菜单。从此菜单中选择您刚刚创建的模板,一切顺利。

    【讨论】:

      【解决方案2】:

      试试这个代码:

      只需将“CATEGORYNAME”替换为您想要的类别名称即可:

      <?php query_posts('category_name=CATEGORYNAME&showposts=5');
      while (have_posts()) : the_post();
        // do whatever you want
      ?>
      <b><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
      <?php
      endwhile;
      ?>
      

      谢谢

      【讨论】:

        猜你喜欢
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        • 2021-12-30
        • 1970-01-01
        • 2017-11-20
        • 2013-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多