簡體   English   中英

如何根據 Wordpress 中的自定義分類過濾歸檔帖子

[英]How to filter archive posts based on custom taxonomy in Wordpress

我使用自定義分類創建了一個自定義帖子類型。 為此,我在functions.php中使用了以下代碼。php

//Custom Posttype
function custom_post_type_rental() {
    // source: https://wordpress.stackexchange.com/questions/156978/custom-post-type-single-page-returns-404-error
        $labels = array(
            'name'               => _x( 'Rentals', 'i2sa'),
            'singular_name'      => _x( 'rentals', 'i2sa'),
            'menu_name'          => _x( 'Rentals', 'i2sa'),
            'name_admin_bar'     => _x( 'Admin Bar', 'i2sa'),
            'add_new'            => _x( 'Nieuwe rental', 'i2sa'),
            'add_new_item'       => __( 'Voeg nieuwe rental toe', 'i2sa'),
            'new_item'           => __( 'Nieuw item', 'i2sa'),
            'edit_item'          => __( 'Bewerk rentals', 'i2sa'),
            'view_item'          => __( 'Bekijk rentals', 'i2sa'),
            'all_items'          => __( 'Alle rentals', 'i2sa'),
            'search_items'       => __( 'Zoek rentals', 'i2sa'),
            'parent_item_colon'  => __( 'Parent rentals:', 'i2sa'),
            'not_found'          => __( 'Zoekopdracht niet gevonden.', 'i2sa'),
            'not_found_in_trash' => __( 'Zoekopdracht niet gevonden in prullenbak.', 'i2sa')
        );
    
        $args = array(
            'labels'             => $labels,
            'description'        => "Beschrijving i2sa",
            'public'             => true, //openbaar
            'exclude_from_search'=> true, //uitschakelen voor zoekopdrachten
            'publicly_queryable' => true, //publiekelijk vindbaar
            'show_in_nav_menus'  => false, //toon in navigatie
            'menu_icon'          => 'dashicons-bank',
            'show_ui'            => true,
            'show_in_rest'       => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'rental' ), //custom url
            'capability_type'    => 'post',
            'has_archive'        => true, //heeft archivepage
            'hierarchical'       => false, // true maakt het een pagina
            'menu_position'      => null, //null = geen
            'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'author', 'trackbacks', 'comments', 'revisions', 'post-formats', 'page-attributes' ), //gesupporte functionaliteiten
            //je kan de support 'thumbnail' ook vervangen door ‘attachment:audio’ and ‘attachment:video’
        );
        register_post_type( 'rental', $args );
         //flush_rewrite_rules();
    }
add_action( 'init', 'custom_post_type_rental' );

function rentalMachineSoort() {
 
    
    //Labels voor de custom taxonomy
     $labels = array(
       'name' => _x( 'Machinesoorten', 'machinesoorten' ),
       'singular_name' => _x( 'Machinesoort', 'machinesoort' ),
       'search_items' =>  __( 'Search Machinesoorten' ),
       'all_items' => __( 'All Machinesoorten' ),
       'parent_item' => __( 'Parent Machinesoort' ),
       'parent_item_colon' => __( 'Parent Machinesoort:' ),
       'edit_item' => __( 'Edit Machinesoort' ), 
       'update_item' => __( 'Update Machinesoort' ),
       'add_new_item' => __( 'Add New Machinesoort' ),
       'new_item_name' => __( 'New Machinesoort Name' ),
       'menu_name' => __( 'Machinesoorten' ),
     );    
    
   // Taxonomy registreren
     register_taxonomy('machinesoort',array('rental'), array(
       'hierarchical' => true,
       'labels' => $labels,
       'show_ui' => true,
       'show_in_rest' => true,
       'show_admin_column' => true,
       'query_var' => true,
       'rewrite' => array( 'slug' => 'machinesoort' ),
     ));
    
   }
   add_action( 'init', 'rentalMachineSoort', 0 );

在我的檔案中,我想按此分類過濾帖子。 為此,我使用了代碼:

<form method="GET">
                            <select name="orderby" id="orderby">
                                <?php 
                                    $terms = get_terms([
                                        'taxonomy' => 'machinesoort',
                                        'hide_empty' => 'false'
                                    ]);
                                    foreach ($terms as $term) :
                                ?>

                                <option value="<?php echo $term->slug;?>" name="machinesoort[]"><?php echo $term->name;?></option>

                                <?php endforeach;?>
                            </select>
                            <button type="submit">Filter</button>

這會使用包含參數的新 url 刷新頁面。 問題是頁面仍然顯示所有文章,包括不應該顯示的文章。

任何人都知道我怎樣才能使這段代碼工作,所以它過濾正確嗎? 如果我只使用 checkboxxes 它工作正常,但我需要在這個過濾器中使用方法

“問題是頁面仍然顯示所有文章,包括不應該顯示的文章。”

因為你錯過了主要步驟! 您尚未根據所選值更改查詢!

你可以使用pre_get_posts鈎子來做到這一點。 像這樣:

add_action('pre_get_posts', 'altering_rental_archive_query', 99);

function altering_rental_archive_query($query)
{
    if (
        is_post_type_archive('rental') 
        && 
        get_query_var('orderby')
       ) 
    {

        $tax_query = array(
            array(
                'taxonomy' => 'machinesoort',
                'field' => 'slug',
                'terms' => sanitize_text_field(get_query_var('orderby')),
            )
        );
        $query->set('tax_query', $tax_query);
    };
};

結果如下:

當存檔頁面第一次加載時沒有任何過濾器/查詢變量:

在此處輸入圖像描述


如果我按第一個自定義分類對其進行過濾:

在此處輸入圖像描述


如果我按第二個自定義分類對其進行過濾:

在此處輸入圖像描述


一個快速提示/獎金:

您可以將selected文檔function 添加到archive頁面上的表單中,以便根據 url 上的查詢變量正確選擇過濾器下拉列表:

<form method='GET'>
  <select name='orderby' id='orderby'>
    <?php
    $terms = get_terms([
      'taxonomy' => 'machinesoort',
      'hide_empty' => 'false'
    ]);
    foreach ($terms as $term) :
    ?>

      <option value='<?php echo $term->slug; ?>' <?php echo selected(sanitize_text_field($_GET['orderby']), $term->slug); ?>><?php echo $term->name; ?></option>

    <?php endforeach; ?>
  </select>
  <button type='submit'>Filter</button>
</form>

您的問題中還缺少一個結束表單標簽! </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM