简体   繁体   English

Wordpress帖子的自定义模板

[英]Custom Template for Wordpress Posts

On WordPress, I have created "Categories" under "Posts". 在WordPress上,我在“帖子”下创建了“类别”。 So what I' am doing is adding new "Post" under their respective Category. 因此,我正在做的是在各自的类别下添加新的“帖子”。 Example of some Category are "Photo Gallery, Press Releases, Video Gallery". 某些类别的示例是“图片库,新闻稿,视频库”。

Now, I' am trying to display from category slug "press-releases". 现在,我正在尝试显示“ slug-releases”类别的广告。 So my URL structure will be www.example.com/press-releases/xyz 所以我的网址结构是www.example.com/press-releases/xyz

I have already read the documentations and few other blogs regarding this but can't really seem to understand. 我已经阅读了文档和其他有关此的博客,但似乎还真不明白。 I' am abit confused when it come to doing this. 当我这样做时,我有点困惑。 Can anyone help me. 谁能帮我。

There is probably few ways to do this I would say the easy way would be to create a template-page like this: 可能有几种方法可以做到这一点,我想说的简单方法是创建一个像这样的模板页面:

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

This will create a template page for you, now create a new page in your WP. 这将为您创建一个模板页面,现在在WP中创建一个新页面。 Call it what ever you like on the right side you will see "Template" drop down menu. 随便叫什么,您都会在右侧看到“模板”下拉菜单。 From this menu select this template you have just created and you are good to go. 从此菜单中选择您刚刚创建的模板,您可以开始使用。

Try this code: 试试这个代码:

Just replace "CATEGORYNAME" with your category name whereever you want: 只需将“ 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;
?>

Thanks 谢谢

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

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