0

I have a page called: fruit...then I have three child pages called cherry, lemon, and peach. I also have a category called fruit, and three child categories called cherry, lemon, and peach. What I am trying to do is when a user clicks on one of the child pages IE: "Cherry" the user gets a category page with all posts associated to the "Cherry" category.

Normally this wouldn't be a big deal, I'd manually do an <?php if (is_page("cherry")) { ?> in my template, but what I'm looking for is a way to do it dynamically so that if someone creates a new child page and a matching child category then everything will be good to go without having to manually adjust the template.

I guess it would be taking the page and redirecting to it's corresponding category page, but is that possible?

Thanks,
Josh

  • Why don't you display the link to the "Cherry" category page if you want to show them category page anyway. This is the default behavior and you won't have to modify anything. Why would you have to create another child page for that? Am I missing something? – sakibmoon Aug 14 '14 at 22:41
  • I would if there were a way to add a category and it's children to wp_list_pages...that would actually be the ideal scenario :-) Thanks for the suggestion. – Josh Rodgers Aug 15 '14 at 1:35
1

You can do something like this

 global $post;
 //Get the page name
 $term = get_post( $post )->post_name;

 //If a category with the same name exists, then show the posts from that category
 if( term_exists( $term, 'category') )
 {
     //Show posts from $term category
 }

I hope you get the idea. You can modify it any way to suit your need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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