如何有条件地从分类页面重定向到帖子?

时间:2017-08-14 作者:Neelam Khan

如果它所属的分类术语只有一篇文章分配给它,我想重定向到一篇文章,到目前为止,我有以下内容:

$term_id = get_queried_object()->term_id;
$taxonomy_name = \'product_range\';
$term_children = get_term_children( $term_id, $taxonomy_name );

foreach ( $term_children as $child ) {
  $term = get_term_by( \'id\', $child, $taxonomy_name );
    if($term->count <= 1 ) {
      echo \'<a href="\'. get_term_link($child, $taxonomy_name) .\'" class="thumb" title="\'.$term->name.\'">\'.$term->name.\'</a>\';
    }
}
这链接到存档页面,但我希望它将用户重定向到相关帖子,我不知道我需要做什么才能将永久链接更改为单个帖子页面。

2 个回复
最合适的回答,由SO网友:Neelam Khan 整理而成

@ClemC我通过添加一个WP\\u查询来查询帖子并检查计数,从而解决了这个问题,下面是我的代码:

    $term_id = get_queried_object()->term_id;
        $taxonomy_name = \'product_range\';
        $custom_terms = get_term_children( $term_id, $taxonomy_name );
            echo \'<div class="row row__condensed">\';
            foreach($custom_terms as $custom_term) {
                $term = get_term_by( \'id\', $custom_term, $taxonomy_name );
                wp_reset_query();
                $args = array(
                    \'post_type\' => \'product\',
                    \'posts_per_page\' => 1,
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'product_range\',
                            \'field\' => \'slug\',
                            \'terms\' => $term->slug,
                        ),
                    ),
                 );

                $loop = new WP_Query($args);

                if($loop->have_posts()) {

                    while($loop->have_posts()) : $loop->the_post();
                        // If only one post exists link to product
                        if($term->count === 1 ) {
                            echo \'<div class="col-xs-12 col-md-4">\';
                                echo \'<div class="grid__item">\';
                                    echo \'<a href="\'.get_permalink().\'">\';                                                       
                                    echo \'</a>\';
                                    echo \'<h3><a href="\' . get_permalink() . \'">\'.$term->name .\'</a></h3>\';
                                    echo \'<p>\'.wp_trim_words($term->description, 23, \'...\').\'</p>\';
                                echo \'</div>\';
                            echo \'</div>\';
                        }
                        //else link to the listing page
                        else {
                            echo \'<div class="col-xs-12 col-md-4">\';
                                echo \'<div class="grid__item">\';
                                    echo \'<a href="\' . get_term_link( $custom_term, $taxonomy_name ) . \'">\';                                    
                                    echo \'</a>\';
                                    echo \'<h3><a href="\' . get_term_link( $custom_term, $taxonomy_name ) . \'">\'.$term->name .\'</a></h3>\';
                                echo \'<p>\'.wp_trim_words($term->description, 23, \'...\').\'</p>\';
                                echo \'</div>\';
                            echo \'</div>\';
                        }
                    endwhile;
                }
            else {
        echo "no posts found.";
            } //endforeach
        echo \'</div>\'; 

SO网友:ClemC

我想你可以简单地使用WP_Term\'s$count 应担任本任期所附职位数量的财产
那么,if 此术语只附加了一篇帖子,请查询此帖子对象并使用它完成您的工作。。。

$term = get_queried_object();

if ( $term->count === 1 ) {
    $args = array(
        \'tax_query\' => array(
            array(
                \'taxonomy\'         => \'product_range\',
                \'field\'            => \'term_id\',
                \'terms\'            => array( $term->term_id ),
                \'include_children\' => false,
            ),
        )
    );

    $query = new WP_Query( $args );
    $posts = $query->posts;
    $post  = $posts[0];

    /**
     * IMPORTANT FOR SEO...
     * Temporary redirection until your category is populated - Use 301 instead of 302 to redirect permanently...
     */
    if ( wp_redirect( get_permalink( $post->ID ), 302 ) ) {
        exit;
    }
}

结束

相关推荐

How to get terms for taxonomy

如何获取分类法customcategorie的所有术语我试图在分类法中保留代码。php<?php /* Template Name:Taxoo */ get_header(); ?> <?php $term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_v

如何有条件地从分类页面重定向到帖子? - 小码农CODE - 行之有效找到问题解决它

如何有条件地从分类页面重定向到帖子?

时间:2017-08-14 作者:Neelam Khan

如果它所属的分类术语只有一篇文章分配给它,我想重定向到一篇文章,到目前为止,我有以下内容:

$term_id = get_queried_object()->term_id;
$taxonomy_name = \'product_range\';
$term_children = get_term_children( $term_id, $taxonomy_name );

foreach ( $term_children as $child ) {
  $term = get_term_by( \'id\', $child, $taxonomy_name );
    if($term->count <= 1 ) {
      echo \'<a href="\'. get_term_link($child, $taxonomy_name) .\'" class="thumb" title="\'.$term->name.\'">\'.$term->name.\'</a>\';
    }
}
这链接到存档页面,但我希望它将用户重定向到相关帖子,我不知道我需要做什么才能将永久链接更改为单个帖子页面。

2 个回复
最合适的回答,由SO网友:Neelam Khan 整理而成

@ClemC我通过添加一个WP\\u查询来查询帖子并检查计数,从而解决了这个问题,下面是我的代码:

    $term_id = get_queried_object()->term_id;
        $taxonomy_name = \'product_range\';
        $custom_terms = get_term_children( $term_id, $taxonomy_name );
            echo \'<div class="row row__condensed">\';
            foreach($custom_terms as $custom_term) {
                $term = get_term_by( \'id\', $custom_term, $taxonomy_name );
                wp_reset_query();
                $args = array(
                    \'post_type\' => \'product\',
                    \'posts_per_page\' => 1,
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'product_range\',
                            \'field\' => \'slug\',
                            \'terms\' => $term->slug,
                        ),
                    ),
                 );

                $loop = new WP_Query($args);

                if($loop->have_posts()) {

                    while($loop->have_posts()) : $loop->the_post();
                        // If only one post exists link to product
                        if($term->count === 1 ) {
                            echo \'<div class="col-xs-12 col-md-4">\';
                                echo \'<div class="grid__item">\';
                                    echo \'<a href="\'.get_permalink().\'">\';                                                       
                                    echo \'</a>\';
                                    echo \'<h3><a href="\' . get_permalink() . \'">\'.$term->name .\'</a></h3>\';
                                    echo \'<p>\'.wp_trim_words($term->description, 23, \'...\').\'</p>\';
                                echo \'</div>\';
                            echo \'</div>\';
                        }
                        //else link to the listing page
                        else {
                            echo \'<div class="col-xs-12 col-md-4">\';
                                echo \'<div class="grid__item">\';
                                    echo \'<a href="\' . get_term_link( $custom_term, $taxonomy_name ) . \'">\';                                    
                                    echo \'</a>\';
                                    echo \'<h3><a href="\' . get_term_link( $custom_term, $taxonomy_name ) . \'">\'.$term->name .\'</a></h3>\';
                                echo \'<p>\'.wp_trim_words($term->description, 23, \'...\').\'</p>\';
                                echo \'</div>\';
                            echo \'</div>\';
                        }
                    endwhile;
                }
            else {
        echo "no posts found.";
            } //endforeach
        echo \'</div>\'; 

SO网友:ClemC

我想你可以简单地使用WP_Term\'s$count 应担任本任期所附职位数量的财产
那么,if 此术语只附加了一篇帖子,请查询此帖子对象并使用它完成您的工作。。。

$term = get_queried_object();

if ( $term->count === 1 ) {
    $args = array(
        \'tax_query\' => array(
            array(
                \'taxonomy\'         => \'product_range\',
                \'field\'            => \'term_id\',
                \'terms\'            => array( $term->term_id ),
                \'include_children\' => false,
            ),
        )
    );

    $query = new WP_Query( $args );
    $posts = $query->posts;
    $post  = $posts[0];

    /**
     * IMPORTANT FOR SEO...
     * Temporary redirection until your category is populated - Use 301 instead of 302 to redirect permanently...
     */
    if ( wp_redirect( get_permalink( $post->ID ), 302 ) ) {
        exit;
    }
}

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的