Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am making a website about world countries and cities.Every Post will be a city name and will have a category of a country.

In the beggining of the post I want to have a brief description of the country instead of writing the same description to 20 or more cities.

Is it possible that wordpress would see the category is Europe > Germany and inside the post put brief description which I would have in a page called Germany ?


edit: I found a piece of code which can add a page inside a post http://css-tricks.com/snippets/wordpress/embed-a-page-inside-a-page/

Still I need a way to do that the page would be added only if post is in certain category


edit2: I've done what I wanted.Half-manually though, but it works.

Soluton:

This in single.php

<?php get_template_part( 'countries' ); ?>

I created countries.php in index and there put the code:

<?php
if(in_category('category-2')) {

$recent = new WP_Query("page_id=15"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile;

} else {
echo "Error";
}
?>

Despite that I will have to add every country manually it's the best solution I could find.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Yup, inside the loop (in single.php for example) you can use get_the_terms to retrieve an array of terms (countries in this case) associated with that post. This will always be an array (or false /error if something has gone wrong) even if there is only one country associated to it.

You can then take the country taxonomy-term and output its description use term_description.

$taxonomy = 'my-country-tax'; Your taxonomy slug

//Should be an array of 1 in this case
$countries = get_the_terms( $post->ID, $taxonomy);

//Check there are countries asccoiated with post
if($countries && !is_wp_error($countries)){
    $country = array_pop($countries);
    $description= term_description( $country->term_id, $taxonomy );

echo $description;
}
share|improve this answer
    
Could you give me a hand with the page and category names ? elseif(in_category('Albania')) { $recent = new WP_Query("pagename=Albania"); while($recent->have_posts()) : $recent->the_post();?> It works fine, but if I create page Albania as a child to Europe it doesnt work anymore, same with categories –  Tomas Mar 2 '12 at 13:58
add comment

As an alternative, you could pull in the description of that category and use that instead of a page for your brief?

To do this, you will need to be strict about categories, e.g. if a city straddles the borders of two countries you will need to pick one, and only one, or if you use a city in somewhere like the UK, e.g. Edinburgh, you would need to choose Scotland or the UK, but not both.

So to start with, pull in the category terms, and then check for terms that have a parent that isn't 0/-1, or are in a list of continents you've chosen (perhaps a custom 'location' taxonomy would be more appropriate than categories).

Once you've found such a term, grab its slug, then search for a page witht he same slug usign WP_Query and display its excerpt.

share|improve this answer
    
Not Really.The description will contain html, images divs so I don't think if that's possible. I have found one way of doing it though, but I'm still working on it I will post it when I'm done –  Tomas Mar 2 '12 at 10:42
    
You can put html in the description, it's not a WYSIWYG editor but it still works –  Tom J Nowell Mar 2 '12 at 11:05
    
Thanks for letting me know that.Still, I'm making a it for the customer, noobs don't know what html is ;D –  Tomas Mar 2 '12 at 11:37
    
You could always turn that field into a tinyMCE editor? it may even become one in v3.4 –  Tom J Nowell Mar 2 '12 at 11:41
    
wordpress.org/extend/plugins/rich-text-tags will add WYSIWYG to the tax description for you –  djrmom Mar 2 '12 at 15:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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