在single.php上仅显示带有自定义分类的一个类别的帖子

时间:2015-05-29 作者:Disha

我创建了一个名为14kgold的自定义帖子类型。在此基础上,我定义了两个类别:交响乐和黑色音乐。现在我在每个类别中添加了项目/产品。当我打开一个类别下的产品(例如symphony)时,我会选择单曲。php。到目前为止,一切都很顺利。但当我下一步做的时候,它会给我展示下一种黑色。如何使分页仅针对symphony?

    /*Custom post type 14K Gold and Silver*/
   function my_custom_post_14kgs() {
       $labels = array(
    \'name\'               => _x( \'14k Gold & Silver\', \'post type general name\' ),
    \'singular_name\'      => _x( \'14k Gold & Silver\', \'post type singular name\' ),
    \'add_new\'            => _x( \'Add New\', \'book\' ),
    \'add_new_item\'       => __( \'Add New Item\' ),
    \'edit_item\'          => __( \'Modify Item\' ),
    \'new_item\'           => __( \'New Item\' ),
    \'all_items\'          => __( \'All Items\' ),
    \'view_item\'          => __( \'View Item\' ),
    \'search_items\'       => __( \'Search Items\' ),
    \'not_found\'          => __( \'No Products found\' ),
    \'not_found_in_trash\' => __( \'No products found in trash\' ),
    \'parent_item_colon\'  => \'\',
    \'menu_name\'          => \'14k Gold & Silver\'
);
$args = array(
    \'labels\'        => $labels,
    \'description\'   => \'\',
    \'public\'        => true,
    \'menu_position\' => 5,
    \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
    \'has_archive\'   => true,
    \'hierarchical\'  => true,
    \'rewrite\'       => array(\'slug\' => \'14k-gold-silver/%14kgscollection%\',\'with_front\' => false),
    \'query_var\'     => true,
    //\'rewrite\'     => true,
    //\'publicly_queryable\' => false,
);
register_post_type( \'14kgs\', $args );
   }
   add_action( \'init\', \'my_custom_post_14kgs\' );

   function my_taxonomies_product_14kgs() {
$labels = array(
    \'name\'              => _x( \'14kgscollection\', \'taxonomy general name\' ),
    \'singular_name\'     => _x( \'14kgscollection\', \'taxonomy singular name\' ),
    \'search_items\'      => __( \'Search Product Categories\' ),
    \'all_items\'         => __( \'All Product Categories\' ),
    \'parent_item\'       => __( \'Parent Product Category\' ),
    \'parent_item_colon\' => __( \'Parent Product Category:\' ),
           \'edit_item\'         => __( \'Edit Product Category\' ),
           \'update_item\'       => __( \'Update Product Category\' ),
           \'add_new_item\'      => __( \'Add New Product Category\' ),
           \'new_item_name\'     => __( \'New Product Category\' ),
           \'menu_name\'         => __( \'14kgscollection\' ),
       );
       $args = array(
                  \'labels\' => $labels,
                  \'hierarchical\'    => true,
                  \'public\'      => true,
                  \'query_var\'       => \'14kgscollection\',

                  \'rewrite\'     =>  array(\'slug\' => \'14k-gold-silver\' ),
                  \'_builtin\'        => false,
       );
       register_taxonomy( \'14kgscollection\', \'14kgs\', $args );
   }
   add_action( \'init\', \'my_taxonomies_product_14kgs\', 0 );


   /*Filter permalink structure*/
   add_filter(\'post_link\', \'collection14kgs_permalink\', 1, 3);
   add_filter(\'post_type_link\', \'collection14kgs_permalink\', 1, 3);

   function collection14kgs_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, \'%14kgscollection%\') === FALSE) return $permalink;
           // Get post
           $post = get_post($post_id);
            if (!$post) return $permalink;

            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, \'14kgscollection\');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
                $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = \'no-collection\';

        return str_replace(\'%14kgscollection%\', $taxonomy_slug, $permalink);
    }
有什么我可以添加到上面的代码,这给了我一个独特的价值,可以帮助我区分两个类别下的产品?

2 个回复
SO网友:BA_Webimax

因为我不确定你是否在用单曲。php对于任何其他内容,我建议您复制单个。php至单重14kg。主题目录中的php。

完成后,请修改以下内容:

<?php next_posts_link(); ?>
变成。。。

<?php next_post_link(\'%link\',\'%title\',TRUE) ?>
以及

<?php previous_posts_link(); ?>
变成。。。

<?php previous_post_link(\'%link\', \'%title\', TRUE); ?>
这些函数的第三个参数告诉WP生成链接并保持在同一类别中。

进一步阅读:

https://codex.wordpress.org/Function_Reference/previous_post_linkhttps://codex.wordpress.org/Function_Reference/next_post_link

SO网友:Disha

找到了解决方案。

我正在使用get_next_post()get_previous_post() 工作正常的功能。

结束

相关推荐

Categories' hierarchy in URL

我目前正在处理的网站中的帖子都有多个层次分类。例如:Source - Books -- Moby Dick -- Sherlock Holmes 永久链接设置为/%category%/%postname%/. 然而,一篇文章的URL并不包括所有的子类别——我得到的只是site.com/source/books/*postname*, 尽管这篇文章在来源上没有分类,但只在书籍+白鲸上。有人能帮我找出如何调整这种行为吗?非常感谢。