【问题标题】:Wordpress Show custom post type specific category in post page?Wordpress 在帖子页面中显示自定义帖子类型特定类别?
【发布时间】:2018-07-16 04:04:55
【问题描述】:

我是开发新手。我手动创建了一个自定义帖子类型,如下面的代码所示。我无法在自定义帖子类型中获取帖子表单特定类别(徽标、网站)。帮我解决这个问题。

function my_custom_taxonomies(){
    $lables = array(
        'name' => 'Type',
        'singular_name' => 'my_custom_taxonomiesType',

        'all_items' => 'All Types',
        'add_new_item' => 'Add Type',
        'edit_item' => 'Edit Type',
        'search_item' => 'Search  Type',
        'parent_item' => 'Parent Type',
        'parent_item_colon' => 'Parent Type',
        'update_item' => 'Update Type',
        'new_item_name' => 'New Type Name',
        'menu_name' => 'Type'
    );

    $args = array(
        'labels' => $lables,
        'query_var' => ture,
        'rewrite' => array('slug' => 'type'),
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,

    );
    register_taxonomy('type', array('portfolio'), $args);
}

add_action('init','my_custom_taxonomies');

我正在尝试此代码,但无法使其正常工作。

$args = array( 'post_type' => 'portfolio', 'category_name' => 'logo', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <ul>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    </ul>

    <?php echo '<div class="entry-content">';
    //the_content();
    echo '</div>';
endwhile;?>

【问题讨论】:

  • 您能否确认您的 Category slug 名称是否为“logo”
  • 这是特定类别的网址example.com/type/logo
  • 确认一下,你能不能通过删除'category_name' =&gt; 'logo'来运行WP_Query(),首先检查你是否得到了结果
  • 如果我删除 'category_name' => 'logo' 它会显示所有帖子。我在自定义帖子类型中创建的。

标签: wordpress custom-post-type


【解决方案1】:

只需使用它,因为您已经注册了分类法。 WP_Query 可以参考here

$query = new WP_Query( array(
    'post_type' => 'portfolio',
    'tax_query' => array(
        array (
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'logo',
        )
    ),
) );

【讨论】:

  • 欢迎@Gagau :-)。也请对此进行投票,以便对其他人有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 2020-05-11
  • 1970-01-01
  • 2019-05-08
相关资源
最近更新 更多