0

I have a little problem to display some articles related to a parent page. I explain.

On the first hand I have a page with a model template. On the wordpress editor part, I create a custom meta field call "cat_page". This meta must be the same as the article related (view more below). This template has a classic loop to display to content of the page :

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>

and below I need to display the articles who have the same category name as the page.

if ( get_post_meta($post->ID, "cat_page", true) == the_category() ) : ( have_posts() ) : while ( have_posts() ) : the_post();

It's only what I think it should look, because I have tried several things but I don't know how to proceed.

Image (if it can help) http://i.imgur.com/SlpQh9v.jpg?1

Thanks in advance.

2
  • Can you adjust your words well to make it more understandable? its really not clear now :) Nov 14, 2013 at 18:30
  • Sorry it was a bit hard to be really clear, but the problem is solve. Nov 14, 2013 at 20:14

1 Answer 1

0

If I understand well, you have a page template, and you want to use this page template as a category archive.

The term to show is in a page meta field with key 'cat_page'.

If I'm right, in this page template use:

the post(); // being a singular page the 'while' stuff is useless
the_content(); // this will show the page content

$cat = get_post_meta( get_the_ID(), 'cat_page', true ); // get the category to display

if ( $cat ) { // if category is setted in page custom field
   $query = new WP_Query( 'category_name=' . $cat ); // run the custom query
   while ( $query->have_posts() ) { $query->the_post(); // run the custom loop

     // your post loop code goes here

   }
   wp_reset_postdata(); // reset the post data after the custom query
}

Pleas note that to make this work in the custom meta 'cat_page' you should put the category slug (not the category name).

1
  • Thanks, that's was perfectly what I was looking for, and for sure, I put the slug of category name. :) Nov 14, 2013 at 20:16

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.