0

I have an art portfolio site in development.

I have used CPT UI to set up:
post type: works
taxonomy: mediums
mediums: paintings, etc

The best way I could think to set this up was to use taxonomy-medium.php as the archive/template for displaying these custom posts.

This is working fine with the following code:

<?php if ($posts) : ?>
    <!-- Slider main container -->
    <div class="swiper mySwiper">
        <!-- Additional required wrapper -->
        <div class="swiper-wrapper">
            <!-- Slides -->
            <?php foreach ($posts as $post) :
                setup_postdata($post) ?>
                <div class="swiper-slide"><?php the_content(); ?></div>
            <?php endforeach; ?>
        </div>
    </div>


    <!-- If we need pagination -->
    <div class="swiper-pagination"></div>

    <!-- If we need navigation buttons -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>

<?php wp_reset_postdata(); ?>

<?php endif; ?>

Now I want to clean up the UI by doing 2 things. First, I would like to remove the custom taxonomy slug from urls, changing it from https://site.dev/medium/paintings to https://site.dev/paintings
I've tried several plugins that claim to do this and none worked.

Next I would like to forego a landing page, and just have https://site.dev/paintings as the front page, showing the content from the archive page code shown above.

I have looked through many answers to both these questions, and have not been able to find a working solution. If I set a page with:

<?php locate_template( 'taxonomy-medium.php', TRUE, TRUE ); ?>

it loads the slider but returns no posts. Ideally I would also like the home url to redirect to https://site.dev/paintings so that clicking on menu item paintings shows the same url.

1
  • You'll want to edit your front-page to dislplay a template that queries your paintings posts. You still want the taxonomy-medium.php archive to exist and work properly, but you don't want that running as your front page, at least, I wouldn't recommend it. You could, in the page.php template use is_frontpage() to distinguish if someone is viewing the front page and then run a query that displays the medium/paintings posts. Dec 16, 2022 at 17:47

1 Answer 1

0

I was able to solve this by removing CPT UI plugin, registering the custom post type with:

function create_post_type() {
  $args = array (
      'public' => true,
      'has_archive' => true,
      'show_ui' => true,
      'supports' => array('revision','title','editor','author', 'thumbnail', 'custom-fields'),
      'taxonomies'          => array( 'category' ),
      'labels' => array(
        'name' => __( 'work' ),
        'singular_name' => __( 'work' )
      )
  );
  register_post_type('work', $args);
}

Then in category.php:

<?php get_header();

    $args = array(
        'post_type' => 'work',
        'cat' => get_query_var('cat'),
        'posts_per_page' => -1 );

    $loop = new WP_Query( $args ); ?>

    <!-- Slider main container -->
    <div class="swiper mySwiper">
        <!-- Additional required wrapper -->
        <div class="swiper-wrapper">
            <!-- Slides -->
            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <div class="swiper-slide"><?php the_content(); ?></div>
            <?php endwhile; ?>
        </div>
    </div>

    <!-- If we need pagination -->
    <div class="swiper-pagination"></div>

    <!-- If we need navigation buttons -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>

<?php
// get_sidebar();
get_footer();

It wasn't immediately intuitive that 'cat' needed to be specified to filter results by the current category, and not have all posts returned.

For now I'm using a plugin 'Remove Taxonomy Slug' to change the url from /category/name to /name

It seemed unnecessarily difficult to have the homepage be a specific category archive (from category.php) so I'm showing the About page instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.