Display Wordpress Category - wordpress

I want to display only the those parent category name and with their child category..
Example category
AAAAA,BBBB,CCCCC,DDDDD
Parent category AAAAA having child category a,b,c
Parent category BBBB having child category x,y,z
Now I want to display only (Result required)
AAAAA
a,b,c
BBBB
x,y,z
Please help me...

If CCCCC's id is 17 and DDDDD id is 38, then the following example would work.
<ul>
<?php wp_list_pages('exclude=17,38' ); ?>
</ul>

Related

In Wordpress get category ID within a blog roll

I'm not a fan of the default blog roll for a theme that I'm using, so I'm deconstructing it, so far I was able to pull into the blog roll template:
Author: get_the_author_link()
Date with formatting: the_time('F jS, Y');
What I'm having trouble with is pulling in the categor(eis) for each post. I've tried:
get_the_category(); // with or witout the get_the_ID()
But All I get back the array object. I then tried traversing to no avail. What am I doing wrong? Again, this is not an individual post, but a blog roll.
You're looking for get_the_category_list().
This function takes three parameters - separator, parents and post id (in that order).
So, you'll want to do something like this:
<?php echo get_the_category_list(', ', '', get_the_ID()); ?>
This will create a an unordered (ul) list with commas in between the category names. You will most likely need to use some CSS to make the li items float or set them to inline block. Something like this:
<ul>
<li>Cat 1, </li>
<li>Cat 2, </li>
<li>Cat 3</li>
<ul>
You can also use get_the_term_list(). This has a little more flexibility in the final output of the category terms.
get_the_term_list() accepts five parameters: post id, taxonomy, before, separator, after.
For your use you would do this:
<?php echo get_the_term_list( get_the_ID(), 'category', '<span>', ', ', '</span>' ); ?>
This will output something like this:
<span>Cat 1, Cat 2, Cat 3</span>
You can change the wrapper (<span>), I just used that as an example.
Here is documentation on both functions from the Codex:
https://codex.wordpress.org/Function_Reference/get_the_category_list
https://codex.wordpress.org/Function_Reference/get_the_term_list

Displaying the name of a child category without listing all childs

I'm trying to display the name of a child category attached to a post using this function:
<?php wp_list_categories('style=none&orderby=id&show_count=0&use_desc_for_title=0&child_of=39&hide_empty=1'); ?>
The problem is that it lists all the child category names of parent 39. I would only like to get the category name that is used by the post.
Thanks in advance
Thanks to #Siyah, I found the answer here
<?php
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(7, $childcat)) {
echo $childcat->cat_name;
}}
?>

Data/Content Architecture in WordPress 3.1

Update
I solved it in that way:
What you need:
Custom Post Type
Enabled Plugin "Posts 2 Posts" (see comments below)
Enabled Plugin "Allow numeric stubs"
You'll need the Plugin "Posts 2 Posts" to assign posts tp pages. "Allow numeric stubs" is a plugin that will allow you to have numeric slugs for pages. If try to add pages named to 2011 or 2012 you'll get Page-Slugs like 2011-2 or 2012-2 because it is not possible to have a page slug that is a number.
First you have to add some Pages. For Example 2011, Nominees and Actors. Now you arrange the Pages as childs to your needs.
2011
-Nominees
--Actors
Your Url will now look like http://example.com/2011/nominees/actors
Now you have to add a Custom Post Type which contains your nominees. Add some nominees to your Post Type (should work with Articles too).
Register a connection type in your functions.php
function my_connection_types() {
// Make sure the Posts 2 Posts plugin is active.
if ( !function_exists( 'p2p_register_connection_type' ) )
return;
p2p_register_connection_type( array(
'id' => 'posts_pages',
'from' => 'page',
'to' => 'nominees' // Your Post-Type
) );
}
add_action( 'init', 'my_connection_types', 100 );
Now go to your Actor-Page, look for a MetaBox named "Connected Nominees" and add your nominees.
Edit your page.php
<article class="single entry">
<header class="post post-header">
<h1><?php the_title(); ?></h1>
</header>
<section class="post post-content">
<?php the_content(); ?>
<?php
// Find connected pages
$connected = p2p_type( 'posts_pages' )->get_connected( get_queried_object_id() );
// Display connected pages
if ( $connected->have_posts() ) :
?>
<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php
// Prevent weirdness
wp_reset_postdata();
endif;
?>
</section>
</article>
Now you should be able to see you asigned Posts (nominees) on your Page Actors.
The solution is easy to understand for authors since they are working with default wordpress behaviors and the content connection process feels really native.
I'm looking for some data architecture best practices in WordPress 3.1.n
Here are the facts:
It is some kind of award
The Award has **n** different categories
People could be nominated
Each nominee could be asscociated with 1 category
Each category is grouped by year
Each category can contain up to 3 or 5 nominees
Categories/Terms(?)
2011
actor
special-effects
story
2012
actor
special-effects
story
For Example:
http://example.com/nominees/2011/actor/all-nominees-in-actor-cat-in-2011.html
http://example.com/nominees/2011/special-effects/all-nominees-in-special-effects-cat-in-2011.html
http://example.com/nominees/2012/special-effects/all-nominees-in-special-effects-cat-in-2012.html
Note: Custom Post-Types and Custom Taxonomies are fine but i can't get hirachical Taxonomy-Terms work.
Does anyone have some suggestions to solve this with WordPress?
You might consider creating an "Award" post type with categories being the types of awards... Best Actor, Special Effects, etc. Also you could tag each award post by what year it is. I like tagging the year vs a year as a category because it gives you the option to build and scale content by year as the site grows. However a category would work as well.
Then, perhaps create an Actor post type with each actors name, this would have the added benefit of being scaleable should you want to expand on actor profiles down the road. Then consider connecting them using something like the "Posts 2 Posts" plugin. Plugin here
This plugin is great for associating posts with other posts like a "review" post with a "product" post. You will essentially be doing the same thing except "review" would be substituted with "award" and "product" would be associated with "actor".

How to add ad code in the top & bottom of "page"s?

My site's pages are made up of content-node-type:page. I want to add advertisement above the H1's(in the top of page) and in the bottom ie., just above the footer.
Is there any trick beyond changing page.tpl? Will blocks do? But their width seem to be small.
My site is this one
Well actually you can do it with a combination of both. First define new regions (call them top_ad and bottom_ad or similar) in your template definition file (template.info) as follows :
regions[top_ad] = Top ad bar
regions[bottom_add] = Bottom ad bar
Then add the following in your page template (page.tpl.php) where you want to display these :
<?php if ($top_ad): ?>
<div id="top-ad" class="...">
<?php print $top_ad; ?>
</div>
<?php endif; ?>
....
<?php if ($bottom_ad): ?>
<div id="bottom-ad" class="...">
<?php print $bottom_ad; ?>
</div>
<?php endif; ?>
Then just create 2 blocks containing the banner content and put them in the top and bottom ad bar region respectively.

Wordpress: how to make a post template with a fix image placement?

I am building a simple WP theme, but now I am stuck. I need to add an image on a fixed place for post_template A and 2 images on post_template B
Like so:
title
img
date
content
title
img img
date
content
Is it possible to make a template and on the admin side have the corresponding # of upload fields?
You can do that with custom fields...
In the edit page view add a custom field called "img1" for instance and the value should be the image url.
Then in your template A file you can output the image with:
<?php
$myimage = get_post_meta($post->ID, 'img1', true);
if ($myimage) { ?>
<img src="<?php echo $myimage; ?>" />
<?php } ?>
And in template B you just do that 2 times ;)

Resources