【问题标题】:How to target custom taxonomy in Wordpress如何在 Wordpress 中定位自定义分类法
【发布时间】:2013-05-23 12:53:34
【问题描述】:

我有一个插件,可以创建自定义分类来显示投资组合。在管理面板上悬停编辑类别时,我看到如下链接:

localhost/site/wp-admin/edit-tags.php?action=edit&taxonomy=mg_item_categories&tag_id=13&post_type=mg_items

如果产品显示来自特定类别,我想做的是添加一些 CSS(功能区图像)。例如,如果显示类别“新”的产品,它都显示“新功能区图像”所以我很困惑如何定位分类?从上面的链接有什么想法吗?

我应该使用is_post_typeis_taxonomy 还是任何其他有助于定位分类 ID 13 的东西。

这是构成此分类的函数:

function register_cpt_mg_item() {
$labels = array( 
        'name' => _x( 'Item Categories', 'mg_item_categories' ),
        'singular_name' => _x( 'Item Category', 'mg_item_categories' ),
        'search_items' => _x( 'Search Item Categories', 'mg_item_categories' ),
        'popular_items' => NULL,
        'all_items' => _x( 'All Item Categories', 'mg_item_categories' ),
        'parent_item' => _x( 'Parent Item Category', 'mg_item_categories' ),
        'parent_item_colon' => _x( 'Parent Item Category:', 'mg_item_categories' ),
        'edit_item' => _x( 'Edit Item Category', 'mg_item_categories' ),
        'update_item' => _x( 'Update Item Category', 'mg_item_categories' ),
        'add_new_item' => _x( 'Add New Item Category', 'mg_item_categories' ),
        'new_item_name' => _x( 'New Item Category', 'mg_item_categories' ),
        'separate_items_with_commas' => _x( 'Separate item categories with commas', 'mg_item_categories' ),
        'add_or_remove_items' => _x( 'Add or remove Item Categories', 'mg_item_categories' ),
        'choose_from_most_used' => _x( 'Choose from most used Item Categories', 'mg_item_categories' ),
        'menu_name' => _x( 'Item Categories', 'mg_item_categories' ),
    );

    register_taxonomy( 'mg_item_categories', array('mg_items'), $args );
}

【问题讨论】:

  • 检查主题输出中特定于该类别的 css 类。例如。按该类别的数字 ID 搜索。你可能会直接找到一些东西,这样你就可以在你的 CSS 中轻松解决这个问题。

标签: wordpress taxonomy


【解决方案1】:

using this code get your '13' category taxonomy post :

<?php
$args = array(
    'tax_query' => array(
        array(
        'taxonomy' => 'mg_item_categories',
        'field' => 'id',
        'terms' => '13'
            )),

'post_type'=>'mg_items',
'order'=>'ASC',
'posts_per_page'=>-1
);

query_posts($args);
while ( have_posts() ) : the_post(); 

$image_id = get_post_thumbnail_id(); 
$image_url = wp_get_attachment_image_src($image_id,'full');   
?>
    <h2> <?php the_title(); ?></h2>
    <?php the_content(); ?>

    <?php if ( has_post_thumbnail() ) { ?>
    <a href="<?php the_permalink(); ?>"><img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" /></a>
    <?php } ?>

    <?php   
    endwhile; 
    wp_reset_query();
?>

【讨论】:

  • 如何使用此代码进行定位?我如何在上面的代码中使用 if() 。类似 if(terms =13) 谢谢
  • if(term=13) 不需要此代码,因为仅在显示后使用此代码 term=13。
猜你喜欢
  • 1970-01-01
  • 2012-06-21
  • 2014-07-20
  • 2013-09-29
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多