I am working on a website on WordPress for a company and I am running into the following issue. This is the structure:
Section:
fruits
Categories (sub-sections):
red, yellow, blue, orange
Items (sub-sub-sections):
apples, oranges, lemons, blueberries
Fruits
is based on the Posts
section of the site (it has the little pushpin beside it). The category plays into the URL for that page, so when an item is selected it would look something like:
food.com/fruits/red/apples/
The category page has it’s own page with some visual navigation and a blurb about that category. When the fruits are selected and you navigate to one of those categories however, the page shows the content of the fruit instead of the category page that was built itself.
For ex, the URL looks like:
food.com/fruits/red/
but it is showing the page
food.com/fruits/red/apples/
without changing the URL.
The only way I have remedied this so far is by NOT selecting a category, however then I run into the issue where the URL for that fruit page looks like this:
food.com/fruits/colour/apples/
I haven’t been able to find any information on this subject so far, and any help would be greatly appreciated.
In the following image you can see that the next step displays the wrong page, however the permalink at the second page is the same in both cases.
Edit:
archive-fruits.blade.php
@include('page')
page.blade.php
@php $iterator = 0 @endphp
@extends('layouts.app')
@section('content')
@while(have_posts()) @php the_post() @endphp
@if ($flexible_content)
@foreach ($flexible_content as $block)
@if ($block->block_type == 'markets_slider')
@include('partials.markets-slider')
@elseif ($block->block_type == 'static_content')
@include('partials.static-content')
@elseif ($block->block_type == 'expandable_content')
@include('partials.expandable-content')
@elseif ($block->block_type == 'image_+_text_listings')
@include('partials.image-plus-text-listings')
@elseif ($block->block_type == 'farmers_markets_list')
@include('partials.farmers-markets-list')
@elseif ($block->block_type == 'fruits_list')
@include('partials.fruits-list')
@elseif ($block->block_type == 'tabbed_content')
@include('partials.tabbed-content')
@endif
@endforeach
@endif
@endwhile
@endsection
layouts: app.blade.php
<!doctype html>
<html {!! get_language_attributes() !!}>
@include('partials.head')
<body @php body_class() @endphp>
@php do_action('get_header') @endphp
@include('partials.header')
<div class="wrap container" role="document">
<div class="content">
<main class="main">
@yield('content')
</main>
@if (Appdisplay_sidebar())
<aside class="sidebar">
@include('partials.sidebar')
</aside>
@endif
</div>
</div>
@php do_action('get_footer') @endphp
@include('partials.footer')
@php wp_footer() @endphp
</body>
</html>
2
Answers
I don’t really get how you are working. Sorry, that I can’t comment but I don’t have enough reputation yet. This "fruit" is a custom-post-type or is the "post" function itself? What does it mean "without changing the URL"?
You can alter the permalinks of the posts in the settings of WordPress, following
Settings>Permalink
, and add the category in the URL, as you can see here.I don’t know how you created the CPT, but it uses the default page template as an archive template. This means that you won’t have a list of posts inside the category archive, just the content.
You have to create an archive template inside that archive-fruits.blade.php, substituting that one line which is already there.
You can take a look at how an archive template is made in the file archive.php of any WordPress Theme, but this is a Custom Post Type, so there may be differences (like the fact that is named archive-fruits). Nothing that a quick search on Google couldn’t solve. I suggest you start reading the Codex and then some guides.