Okay
  Public Ticket #3626701
Using portfolio-tag in URL of portfolio items
Closed

Comments

  •  6
    jeskobendmann started the conversation

    I have 3 custom Queries for the Portfolio and they are each select from tags and can be filtered by categories. Now I want to have the tags (every item has only one tag) appear in the URL of the portfolio-item like so: 

    /portfolio/tag/post-title

    I dont seem to find a way to do so. Can you think of something?

    Thx in advance

    Jesko

  • [deleted] replied

    Hi Jesko,

    This is a customization question and support doesn't offer customization services as we wrote on our support homepage and on this area. I will try to help you this time thoughsmile.png

    Yes that is possible, to do that you should add this code in functions.php

    add_filter( 'portfolioposttype_args', function ( $args ) {
        $args['rewrite'] = [
            'slug'       => 'portfolio/%portfolio_tag%',
            'with_front' => true,
        ];
        return $args;
    } );
    add_filter( 'post_type_link', function ( $post_link, $post ) {
        if ( is_object( $post ) && $post->post_type == 'portfolio' ) {
            $terms = wp_get_object_terms( $post->ID, 'portfolio_tag' );
            if ( $terms ) {
                return str_replace( '%portfolio_tag%', $terms[0]->slug, $post_link );
            }
        }
        return $post_link;
    }, 10, 2 );

    After this, go to Settings > Permalinks and just click Save Changes to flush permalinks. After that you will be able to see the permalink structure of /portfolio/tag/post-title

  •  6
    jeskobendmann replied

    No way! :D You are the best! That worked like a charm. 

    Thank you so much! :) 

  •  6
    jeskobendmann replied

    Hi Arlind,

    sorry I just realized that this is not quite working in my special case. I need the have identical post names with different tags. For example: 

    domain.com/portfolio/tag-A/ABC and
    domain.com/portfolio/tag-B/ABC

    What wordpress is doing now is this:

    domain.com/portfolio/tag-B/ABC-2

    Because it thinks that the slug already exists. Do you have any ideas to tweak the code to do what I need? 

    Thanks in advance!

    Jesko

  • [deleted] replied

    Hi Jesko,

    I am glad that it worked for you! 

    Sure you can prevent this behavior of WordPress by disabling the unique post slug check for "portfolio" post type:

    function custom_prevent_portfolio_duplicate_slug( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
        if ( 'portfolio' === $post_type ) {
            return $original_slug;
        }
        
        return $slug;
    }
    add_filter( 'wp_unique_post_slug', 'custom_prevent_portfolio_duplicate_slug', 10, 6 );
  •  6
    jeskobendmann replied

    Hi Arlind,

    thank you very much, I tried that and now the contents of each portfolio-item are shown on one page. Do you have any ideas how to solve that? I'm sorry, please tell me when this exceeds the realm of support. :) 

    Best regards,

    Jesko


  • [deleted] replied

    Hi Jesko,

    Sorry for the delay as I was not able to provide support for customers.

    Can you please tell me what exactly is the problem, because I do not understand the content of each portfolio item is showing in one page. It would help me if you provide your WP credentials so I can test directly on your site my solution.

    To include your credentials click Insert Credentials button in editor toolbar. (See how)

  •   jeskobendmann replied privately
  • [deleted] replied

    Hi Jesko,

    This pertains to customization, and our support team does not provide customization services, as stated on our support homepage and in this section. However, I'll do my best to assist you this last time.

    I've added the following code snippet that will distinguish the requested portfolio ID, since the WordPress behavior returns one or more posts depending on the matched items:

    // Differentiate the portfolio items by their portfolio_tag taxonomy
    add_filter( 'request', function( $query_vars ) {
        $portfolio_slug = kalium_get_array_key( $query_vars, 'portfolio' );
        $portfolio_tag = kalium_get_array_key( $query_vars, 'portfolio_tag' );
        
        if ( ! empty( $portfolio_slug ) && ! empty( $portfolio_tag ) ) {
            $posts = get_posts( [ 
                'post_type' => 'portfolio', 
                'portfolio_tag' => $portfolio_tag,
                'posts_per_page' => -1,
            ] );
            
            foreach ( $posts as $post ) {
                if ( $post->post_name === $portfolio_slug ) {
                    $post_id = $post->ID;
                }
            }
            
            if ( isset( $post_id ) ) {
        
                unset( $query_vars['portfolio'] );
                unset( $query_vars['portfolio_tag'] );
                unset( $query_vars['name'] );
                $query_vars['p'] = $post_id;
            }
        }
        
        return $query_vars;
    } );

    Now after this, you see that these portfolio items are separated:

    Post 1

    medium
    (view large image)

    Post 2

    medium
    (view large image)

  •  6
    jeskobendmann replied

    Hi Arlind,

    thank you so much for your help and your goodwill!

    In case something else would pop up where I don't know how to solve it. Would it be possible for me to hire you? Or in case you are not interested would you know someone who would be willing to take on some smaller projects like this customization? I am pretty much always using the kalium theme or the twenty-tweny-4 theme to build websites so those would be familiar to you. 

    Thanks again and best regards,

    Jesko

  • [deleted] replied

    Hi Jesko,

    You are welcome, always. I am sorry but I cannot take any custom work apart from Laborator, however feel free to ask questions and if I cannot help you, then I could recommend adequate people for you. Simply open a ticket here or drop an email in our contact form requesting for my help.


  •  6
    jeskobendmann replied

    Hi Arlind,

    allright that's what I thought. :) That's perfect. Will do so. 

    Thanks again and kind regards,
    Jesko

  • [deleted] replied

    Thank you for your understanding. 

    If you have a spare moment, I kindly request that you consider sharing your positive experience with us on G2 because we are trying to push Kalium outside of ThemeForest and we need your help. Your review would not only make our day but also help other potential customers to make an informed decision about choosing our theme for their needs.

  •  6
    jeskobendmann replied

    Of course. Review is done. ;)

    All the best for your aspirations to be independent from themeforest! 

    Kind regards,
    Jesko