0

I am looking for some sage advice on how to best create a page/slug structure for a WP site I am building. It is a portfolio site that will showcase creative work. I have currently created several custom post types for things such as the portfolio/work items and for their associated clients and have created relationships between those items using the Advanced Custom Fields Plugin. All of this works great. What I am struggling with is how to create the best URL structure.

I have the following pages already created and working well:

/work/ - index page that shows all work regardless of client /clients/ - list of all clients

I need to create the following pages:

/clients/client/ - this page would show all work associated with a specific client. Here is where I am struggling. I need help understanding how to use the page template system to set the correct page slugs. Can I use page slugs as part of my WP query? Can I simply query based on the custom post type? What do I name the page template file for this to work?

Appreciate any advice and/or examples anyone can offer. Thank you.

2 Answers 2

1

WordPress offers an easy mechanism to handle pages and posts as well that are being created and rendered. It is us who has to take up the challenge to do those wonderful designs and tasks that we need. Moving on to the following topic of page and slug architecture we shall discuss in detail.

First let us look onto the following thing.

Creating Page Template for Custom Posts Type

Ex: If you are creating the post type called news the WordPress system will look for the following structures.

  1. single-{post-type}.php
  2. archive-{post-type}.php

single-{post_type}.php

If your custom post type were 'news', and/or query_var = "news", WordPress would look for single-news.php to display the single or permalink of the post.

archive-{post_type}.php

If your custom post type were 'news', and/or query_var = "news", WordPress would look for archive-news.php to display the archive of posts.

If these files are not available in your Theme's directory WordPress will look for archive.php and single.php, respectively. If even these files are not present it will default to index.php.

Template File will be like this.

<?php 
$args = array( 'post_type' => 'news', 'posts_per_page' => 10,'post_status'=>'publish','orderby'=>'ID','order'=>'DESC' );
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?> 
</div>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Hence from the above code you can display the Latest Posts from News category in the count of 10.

If you need the count to be infinite you have to change the post_per_page=10 to post_per_page=-1.

Hope so this reference and advice that i have tried to explain will be helpful for you. Still if you face any issues about my explanation comment over to my answer and i am there to help you.

0

I didn't go this exact route to resolve my question as I am using the Advanced Custom Relationships plug-in. My solution is somewhat similar to this: https://www.advancedcustomfields.com/resources/querying-relationship-fields/ - but your response was very helpful and got me going on the right track, thank you!

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.