简体   繁体   English

WordPress动态更改帖子类别

[英]WordPress dynamic change of posts category

Good day, everyone! 今天是个好日子! I wonder is there a way to create a single page for all categories and dynamically change category name in WP_query depending on a clicked navigation menu item or I have to create a separate page for every single category (there are 23 of them in my case)? 我想知道是否有一种方法可以为所有类别创建一个页面,并根据单击的导航菜单项动态更改WP_query中的类别名称,或者我必须为每个单个类别创建一个单独的页面(在我的案例中有23个) ?

Menu: 菜单:

<?php   
          $args = array(
            'menu' => 'category_nav',
            'container' => 'ul',
            'container_class' => 'accordion-content',
            'container_id' => '',
            'menu_class' => 'accordion-content',
            'menu_id' => '',
            'echo' => true,
            'fallback_cb' => false,
            'before' => '',
            'after' => '',
            'link_before' => '',
            'link_after' => '',
            'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
            'item_spacing' => 'preserve',
            'depth' => 0,
            'walker' => '',
            'theme_location' => ''
          );

          wp_nav_menu($args); ?>

Posts: 帖子:

<?php
    $args = array(
        'nopaging' => true,
        'orderby' => 'name',
        'category_name' => 'HERE GOES A CATEGORY NAME',
    );

    $q = new WP_Query($args);
    if($q->have_posts()) {
        while($q->have_posts()){ $q->next_post();
            $post_id = $q->post->ID;
            $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $post_id ), 'full' );
            $title = get_the_title($post_id);
            $date = get_the_date('d.m.Y', $post_id);
            $content = get_post_field('post_content', $post_id);
            $discount = get_post_field('discount', $post_id);
            $discount_exists = get_post_meta( $post_id, 'discount', true );
            $full_description = get_post_field('full_description', $post_id);
            $full_description_exists = get_post_meta( $post_id, 'full_description', true ); ?>
      <div class="gallery-item">
          <?php echo '<div class="item-img" style="background-image:url(\'' . $thumbnail[0] . '\')"></div>'; ?>
          <div class="item-content">
            <div class="item-header"> <?php echo $title; ?> </div>
            <div class="item-desc"> <?php echo $content; ?> </div>
          </div>
          <?php 
              if ( $full_description_exists ) {
                ?><div class="btn">More</div><?php
              } ?>
        </div>
    <?php
        }
    }

    wp_reset_postdata();
    ?>

Where should I get that cateory name/ID/slug and how can I use it in my code? 我应该在哪里获得该类别名称/ ID /段,如何在我的代码中使用它? Should I create just category.php file in template folder or I should use another file structure to cusomize categories output? 我应该在模板文件夹中仅创建category.php文件,还是应该使用其他文件结构来定制类别输出?

single.php IS for all categories. single.php适用于所有类别。 Every post can use this template (except if you create template like single-$template.php) and in code you can get categories from this post by using this https://developer.wordpress.org/reference/functions/get_the_category/ 每个帖子都可以使用该模板(除非您创建诸如single- $ template.php之类的模板),并且在代码中,您可以使用以下https://developer.wordpress.org/reference/functions/get_the_category/从该帖子中获取类别。

You don't need to create every single page for category, you can create base file which could be used to display content dynamically. 您无需为类别创建每个页面,您可以创建可用于动态显示内容的基本文件。 as you can see over the template hierarchy of wordpress here for categories which is default category attached with the default post Types. 正如您可以在此处看到的wordpress模板层次结构中看到的类别一样,该类别是默认类别以及默认的post类型。

Default Category with wordpress Post Types. 具有Wordpress Post类型的默认类别。

Rendering category archive index pages uses the following path in WordPress: 呈现类别存档索引页面在WordPress中使用以下路径:

  1. category-{slug}.php – If the category's slug is news, WordPress will look for category-news.php. category- {slug} .php –如果类别的类别是新闻,则WordPress将查找category-news.php。
  2. category-{id}.php – If the category's ID is 6, WordPress will look for category-6.php. category- {id} .php –如果类别的ID为6,则WordPress将查找category-6.php。
  3. category.php category.php
  4. archive.php archive.php
  5. index.php 的index.php

Custom Taxonomy with Custom Post Types 具有自定义帖子类型的自定义分类

Custom taxonomies use a slightly different template file path: 自定义分类法使用的模板文件路径略有不同:

  1. taxonomy-{taxonomy}-{term}.php – If the taxonomy is sometax, and taxonomy's term is someterm, WordPress will look for taxonomy-sometax-someterm.php. taxonomy- {taxonomy}-{term} .php –如果分类法是sometax,而分类法的术语是someterm,则WordPress将寻找taxonomy-sometax-someterm.php。 In the case of post formats, the taxonomy is 'post_format' and the terms are 'post-format-{format}. 对于帖子格式,分类法为“ post_format”,术语为“ post-format- {format}”。 ie taxonomy-post_format-post-format-link.php for the link post format. 即taxonomy-post_format-post-format-link.php为链接发布格式。
  2. taxonomy-{taxonomy}.php – If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php. taxonomy- {taxonomy} .php –如果分类法是sometax,则WordPress会寻找taxonomy-sometax.php。
  3. taxonomy.php taxonomy.php
  4. archive.php archive.php
  5. index.php 的index.php

In your case you can use category.php for dynamic category display unless your category is not custom taxonomy else you will need to use taxonomy.php as base template. 在您的情况下,可以使用category.php进行动态类别显示,除非您的类别不是自定义分类法,否则您将需要使用taxonomy.php作为基本模板。

  • Q) Where should I get that cateory name/ID/slug and how can I use it in my code? 问)我应该从哪里获得该类别名称/ ID /段,如何在我的代码中使用它?

You can use get_query_var(); 您可以使用get_query_var(); function to achieve 功能实现

get_query_var('cat'); get_query_var('cat'); > This will return current category id. >这将返回当前类别ID。

$category = get_category(get_query_var('cat')); 

And then you can get the category object by category id so you can get the cat id, name, slug etc 然后您可以按类别ID获取类别对象,以便获取cat id, name, slug etc

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

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