【问题标题】:Displaying a custom post from a custom taxonomy显示自定义分类中的自定义帖子
【发布时间】:2016-06-05 18:29:13
【问题描述】:

我做了一个自定义分类:

function project_register_taxonomy(){

$singular = 'Project Categories';
$plural = 'Projects Categories';
$slug = str_replace( ' ', '_', strtolower( $singular ) );
$labels = array(
        'name'                       => $plural,
        'singular_name'              => $singular,
        'search_items'               => 'Search ' . $plural,
        'popular_items'              => 'Popular ' . $plural,
        'all_items'                  => 'All ' . $plural,
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => 'Edit ' . $singular,
        'update_item'                => 'Update ' . $singular,
        'add_new_item'               => 'Add New ' . $singular,
        'new_item_name'              => 'New ' . $singular . ' Name',
        'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
        'add_or_remove_items'        => 'Add or remove ' . $plural,
        'choose_from_most_used'      => 'Choose from the most used ' . $plural,
        'not_found'                  => 'No ' . $plural . ' found.',
        'menu_name'                  => $plural,
);
$args = array(
        'hierarchical'          => true,
        'labels'                => $labels,
        'show_ui'               => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        'rewrite'               => array( 'slug' => $$slug ),
);

register_taxonomy ('Project Categories','projects', $args);
}

add_action( 'init', 'project_register_taxonomy');

并将其注册到自定义帖子:

function create_galblock() {

register_post_type( 'projects',
        // CPT Options
        array(
                'labels' => array(
                        'name' => __( 'Projects' ),
                        'singular_name' => __( 'Project' )
                ),

                'taxonomies' => array('category'),

                'description' => 'Projects by Almog',
                'public' => true,
                'has_archive' => true,
                'rewrite' => array('slug' => 'projects'),
                'supports' => array( 'title','editor', 'thumbnail')
        )
);
}

add_action( 'init', 'create_galblock' );

我需要获取自定义分类“项目类别”的自定义帖子“项目”的帖子标题、帖子缩略图和内容。

这是我尝试至少实现内容的代码:

$args = array(
'post_type' => 'projects',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
    array(
        'taxonomy' => 'Projects Categories',
        'field' => 'id',
        'terms' => '8'
    )
)
 );
 $the_query = new WP_Query( $args );
 while ( $the_query->have_posts() ) : $the_query->the_post();
 //content
 endwhile;
 ?>

显然它不会从自定义分类中返回自定义帖子。

【问题讨论】:

  • taxonomy 应该是一个蛞蝓。所以在你的情况下projects,如果我没记错的话。您还想在哪里显示此查询?
  • 你的意思是,怎么做?就像 nano 说的那样,$taxonomy 是 project_categories ,据我所知
  • 是的,就像 nano 建议的 register_taxonomy ('project_categories','projects', $args); 然后就像他建议的那样应该可以工作。
  • 不幸的是没有

标签: php wordpress custom-taxonomy


【解决方案1】:

好的,所以我得到了这个工作:

function project_register_taxonomy(){

    $singular = 'Project Categories';
    $plural = 'Projects Categories';
    $slug = str_replace( ' ', '_', strtolower( $singular ) );
    $labels = array(
            'name'                       => $plural,
            'singular_name'              => $singular,
            'search_items'               => 'Search ' . $plural,
            'popular_items'              => 'Popular ' . $plural,
            'all_items'                  => 'All ' . $plural,
            'parent_item'                => null,
            'parent_item_colon'          => null,
            'edit_item'                  => 'Edit ' . $singular,
            'update_item'                => 'Update ' . $singular,
            'add_new_item'               => 'Add New ' . $singular,
            'new_item_name'              => 'New ' . $singular . ' Name',
            'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
            'add_or_remove_items'        => 'Add or remove ' . $plural,
            'choose_from_most_used'      => 'Choose from the most used ' . $plural,
            'not_found'                  => 'No ' . $plural . ' found.',
            'menu_name'                  => $plural,
    );
    $args = array(
            'hierarchical'          => true,
            'labels'                => $labels,
            'show_ui'               => true,
            'show_admin_column'     => true,
            'update_count_callback' => '_update_post_term_count',
            'query_var'             => true,
            'rewrite'               => array( 'slug' => $slug ),
    );

    register_taxonomy ('project_categories','projects', $args);
}

add_action( 'init', 'project_register_taxonomy');


function create_galblock() {

    register_post_type( 'projects',
            // CPT Options
            array(
                    'labels' => array(
                            'name' => __( 'Projects' ),
                            'singular_name' => __( 'Project' )
                    ),

                    'taxonomies' => array('category'),

                    'description' => 'Projects by Almog',
                    'public' => true,
                    'has_archive' => true,
                    'rewrite' => array('slug' => 'projects'),
                    'supports' => array( 'title','editor', 'thumbnail')
            )
    );
}

add_action( 'init', 'create_galblock' );

我使用page-projects.php 模板创建了一个测试页面,看起来像这样

<?php
/**
 * The template for displaying project pages
 * Template Name: Projects Page
 */

get_header(); ?>

<div id="primary" class="content-area">
<h3>Projects</h3>
    <main id="main" class="site-main" role="main">
        <?php

        $args = array(
            'post_type'      => 'projects',
            'post_status'    => 'publish',
            'posts_per_page' => -1,
            'tax_query'      => array(
                array(
                    'taxonomy' => 'project_categories',
                    'field'    => 'term_taxonomy_id',
                    'terms'    => 61,
                )
            )
        );

        $the_query = new WP_Query( $args );

        while ( $the_query->have_posts() ) :
            $the_query->the_post();

            echo the_title();

        endwhile;
        ?>

    </main><!-- .site-main -->

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

<?php get_footer(); ?>

现在我添加了一个测试项目类别,其中包含tag_id 61(当您将鼠标悬停在它上面时,您可以看到它),以及该类别中的一个测试帖子。

它显示该帖子很好。现在,如果您在tax_query 中省略了fieldsterms,这将不起作用。但我会先把它放在一边,然后把所有的项目都拿出来(然后再弄清楚排序)。

希望这会有所帮助。

【讨论】:

  • 我居然搞定了,term_taxonomy_id 和 term_id 有什么区别?
  • 嗯,我认为这与术语和分类法在 WordPress 数据库中的编写方式有关。 term_id 位于术语表中,term_taxonomy_id 链接分类法和术语(具有 1-1 对应关系)。
  • 如果我有一个与两个类别相关联的帖子,并且我想在两个页面中都显示它怎么办? (每个页面都有一个类别的模板)
  • 如果它与两者相关联,它应该出现在两个页面上,就像常规类别一样。
【解决方案2】:

这是来自documentation

$taxonomy:(字符串)(必需)分类的名称。名称应仅包含小写字母和下划线字符,且长度不得超过 32 个字符(数据库结构限制)。

您指定了项目类别,正如您在上面看到的那样,这并不好。

register_taxonomy ('project_categories','projects', $args);

更新:

您还必须修改您的查询:

$args = array(
    'post_type' => 'projects',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'projects_categories',
            'field' => 'term_id',
            'terms' => '8'
        )
    )
);

注意tax_query的变化:

taxonomy 应该是projects_categories

field 应该是term_id

【讨论】:

  • 无显示...我想要做的是显示自定义分类“projects_categories”中名为“Renovation”的类别中的自定义帖子项目,它的 ID 为 8,我的代码是否使用你的修正达到了吗?
  • 我从底部控制台得到了 id=8,它说 tag_id=8,我想这就是问题所在?
  • 非常感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 2016-05-07
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多