在存档页面上的帖子中按插件获取分类术语

时间:2020-01-15 作者:Sam

我注册了一个名为“项目”的自定义帖子类型,并分配了一个名为“规程”的分类法。下面是它的外观:

<?php
// Register projects cpt
function projects() {
    $labels = array(
        \'name\'                  => _x( \'Projects\', \'Post Type General Name\', \'sitename\' ),
        \'singular_name\'         => _x( \'Project\', \'Post Type Singular Name\', \'sitename\' ),
        \'menu_name\'             => __( \'Projects\', \'sitename\' ),
        \'name_admin_bar\'        => __( \'Projects\', \'sitename\' ),
        \'archives\'              => __( \'Item Archives\', \'sitename\' ),
        \'attributes\'            => __( \'Item Attributes\', \'sitename\' ),
        \'parent_item_colon\'     => __( \'Parent Item:\', \'sitename\' ),
        \'all_items\'             => __( \'All Items\', \'sitename\' ),
        \'add_new_item\'          => __( \'Add New Item\', \'sitename\' ),
        \'add_new\'               => __( \'Add New\', \'sitename\' ),
        \'new_item\'              => __( \'New Item\', \'sitename\' ),
        \'edit_item\'             => __( \'Edit Item\', \'sitename\' ),
        \'update_item\'           => __( \'Update Item\', \'sitename\' ),
        \'view_item\'             => __( \'View Item\', \'sitename\' ),
        \'view_items\'            => __( \'View Items\', \'sitename\' ),
        \'search_items\'          => __( \'Search Item\', \'sitename\' ),
        \'not_found\'             => __( \'Not found\', \'sitename\' ),
        \'not_found_in_trash\'    => __( \'Not found in Trash\', \'sitename\' ),
        \'featured_image\'        => __( \'Featured Image\', \'sitename\' ),
        \'set_featured_image\'    => __( \'Set featured image\', \'sitename\' ),
        \'remove_featured_image\' => __( \'Remove featured image\', \'sitename\' ),
        \'use_featured_image\'    => __( \'Use as featured image\', \'sitename\' ),
        \'insert_into_item\'      => __( \'Insert into item\', \'sitename\' ),
        \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'sitename\' ),
        \'items_list\'            => __( \'Items list\', \'sitename\' ),
        \'items_list_navigation\' => __( \'Items list navigation\', \'sitename\' ),
        \'filter_items_list\'     => __( \'Filter items list\', \'sitename\' ),
    );
    $args = array(
        \'label\'                 => __( \'Projects\', \'sitename\' ),
        \'description\'           => __( \'A custom post type for projects.\', \'sitename\' ),
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\' ),
        \'taxonomies\'            => array( \'disciplines\' ),
        \'hierarchical\'          => false,
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 5,
        \'show_in_admin_bar\'     => true,
        \'show_in_nav_menus\'     => true,
        \'can_export\'            => true,
        \'has_archive\'           => true,
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'capability_type\'       => \'page\',
        \'menu_icon\'             => \'dashicons-admin-appearance\',
        \'show_in_rest\'          => true,
    );
    register_post_type( \'projects\', $args );

}
add_action( \'init\', \'projects\', 0 );

// Register disciplines taxonomy
function disciplines() {

    $labels = array(
        \'name\'                       => _x( \'Disciplines\', \'Taxonomy General Name\', \'sitename\' ),
        \'singular_name\'              => _x( \'Discipline\', \'Taxonomy Singular Name\', \'sitename\' ),
        \'menu_name\'                  => __( \'Disciplines\', \'sitename\' ),
        \'all_items\'                  => __( \'All Items\', \'sitename\' ),
        \'parent_item\'                => __( \'Parent Item\', \'sitename\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'sitename\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'sitename\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'sitename\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'sitename\' ),
        \'update_item\'                => __( \'Update Item\', \'sitename\' ),
        \'view_item\'                  => __( \'View Item\', \'sitename\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'sitename\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'sitename\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used\', \'sitename\' ),
        \'popular_items\'              => __( \'Popular Items\', \'sitename\' ),
        \'search_items\'               => __( \'Search Items\', \'sitename\' ),
        \'not_found\'                  => __( \'Not Found\', \'sitename\' ),
        \'no_terms\'                   => __( \'No items\', \'sitename\' ),
        \'items_list\'                 => __( \'Items list\', \'sitename\' ),
        \'items_list_navigation\'      => __( \'Items list navigation\', \'sitename\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'show_in_rest\'               => true,
    );
    register_taxonomy( \'disciplines\', array( \'projects\' ), $args );

}
add_action( \'init\', \'disciplines\', 0 );
然后我创建了一个archive-projects.php 页面,如下图所示:

<?php
/**
 * The template for displaying archive pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package sitename
 */

get_header();
?>
<div class="has-sidebar">

    <div id="primary" class="content-area">

        <main id="main" class="site-main">

            <article class="navy">

                <?php if ( have_posts() ) : ?>

                    <header class="entry-header screen-reader-text">
                        <h1>Projects</h1>
                    </header><!-- .page-header -->

                    <div class="button-group filters-button-group narrow" id="filters">
                        <button class="button is-checked" data-filter="*">All</button>

                        <?php
                            // Loop through categories - https://developer.wordpress.org/reference/functions/get_categories
                            $categories = get_categories( array(
                                \'taxonomy\' => \'disciplines\',
                                \'orderby\'  => \'name\',
                                \'order\'    => \'ASC\'
                            ) );
                            foreach( $categories as $category ) {
                                echo \'<button class="button" data-filter=".\'.$category->slug.\'">\';
                                echo $category->name;
                                echo \'</button>\';
                            }
                        ?>

                    </div>

                    <div class="entry-content filter" id="images">

                        <div class="gutter-sizer"></div>

                        <?php
                            // Loop through posts
                            while ( have_posts() ) :
                            the_post();

                            // Get post ID - Important for pulling ACF fields from single posts
                            $post_id = get_the_ID();

                            // Get post terms
                            $terms = wp_get_post_terms( $post_id, \'disciplines\' );

                        ?>

                            <a href="<?php the_permalink(); ?>" class="filter-item <?= $terms->slug; ?>" data-category="<?= $terms->slug; ?>">

                                <figure>
                                    <?php
                                        if ( get_field(\'thumbnail_image\', $post_id) ) {
                                            echo wp_get_attachment_image( get_field(\'thumbnail_image\', $post_id), \'medium-square\');
                                        }
                                        else {
                                            echo wp_get_attachment_image( get_field(\'hero_image\', $post_id), \'medium-square\');
                                        }
                                    ?> 
                                </figure>

                                <h2><?php the_title(); ?></h2>

                            </a>

                        <?php
                            endwhile;
                        ?>

                    </div><!-- .entry-content -->

                    <?php
                        else:
                            echo \'<p>Please add some projects.</p>\';
                        endif;
                    ?>

            </article>

        </main><!-- #main -->

    </div><!-- #primary -->

    <?php get_sidebar(); ?>

</div><!-- .has-sidebar -->

<?php get_footer(); ?>
我试图通过slug为每个post类和数据属性获取分类术语,但我使用的方法不起作用。我跟踪了this documentation 作为指导。

我做错了什么?

1 个回复
最合适的回答,由SO网友:Amine Faiz 整理而成

在代码中,函数wp\\u get\\u post\\u terms()返回wp\\u Term对象的数组,因此如果不为数组指定索引,则无法直接访问对象属性,如:

$terms[0]->slug
否则,您应该遍历整个术语数组,并将slug检索到另一个数组中,如:

 $terms = wp_get_post_terms( $post_id, \'disciplines\' );
    $terms_slug = [];
    foreach ($terms as $term) {
      $terms_slug[] = $term->slug;
    }
最后,您可以将所有收集的slug内爆到您的class属性中:

class="filter-item <?= implode(\' \', $terms_slug); ?>"

相关推荐

在unctions.php文件中获取每个帖子的帖子术语‘wp_get_POST_Terms’

我使用了一个带有Ajax操作的表单来在提交时获取帖子信息。它很有魅力。然而,我使用了类别来将工作划分为不同的类别。其中之一就是品牌。在页面模板中,我使用的脚本有效,但在函数中使用时有效。php文件。它无法获得所需的结果。我认为这可能与何时触发对帖子的查询或如何设置add\\u操作有关。有人能帮我查一下ID为31的类别的名称吗?在函数中使用时。php。以下是我写的:if( $query->have_posts() ) : while( $query->have_posts() )