• I have reached my wit’s end with this issue. I’m making a motorcycle site that will have posts with multiple categories. And what I’m trying to do is so that whenever I click a link to a single post, that URL’s post should change based on what category I’m clicking the read more button from.

    For example, I have a single post named Kawasaki, and that brand offers multiple services(those are my categories), services being: moto hotels, shops, repairs. So the Kawasaki post appears on 3 different occasions. And when I enter from mysite.com/moto-hotels, I want my URL to be mysite.com/moto-hotels/kawasaki. BUT if I enter from mysite.com/shops, I want that to be reflected in my URL and breadcrumbs, meaning it shows as mysite.com/shops/kawasaki.

    But for some reason I can’t seem to figure out, the URL always remains the same(same for the breadcrumbs), meaning that my post displays the same category, not changing like I’d want it based on the category(ex. when clicking from mysite.com/shops, the URL still shows as mysite.com/moto-hotels/kawasaki). I’ve tried working it out with Yoast SEO and Permalinks Manager, but to no avail, the problem probably being more complex than I would like it to.

    I appreciate any help and tips that may come my way.

    • This topic was modified 5 months, 2 weeks ago by vladruswork.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi there,

    If I got this correct, I can suggest you to go to Settings -> Permalink. In this page you can select a Custom Structure for the permalinks, and then try something like /%category%/%postname%/

    I hope this is a useful suggestion

    Thread Starter vladruswork

    (@vladruswork)

    Hi @redfra7 ,

    Thanks for the tip, unfortunately I have tried this before but the result isn’t what I had hoped for, as you can see in the image, even though I’m on the hotel category, my link still points towards another category, and this is the behavior I’m trying to fix.

    <?php
    while (have_posts()) {
    the_post(); ?>

    <div class="category-container">
    <div class="category-box">
    <div class="category-photo-box">
    <img class="category-photo" src="<?php the_post_thumbnail_url('categoryPhoto'); ?>" alt="" />
    </div>
    <div class="category-content">
    <div class="category-title-link-box">
    <div class="category-title-box">
    <h2 class="category-title"><?php esc_attr(the_title()); ?></h2>
    <img src="<?php echo get_template_directory_uri(); ?>/img/card-slides-rating.svg" alt="" />
    </div>
    <div class="category-link-box">
    <a href="<?php the_field('website_link'); ?>" target="_blank">
    Vezi site-ul
    </a>
    <button>
    <a href="<?php the_field('website_link'); ?>">
    <img src="<?php echo get_template_directory_uri(); ?>/img/external-link.svg" alt="" />
    </a>
    </button>
    </div>
    </div>
    <div class="category-desc">
    <p>
    <?php echo esc_attr(wp_trim_words(get_the_excerpt(), 18)); ?>
    </p>
    </div>
    <div class="category-buttons">
    <div class="category-read-more">
    <a class="read-more-link" href="<?php esc_url(the_permalink()); ?>">Vezi mai multe<span
    class="clickable-div"></span></a>
    <img src="<?php echo get_template_directory_uri(); ?>/img/right-arrow.svg" alt="" />
    </div>
    <div class="category-save">
    <p class="save-text">Salveaz?</p>
    <img src="<?php echo get_template_directory_uri(); ?>/img/bookmark.svg" alt="" />
    </div>
    </div>
    </div>
    </div>
    </div>
    <?php }
    ?>

    This is the code I’m working with, maybe it helps, might be an issue with the_permalink(), maybe I need to use another function?

    I see that the website is still local, so I can’t see it. I don’t think it depends on the piece of code you entered.
    If you have multiple categories assigned to the post, have you tried removing them, leaving only the one you want to appear in the permalink? Some plugins that handle SEO also allow you to assign a category as primary. I did a test and I see that you can replicate the problem you described when there are multiple categories to which you assigned the post.

    Moderator bcworkz

    (@bcworkz)

    It’s considered poor SEO practice to have varying URLs all leading to the same resource. It’s only possible if you ensure the link rel=”canonical” tag is always the same, regardless of URL requested. It’s possible to have WP generate varying links based on context by using a few filter hooks that vary depending upon the post type. For example, for pages the filter is “page_link”.

    Even if you alter the page’s URL like this, when the link is followed, WP will redirect the request to the page’s canonical URL, so the users will most likely only see that canonical URL and not the one they actually followed. It’s possible to alter the canonical redirect behavior through the “redirect_canonical” filter, except this also impacts the link rel=”canonical” URL which you certainly do not want to vary by context.

    Personally, I’d leave the category out of the URLs. Let the assigned categories be prominent on the page itself and don’t try to mess with the URLs by context. Of course the category archives will still list all appropriate motorcycles regardless of what their individual URLs are.

    Thread Starter vladruswork

    (@vladruswork)

    @bcworkz ,

    Thank you for the explanation! The last bit of my problem would then be if I could write some custom code for the breadcrumbs so that the user can go back from which category he came from. As of now my breadcrumbs will only show the main category I have selected, meaning if the user clicks from category A BUT the main category is category B, breadcrumbs do not update so they still show Home > category B.

    What I’d like it to be is that if my user clicks from category A, the breadcrumbs show Home > category A, and if he clicks from category B, they show Home > category B. If you have any ideas I’d love to hear them.

    Moderator bcworkz

    (@bcworkz)

    You could check the “referer” header to get the category archive that users came from. Your breadcrumb code could then generate a proper label from the information.

    Alternately you could append a query string to the archive’s single post URLs that indicate which archive to display in the breadcrumb trail. This sort of approach would allow you to track farther back in a user’s journey than just checking the referrer would allow. But with referrers you’ll have cleaner URLs, they needn’t have cryptic seeming query strings tacked onto the end.

    Of course there are other ways to track a user’s journey. Where they were could be saved in a cookie or session var.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.