Wordpress帖子的自定义模板

时间:2013-12-16 08:40:24

标签: php wordpress

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

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

我已经阅读了有关此内容的文档和其他一些博客,但似乎无法理解。在这样做的时候,我感到很困惑。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:2)

可能很少有方法可以做到这一点我会说简单的方法就是创建一个这样的模板页面:

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中创建一个新页面。 把它称之为你喜欢的 在右侧,您将看到“模板”下拉菜单。从这个菜单中选择你刚刚创建的这个模板,你就可以了。

答案 1 :(得分:2)

试试这段代码:

只需更换&#34; CATEGORYNAME&#34;随你的类别名称:

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

由于

相关问题