简体   繁体   English

如何在Wordpress中设置页面名称等于类别名称

[英]How to set the pagename equal to the category name in Wordpress

Hi I'm working on a project where I need to display a set of products on a page that are within a certain category of those products. 嗨,我正在做一个项目,我需要在页面上显示属于特定产品类别的一组产品。 I am establishing these categories by using the actual "Categories" functionality found in Wordpress. 我通过使用Wordpress中的实际“类别”功能来建立这些类别。 Then the plan is to create the actual products themselves as "Posts" that will be output put into a specific format. 然后计划是将实际产品本身创建为“帖子”,然后将其输出为特定格式。 I will assign each product (post) to a category that I have created and chosen. 我将把每个产品(帖子)分配给我创建和选择的类别。

The problem is that I will have several different pages of categories, and those pages need to call the specific categories that I assign to those pages. 问题是我将拥有几个不同的类别页面,并且这些页面需要调用我分配给这些页面的特定类别。 So I'm thinking that I could create a condition in my template that determines whether or not the "Pagename" is equal to "Categoryname". 因此,我认为我可以在模板中创建一个条件,以确定“ Pagename”是否等于“ Categoryname”。 Then if the condition is true, I would tell Wordpress to display the posts in the category that is output. 然后,如果条件为真,我将告诉Wordpress在输出的类别中显示帖子。

This is very difficult for me to wrap my mind around. 这对我来说很难全神贯注。 I have successfully been able to get the pagename and the category name as a string, shown below. 我已经能够成功获取页面名称和类别名称作为字符串,如下所示。 But I don't know how to create a way to set them equal to each other, then display the posts in the category that is output. 但是我不知道如何创建一种方法来使它们彼此相等,然后在输出的类别中显示帖子。

<?php
$pagename = get_query_var('pagename');
   if ( !$pagename && $id > 0 ) {
$post = $wp_query->get_queried_object();
$pagename = $post->post_name;
echo $pagename;
}


rewind_posts();

$page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("cat=&showposts=10&paged=$page");

    while ( have_posts() ) : the_post();

               $category = get_the_category(); 
                                  if($category[0]){
                                  echo '<p>'.$category[0]->cat_name.'</p>';
                                                  }

               get_template_part( 'content', get_post_format() );
    endwhile; 
    simplemarket_pagination();
?>

Wouldn't you just use the category.php template? 您不只是使用category.php模板吗?

I'm not sure I understand the need for having these be on Pages rather than Category Pages , per se. 我不确定我是否理解将它们放在页面而不是类别页面上的必要性。

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

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