dongqiao1888 2017-10-02 13:44
浏览 80
已采纳

WordPress动态更改帖子类别

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

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? Should I create just category.php file in template folder or I should use another file structure to cusomize categories output?

  • 写回答

2条回答 默认 最新

  • dpb42021 2017-10-02 14:12
    关注

    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.

    Default Category with wordpress Post Types.

    Rendering category archive index pages uses the following path in WordPress:

    1. category-{slug}.php – If the category’s slug is news, WordPress will look for category-news.php.
    2. category-{id}.php – If the category’s ID is 6, WordPress will look for category-6.php.
    3. category.php
    4. archive.php
    5. 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. In the case of post formats, the taxonomy is ‘post_format’ and the terms are ‘post-format-{format}. i.e. taxonomy-post_format-post-format-link.php for the link post format.
    2. taxonomy-{taxonomy}.php – If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php.
    3. taxonomy.php
    4. archive.php
    5. 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.

    • Q) Where should I get that cateory name/ID/slug and how can I use it in my code?

    You can use get_query_var(); function to achieve

    get_query_var('cat'); > This will return current category 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。