最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

associate custom post type with tags with specific pages

programmeradmin1浏览0评论

I have various products (custom post type) which already have categories but I have now I have added this line to be able to add tags to the products.

'taxonomies' => array('post_tag')  

So, I have an industries section with various pages within that and for example one of them is called 'Woodworking'. I have added the tag 'Woodworking' to various products and when someone visits /industry/woodworking I want those products to show up.

Is there an easy way to achieve this? I thought of perhaps using WP_Query but not sure how to create that relationship between the page name and the product tags.

EDIT:

I have done this which seems to work but not sure if the right way to go about it?

<?php $page_title = get_the_title(); ?>

<?php 
$tag_prods = new WP_Query(array(
    'tag' => 'Woodworking',
    'posts_per_page' => -1,
    'post_type' => 'Products',
    'order' => 'ASC'
    ));
?>

<?php if ($tag_prods->have_posts() ): while ($tag_prods->have_posts() ): $tag_prods->the_post(); ?>
    <?php foreach(get_the_tags() as $tags): ?>
        <?php if ($tags->name === $page_title): ?>
            <p><?php the_title(); ?></p>
        <?php endif; ?>
    <?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>

Just noticed that this only works with single words. As soon as a tag word has a space in it then it doesn't pick it up.

I have various products (custom post type) which already have categories but I have now I have added this line to be able to add tags to the products.

'taxonomies' => array('post_tag')  

So, I have an industries section with various pages within that and for example one of them is called 'Woodworking'. I have added the tag 'Woodworking' to various products and when someone visits /industry/woodworking I want those products to show up.

Is there an easy way to achieve this? I thought of perhaps using WP_Query but not sure how to create that relationship between the page name and the product tags.

EDIT:

I have done this which seems to work but not sure if the right way to go about it?

<?php $page_title = get_the_title(); ?>

<?php 
$tag_prods = new WP_Query(array(
    'tag' => 'Woodworking',
    'posts_per_page' => -1,
    'post_type' => 'Products',
    'order' => 'ASC'
    ));
?>

<?php if ($tag_prods->have_posts() ): while ($tag_prods->have_posts() ): $tag_prods->the_post(); ?>
    <?php foreach(get_the_tags() as $tags): ?>
        <?php if ($tags->name === $page_title): ?>
            <p><?php the_title(); ?></p>
        <?php endif; ?>
    <?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
<?php endif; ?>

Just noticed that this only works with single words. As soon as a tag word has a space in it then it doesn't pick it up.

Share Improve this question edited Jul 18, 2019 at 8:45 user8463989 asked Jul 18, 2019 at 7:34 user8463989user8463989 5931 gold badge8 silver badges25 bronze badges 4
  • 1 The tag argument expects one or more tag slugs (e.g. tag-name and not Tag Name). See here for the full arguments list. – Sally CJ Commented Jul 18, 2019 at 8:56
  • 1 That helps, thank you! – user8463989 Commented Jul 18, 2019 at 9:02
  • @SallyCJ Can you add that as an answer so I can mark it as the accepted answer? – user8463989 Commented Jul 19, 2019 at 10:41
  • I've just posted it as part of my answer.. I hope it helps you more. :) – Sally CJ Commented Jul 19, 2019 at 11:54
Add a comment  | 

1 Answer 1

Reset to default 2

I have done this which seems to work but not sure if the right way to go about it?

Yes, you can create a custom Page template which displays posts in your custom post type, and use that template with those "industry" pages.

And if you want, you can create a single Page which will handle all the "industry" requests/URLs like /industry/woodworking. And then you can use add_rewrite_rule() to add a custom rewrite rule which redirects the requests to that Page.

Or you can also redirect them to the tag archives. Example:

add_action( 'init', function(){
    add_rewrite_rule( '^industry/woodworking',
        'index.php?tag=woodworking&post_type=Products',
        'top' );
} );

Just noticed that this only works with single words. As soon as a tag word has a space in it then it doesn't pick it up.

As pointed in my comment, that's because the tag parameter expects one or more tag slugs and not names. Examples:

  • 'tag' => 'tag-one, tag-two' — correct.

  • 'tag' => 'Tag One, Tag Two' — incorrect.

You can check the full parameters list here.

发布评论

评论列表(0)

  1. 暂无评论