简体   繁体   English

Timber Twig:按类别查询自定义帖子类型并输出帖子的简短代码

[英]Timber Twig: query a Custom Post Type by category and output a shortcode of posts

What I'm trying to do is generate a new Custom Post Type called report , add a taxonomy, query the reports, and then generate a shortcode in the format of [report_listing category="general"] to display the reports in the category General. 我要执行的操作是生成一个名为report的新自定义帖子类型,添加分类法,查询报告,然后生成[report_listing category="general"]格式的简码以将报告显示在[report_listing category="general"]类别中。

Right now, on report/index.twig , I get a listing of all reports using the shortcode [report_listing] , and the error No reports available when using [report_listing category="general"] . 现在,在report/index.twig ,我得到了使用简码[report_listing]列出的所有报告,并且出现了错误,当使用[report_listing category="general"]No reports available

I am using Advanced Custom Fields, but that doesn't appear to be an issue with this or the rest of the site. 我正在使用“高级自定义字段”,但这似乎与该网站或网站的其余部分无关。 The categories are being generated and associated with the posts, since this is a sample {{ dump }} output on /index.twig for one report when using [report_listing] and all reports are displayed: 由于使用[report_listing]时这是一个报告在/index.twig上的示例{{ dump }}输出,并且正在显示所有报告,因此正在生成类别并将其与帖子相关联。

["report_category"]=> array(1) { [0]=> object(Timber\Term)#2131 (15)
{ ["PostClass"]=> string(11) "Timber\Post" ["TermClass"]=> string(4) "Term"
["object_type"]=> string(4) "term" ["_children"]=> NULL ["name"]=> string(7)
"General" ["taxonomy"]=> string(15) "report_category" ["id"]=> int(89) ["ID"]=> int(89)
["term_id"]=> int(89) ["slug"]=> string(7) "general" ["term_group"]=> int(0)
["term_taxonomy_id"]=> int(89) ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } } 

I looked at ACF Querying custom post types with taxonomies , but I am using tax_query parameters. 我看了用分类法查询ACF查询自定义帖子类型 ,但是我正在使用tax_query参数。 There are no php errors on the debug log. 调试日志上没有php错误。

The loop on index.twig : index.twig上的循环:

{% if reports %}

    {% for report in reports %}

        (html)

    {% endfor %}

    {% else %}

        No reports available.

{% endif %}

Full code in the plugin that generates the custom post type and generates the shortcode: 插件中的完整代码可生成自定义帖子类型并生成简码:

// Add a standard Custom Post Type called report

function add_report_post_type() {
    register_post_type('report',
    array(
        'labels' => array(
            'name' => __('Reports'),
            'singular_name' => __('Report'),
            'add_new' => __('Add New'),
            'add_new_item' => __('Add Report'),
            'edit' => __('Edit'),
            'edit_item' => __('Edit Report'),
            'new_item' => __('New Report'),
            'view' => __('View Report'),
            'view_item' => __('View Report'),
            'search_items' => __('Search Reports'),
            'not_found' => __('No reports found'),
            'not_found_in_trash' => __('No reports found in Trash')
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => false,
        'supports' => array(
            'title',
            'revisions'
        ),
        'can_export' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-clipboard',
        'rewrite' => array(
            'slug' => 'report',
            'with_front' => false,
            'hierarchical' => true
        ),
        )
    );
}
add_action('init', 'add_report_post_type');


// Add categories

function reports_taxonomy() {
    $labels = array(
        'name'                       => _x( 'Report Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Report Categories', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Categories', 'text_domain' ),
        'all_items'                  => __( 'All Items', 'text_domain' ),
        'parent_item'                => __( 'Parent Item', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
        'new_item_name'              => __( 'New Item Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Item', 'text_domain' ),
        'edit_item'                  => __( 'Edit Item', 'text_domain' ),
        'update_item'                => __( 'Update Item', 'text_domain' ),
        'view_item'                  => __( 'View Item', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular Items', 'text_domain' ),
        'search_items'               => __( 'Search Items', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No items', 'text_domain' ),
        'items_list'                 => __( 'Items list', 'text_domain' ),
        'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
    );
}


function create_report_taxonomy () {
register_taxonomy(
        'report_category', 'report',
        array(
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'meta_box_cb'                   => false,
            )
    );
}
add_action( 'init', 'create_report_taxonomy', 2 );



// Query all reports and create shortcode

function report_get_listing($params) {
    $reports = [];
    $context = Timber::get_context();

    $args = array(
        'post_type' => 'report',
        'orderby' => [
            'weight' => 'ASC',
            'post_date' => 'DESC'
        ],
        'post_status' => 'publish',
        'meta_query' => [
            'weight' => [
                'key'     => 'weight',
                'compare' => 'EXISTS',
                'type'    => 'NUMERIC'
            ],

        ],
    );


// Check for a report category in the query

  if (!empty($params['category'])) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'report_category',
                'terms' => explode(',', $params['category']),
                'field' => 'slug',
                'operator' => 'IN',
            ),
        );
    }

    query_posts($args);

    $report_listing = Timber::get_posts($args);

    foreach ($report_listing as $p) {
        $reports[] = get_single_report($p);
    }

    $context['reports'] = $reports;

    return Timber::compile('report/index.twig', $context);
}


// Build all the reports for the shortcode

function get_single_report($post) {
    $report = [
        'id' => $post->ID,
        'link' => $post->link,
        'title' => $post->title(),
        'date' => $post->post_date,
        'summary' => $post->get_field('summary'),
        'report_category' => $post->get_field('report_category'),
        'document' => $post->get_field('document'),
        'image' => $post->get_field('image'),
        'url' => $post->get_field('url')
    ];
    return $report;
}
add_shortcode('report_listing', 'report_get_listing');

结果发现这是一个奇怪的插件冲突:(。。我发现使用查询监视器。问题中的上述示例代码正常工作。

From reading your current question and code, I can see a few issues. 通过阅读您当前的问题和代码,我可以看到一些问题。 But I'd like to address the shortcode function which is what I think is causing the reports to not get listed by category. 但我想解决一下短代码功能,这是我认为导致报告未按类别列出的原因。

// Query all reports and create shortcode

function report_get_listing($params) {
    $reports = [];

   $a = shortcode_atts( array(
    'category' => '', // [report_listing category=""]
   ), $params );

    $context = Timber::get_context();

    $args = array(
        'post_type' => 'report',
        'orderby' => [
            'weight' => 'ASC',
            'post_date' => 'DESC'
        ],
        'post_status' => 'publish',
        'meta_query' => [
            'weight' => [
                'key'     => 'weight',
                'compare' => 'EXISTS',
                'type'    => 'NUMERIC'
            ],

        ],
    );


  // Check for a report category in the query

  if (!empty($a['category'])) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'report_category',
                'terms' => explode(',', $a['category']),
                'field' => 'slug',
                'operator' => 'IN',
            ),
        );
    }

    query_posts($args);

    $report_listing = Timber::get_posts($args);

    foreach ($report_listing as $p) {
        $reports[] = get_single_report($p);
    }

    $context['reports'] = $reports;

    return Timber::compile('report/index.twig', $context);
}

add_shortcode('report_listing', 'report_get_listing');

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

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