如何从URL获取类别名称并传递给模板

时间:2014-01-02 16:29:27

标签: wordpress categories

我正在使用以下页面模板在单个类别中显示帖子,并计划将第一个帖子的格式设置为与其他帖子不同。这可以按照需要工作,但我在模板中硬编码了category_name。我想将此模板用于几个不同的类别,并希望了解如何通过链接将category_name传递给模板。

例如,使用特殊模板指向所需页面的链接为http://wildcatweb.net/5th/,而“5th”也是category_name。如何告诉模板从URL获取category_name并在模板中使用它?

<?php
/*
Template Name: pageAssignments
*/ ?>

<?php get_header(); ?>

<div class="small-12 large-8 columns" id="content" role="main">
<header>
                <h1 class="entry-title"><?php the_title(); ?></h1>

        </header>

  <!-- show latest post FULL -->

 <?php query_posts('showposts=1&category_name=5th'); ?>

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

    <div class="lastest-post">



     <h2 class="pagetitle"><?php the_title(); ?></h2>

                        <?php the_content(); ?>

</div><!--close .latest-post -->

 <?php endwhile; endif; ?><!-- end lastest post -->

 <!-- show older post excerpts -->

<?php query_posts('showposts=5&offset=1&category_name=5th'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="older-post">

         <h3 class="pagetitle"><a href="<?php the_permalink() ?>"     rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        <?php the_excerpt(); ?>

    </div><!--.older-post -->

<?php endwhile; endif; ?><!-- end past-entry -->

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:0)

试试这个:

$current_url = $_SERVER['PHP_SELF'];

$category_name = explode ('',$current_url);
$category_name = $category_name['3'];
query_posts('showposts=1&category_name='. $category_name);