Horje
Display post loop associated with current category url slug - Wordpress Solution
Hi There, I have to display posts dependent on what category slug the user is on. For example: - If the user is on a page www.me.com/topic/wheels. Then they will only see posts of topic wheels. - If the user is on a page www.me.com/topic/rubber. Then they will only see posts of topic rubber. Since there are 100's of topics - I would need a code snippet that retrieves the current url topic slug and then displays the posts associate to that current url topic slug. Below is what I had in mind but not sure if this is the best way of doing this:
<?php 
$term = get_queried_object();
echo $term->slug;
 if (have_posts() ) : ?>
  <?php while ( have_posts() ) : the_post(); ?>
<ul>
<li><?php the_title(); ?>  </li>
</ul>
  <?php endwhile; ?>
<?php endif; ?>
Please let me know of any suggestions - very much appreciated.

Solution - 1

Please try this

<?php

$term = get_queried_object();

$args = array(
    'post_type'     => 'post',
    $term->taxonomy    => $term->slug,
    
);   


$the_query = new WP_Query( $args );

if ($the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<ul>
<li><?php the_title(); ?> </li>
</ul>
<?php endwhile; ?>
<?php endif; ?>


Solution - 2

Hi, you can use Tags as Topic set Tag base = topic under Settings - Permalinks menu https://your-domain/wp-admin/options-permalink.php


Solution - 3

This is how WordPress works actually. Are you using the archive taxonomy? There you will have it done.





Wordpress

Related
L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution L.S. I'am developing an website for a local photoclub, i using Nextgen - Wordpress Solution
wp query with multiple of same meta keys - Wordpress Solution wp query with multiple of same meta keys - Wordpress Solution
list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution list of the children of a Woocommerce Grouped product ( with the Product picture,name and price ) - Wordpress Solution
Create array of hours within wordpress loop - Wordpress Solution Create array of hours within wordpress loop - Wordpress Solution
Query Parameters Errors - Wordpress Solution Query Parameters Errors - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
27