【问题标题】:Getting pages list by taxonomy in wordpress custom post在wordpress自定义帖子中按分类获取页面列表
【发布时间】:2019-10-31 11:06:15
【问题描述】:

我有用于不同页面的条款和条件。现在我想根据条款和条件列出页面。所以我想获取特定条款和条件(分类)的页面。需要您的帮助。我浏览了一些博客,我明白了。

$product_page_args = array(
                        'post_type' => 'page',
                        'order' => 'ASC',
                        'orderby' => 'menu_order',
                        'child_of' => $post_id,
                        'taxonomy' => 'term-condition',
                        'field' => 'slug',
                        'term' => 'ID'
                    );

                    $product_pages = new WP_Query($product_page_args);
                    foreach ($product_pages as $product_page){  
                        echo $product_page->post_title;
                    }

【问题讨论】:

  • 您在$product_page 中得到了什么?

标签: php wordpress custom-post-type


【解决方案1】:

我将分享我的代码以获得非常相似的内容。我的代码列出了分类模板上给定分类的所有页面:website.com/{taxonomy-slug}/{term-slug}

$taxonomy = get_query_var( 'taxonomy' );
$tax_term = get_query_var( 'term' );

// get all post types for tax term
$posts_list = new WP_Query(array(
    'post_status' => 'publish',
    'post_type' => 'page',
    'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'slug',
            'terms' => $tax_term,
        ),
    ),
    'fields' => 'ids',
    'posts_per_page' => -1,
    'nopaging' => true
));

在你的情况下,这将是这样的:

$posts_list = new WP_Query(array(
    'post_status' => 'publish',
    'post_type' => 'page',
    'tax_query' => array(
        array(
            'taxonomy' => 'term-condition',
            'field' => 'slug',
            'terms' => 'ID',
        ),
    ),
    'fields' => 'ids',
    'posts_per_page' => -1,
    'nopaging' => true
));

鉴于“ID”是分类“term-condition”的一个术语。如果是这样,我会重新考虑这个名称,因为它很容易与 ID 作为帖子对象的属性混淆。

希望对你有帮助。

【讨论】:

  • 我是 wordpress 新手。所以我对这些术语有点困惑,它会包含值还是字段名?
  • 抱歉我的回复晚了。 $taxonomy 将包含名称,$tax_term 将包含值。举个例子; $taxonomy 可以是“工业”,$tax_term 可以是“农业”
猜你喜欢
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
相关资源
最近更新 更多