Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Adding New Custom Post Type, Output to Index.php WordPress

So, I currently have my site setup like this. To add a new post, I go to Posts, Add New. I add the content, check a category, check a grade level, and boom… it outputs a link and except to the post on the index.php (homepage) here - [[LINK href="http://pefriends.com"]]http://pefriends.com[[/LINK]].

Here's what it looks like from the backend when adding a new post: [[LINK href="http://cl.ly/3e1h0r1f1U1H262R0a0e"]]http://cl.ly/3e1h0r1f1U1H262R0a0e[[/LINK]]. The grades taxonomy was developed by someone here on wpquestions.com, and <strong>this is the code in the functions.php they created so that it's placed on the Add Post screen…</strong>

//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_custom_taxonomies', 0 );

//create two taxonomies, genres and writers for the post type "book"
function create_custom_taxonomies()
{

// Add new taxonomy, make it hierarchical (like categories)

$labels = array(
'name' => 'Grades',
'singular_name' => 'Grade',
);
register_taxonomy('grade',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
));
}


And here's the code added to the single.php file so that both the suggested grades, and category is output to produce the 2 icons you see on the green flag…

<div class="downflag">

<?php
// set vars
$post_id = get_the_ID();
$html = '';

// get post taxonomies(skill & grade)
$skills = get_the_terms ( $post_id, 'category' );
$grades = get_the_terms ( $post_id, 'grade' );

// do for each category(skill)
if ( $skills && !is_wp_error( $skills ) ) {
foreach ( $skills as $term ) {

$html .= '<a href="' . get_term_link( $term->slug, 'category' ) . '" class="skill ' . $term->slug . '">' . $term->name . '</a>';

}
}

// and for each taxonomy(grade)
if ( $grades && !is_wp_error( $grades ) ) {
foreach ( $grades as $term ) {

$html .= '<a href="' . get_term_link( $term->slug, 'grade' ) . '" class="grade ' . 'level-'.$term->slug . '">' . $term->name . '</a>';

}
}

// print the results on screen
?>

<?php echo $html; ?>

</div><!--end downflag-->


Go to this post to see the green flag displayed: [[LINK href="http://pefriends.com/the-24-hour-challenge-run/"]]http://pefriends.com/the-24-hour-challenge-run/[[/LINK]].

Currently, when adding a new post, I've been simply entering in some content into the text editor, embedding a video from YouTube, and then simply posting it, along with checking both a category and suggested grade.

But, I recently stumbled upon a plugin called [[LINK href="http://pippinspages.com/wordpress/easy-content-types-for-wordpress/"]]Easy Content Types[[/LINK]]. I'd like to add a custom post type called "Lesson Plans", where I could go in, add a new lesson plan, and like a regular post, it would output to the index.php (homepage), just like all the regular new posts do.

Using this plugin, I've figured out how to add the Lesson Plans custom post type, and also how to add the custom meta boxes for certain information needed for each lesson plan, such as name of the activity, description of activity, equipment needed, suggested grades, etc.

Here's a screenshot of the custom meta boxes I've created using this plugin: [[LINK href="http://cl.ly/2D0B1I2l3G2r1W1J1G08"]]http://cl.ly/2D0B1I2l3G2r1W1J1G08[[/LINK]]. It sits right underneath the main content editor part of adding a new Lesson Plan, just like Adding a New Post.

So… what I am needing to do is two-fold.

<strong>First, when I add a New Lesson Plan, I need it to be output just like regular posts to the index.php page so that it displays on the homepage.</strong>

<strong>Second, I need each custom meta box output on single.php to be within a definition list.</strong>

For example, on the single.php, the code would be output like this…

<dl>
<dt>Name of Activity</dt>
<dd>content here</dd>
<dt>Description of Activity</dt>
<dd>content here</dt>
<dt>Etc., etc.,</dt>
</dd>content here</dt>
</dl>


Now the plugin provides the code that goes in between the <dd> tags for each custom meta box, but I don't know exactly how to work that in using PHP.

Here's a screenshot of how the plugin author says to include the code into template files: [[LINK href="http://cl.ly/3p3q2K3C241b2X3y310B"]]http://cl.ly/3p3q2K3C241b2X3y310B[[/LINK]]

And so underneath the "Shortcode" header in this screenshot are the codes needed for including them in template files: [[LINK href="http://cl.ly/2Z3K2z1o0A131V2M3n0n"]]http://cl.ly/2Z3K2z1o0A131V2M3n0n[[/LINK]]

And here's some information from the custom post type screen: [[LINK href="http://cl.ly/1E451E270s372w3S0j2Y"]]http://cl.ly/1E451E270s372w3S0j2Y[[/LINK]]

Ok, I hope that make sense to everyone. If you need me to send more code, either from the index.php file, or something else, let me know.

Also, if there is an easier way to do this (maybe without a plugin), by all means go for it.

Thanks for your help!

Answers (2)

2011-07-16

Maor Barazany answers:

About adding your new Custom Post Type (CPT) to the index.php, go to this file, and just <strong>before</strong> the look, that starts with if have_posts() etc.. you should add

$args['post_type'] = array( 'post', 'lessonplans' );
query_posts($args);



Maor Barazany comments:

About the metaboxes in the single.php -

You should add this:


<dl>
<dt>Name of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'namefactivity', true);?></dd>
<dt>Description of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'descriptionofactivity', true);?></dd>


As you can see, the name of the field that appears as the shortcode id, is being used here as the content, using the get_post_meta function.


Spencer Barfuss comments:

Where would I put it in this piece of code? This is all the code within the index.php file...

get_header();
global $woo_options;
?>
<!-- #content Starts -->
<?php woo_content_before(); ?>
<div id="content" class="col-full">

<div id="main-sidebar-container">

<!-- #main Starts -->
<?php woo_main_before(); ?>
<div id="main" class="col-left">

<?php get_template_part( 'loop', 'index' ); ?>

</div><!-- /#main -->
<?php woo_main_after(); ?>

<?php get_sidebar(); ?>

</div><!-- /#main-sidebar-container -->

<?php get_sidebar( 'alt' ); ?>

</div><!-- /#content -->
<?php woo_content_after(); ?>

<?php get_footer(); ?>


Maor Barazany comments:

About the metaboxes in the single.php -

You should add this:


<dl>
<dt>Name of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'namefactivity', true);?></dd>
<dt>Description of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'descriptionofactivity', true);?></dd>


As you can see, the name of the field that appears as the shortcode id, is being used here as the content, using the get_post_meta function.


Spencer Barfuss comments:

Thanks for the metabox code, but I'm not sure where to place it within the single.php without the file breaking. Here is the code within the #main content section...


<?php
woo_loop_before();

if (have_posts()) { $count = 0;
while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.
}
}

woo_loop_after();
?>


Maor Barazany comments:

Try to put it before


<?php get_template_part( 'loop', 'index' ); ?>

If it's not working, paste here the loop.php file


Maor Barazany comments:

You can also add this to your functions.php file


<?php
add_filter( 'pre_get_posts', 'tc_add_to_query' );

function tc_add_to_query( $query ) {
// if ( is_home() ) {
if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
return $query;
$supported = $query->get( 'post_type' );
if ( !$supported || $supported == 'post' )
$supported = array( 'post', 'lessonplans' );
elseif ( is_array( $supported ) )
array_push( $supported, 'lessonplans' );
$query->set( 'post_type', $supported );
return $query;
//}
}
?>


Spencer Barfuss comments:

I put it before the <?php get_template_part( 'loop', 'index' ); ?>, but I have a feeling that it's not going to work because it's all black in Dreamweaver...

[[LINK href="http://cl.ly/3j190w3e1G30022X3w43"]]http://cl.ly/3j190w3e1G30022X3w43[[/LINK]]

Is there something missing with this code, or do I need to send you the loop.php file? In any case, here's the loop.php file code...

woo_loop_before();

// Exclude stored duplicates
// $exclude = get_option('woo_exclude');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post',
'category__not_in' => '',
'paged' => $paged );
query_posts( $args );

if (have_posts()) { $count = 0;
?>

<div class="fix"></div>

<?php
while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() );

} // End WHILE Loop
} else {
get_template_part( 'content', 'noposts' );
} // End IF Statement

woo_loop_after();

woo_pagenav();
?>



Maor Barazany comments:

About single.php, try this -



?php

woo_loop_before();



if (have_posts()) { $count = 0;

while (have_posts()) { the_post(); $count++;



woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.


<dl>
<dt>Name of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'namefactivity', true);?></dd>
<dt>Description of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'descriptionofactivity', true);?></dd>

}

}

woo_loop_after();
?>


Maor Barazany comments:

About the loop.php and the index - try my suggested function to the finctions.php instead.

If that function won't work for you, than change this in loop.php -


$args = array( 'post_type' => 'post',
'category__not_in' => '',
'paged' => $paged );


into this -

$args = array( 'post_type' => array('post', 'lessonplans'),
'category__not_in' => '',
'paged' => $paged );


Maor Barazany comments:

And the black lines you got, is since you didn't add php tags before and after mycode...

That would become -


<?php $args['post_type'] = array( 'post', 'lessonplans' );

query_posts($args); ?>


But try also the other options I gave you - the function to functions.php, or the change in loop.php


Spencer Barfuss comments:

Ok, posted that portion of your code into the single.php file, just to see what happened, and it already looks like there's a problem. Notice how the first php echo code is not red like the second one?
[[LINK href="http://cl.ly/2G020D0x2R1m2u2o1Z1m"]]
http://cl.ly/2G020D0x2R1m2u2o1Z1m[[/LINK]]


Maor Barazany comments:

In the single.php there should be a close to the php tag before the dl

Use this please:



<?php
woo_loop_before();

if (have_posts()) { $count = 0;

while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually. ?>

<dl>
<dt>Name of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'namefactivity', true);?></dd>
<dt>Description of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'descriptionofactivity', true);?></dd>

<?php
}

}

woo_loop_after();

?>


Spencer Barfuss comments:

A close to the php tag <em>before</em> the <dl> tag? Can you check your code and resend? Something doesn't look right, or maybe it's just me...


Spencer Barfuss comments:

Ok, I changed the code within the single.php file to this...

<?php
woo_loop_before();

if (have_posts()) { $count = 0;
while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.


<dl>
<dt>Name of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'namefactivity', true);?></dd>
<dt>Equipment Needed</dt>
<dd><?php echo get_post_meta($post->ID, 'equipmentneeded', true);?></dd>
<dt>Skills Developed</dt>
<dd><?php echo get_post_meta($post->ID, 'skillsdeveloped', true);?></dd>
<dt>Suggested Grades</dt>
<dd><?php echo get_post_meta($post->ID, 'suggestedgrades', true);?></dd>
<dt>Description of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'descriptionofactivity', true);?></dd>
<dt>Variations</dt>
<dd><?php echo get_post_meta($post->ID, 'variations', true);?></dd>
<dt>Teaching Suggestions</dt>
<dd><?php echo get_post_meta($post->ID, 'teachingsuggestions', true);?></dd>
</dl>

<?php
}
}

woo_loop_after();
?>


I changed the code within the index.php file to this...

<?php woo_main_before(); ?>
<div id="main" class="col-left">

<?php $args['post_type'] = array( 'post', 'lessonplans' );
query_posts($args); ?>

<?php get_template_part( 'loop', 'index' ); ?>

</div><!-- /#main -->


And finally, I changed the code within the functions.php file to this...

// LESSON PLAN CUSTOM POST TYPE ADDED 7-17-2011
add_filter( 'pre_get_posts', 'tc_add_to_query' );
function tc_add_to_query( $query ) {
// if ( is_home() ) {
if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
return $query;
$supported = $query->get( 'post_type' );
if ( !$supported || $supported == 'post' )
$supported = array( 'post', 'lessonplans' );
elseif ( is_array( $supported ) )
array_push( $supported, 'lessonplans' );
$query->set( 'post_type', $supported );
return $query;
//}
}


I went ahead and created a new Lesson Plan filling out all the details, but the main content area and sidebar are completely missing: [[LINK href="http://cl.ly/06292C390l2w1M1d0H3M"]]http://cl.ly/06292C390l2w1M1d0H3M[[/LINK]]

<strong>What would you suggest that I do now?</strong>


Maor Barazany comments:

Your single.php code is note right. Use the version I posted here, yes the php tag should be clsosed before the dl tag, since dl tag is html and not a php. The php is being opened and closed again later on the code as needed, just copy and paste it.

You DO NOT need to change both index.php and add the code to functions.php - you should either do one of the three options - change the index.php <strong>OR</strong> add the function to functions.php <strong>OR</strong> change the loop.php


Spencer Barfuss comments:

Ok, that didn't seem to work. Here's what I put in the single.php, as you requested...


<?php
woo_loop_before();

if (have_posts()) { $count = 0;
while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.
?>

<dl>

<dt>Name of Activity</dt>

<dd><?php echo get_post_meta($post->ID, 'namefactivity', true);?></dd>

<dt>Equipment Needed</dt>

<dd><?php echo get_post_meta($post->ID, 'equipmentneeded', true);?></dd>

<dt>Skills Developed</dt>

<dd><?php echo get_post_meta($post->ID, 'skillsdeveloped', true);?></dd>

<dt>Suggested Grades</dt>

<dd><?php echo get_post_meta($post->ID, 'suggestedgrades', true);?></dd>

<dt>Description of Activity</dt>

<dd><?php echo get_post_meta($post->ID, 'descriptionofactivity', true);?></dd>

<dt>Variations</dt>

<dd><?php echo get_post_meta($post->ID, 'variations', true);?></dd>

<dt>Teaching Suggestions</dt>

<dd><?php echo get_post_meta($post->ID, 'teachingsuggestions', true);?></dd>

</dl>



<?php

}
}

woo_loop_after();
?>


I also tried all 3 different solutions, but still nothing. The single.php is not showing the article. I created a Lesson Plan with the title "Roller Derby Skating." Here's the url that it created...

http://localhost:8888/pefriends/pelessons/roller-skating-derby/

/pefriends is just the directory I installed Wordpress in, so ignore that. But notice the /pelessons that was created. That is only created after creating a new Lesson Plan. I'd rather it not be there...

That was done here - [[LINK href="http://cl.ly/2v1e3K1U2t3v0q2P0K0N"]]http://cl.ly/2v1e3K1U2t3v0q2P0K0N[[/LINK]]. It gives me these options though - [[LINK href="http://cl.ly/1Q1v0r0r2w2h0X2z1f0i"]]http://cl.ly/1Q1v0r0r2w2h0X2z1f0i[[/LINK]].

Currently, my site's permalink structure is /%postname%/.


Maor Barazany comments:


Does the single should show your posts and the data from the other CPT?
If so, this is not what you mentioned at first, and other code needed to be written.

If you have a CPT - it has a single view. If you want that your single will be only for the <strong>lessonplans</strong> CPT, you should copy your single.php and give it the name single-lessonplans.php. You may read here about [[LINK href="http://codex.wordpress.org/Template_Hierarchy"]]Template Hierarchy[[/LINK]].

If you want to show your regular posts and inside them data from another CPT - this is another story, and another code will be needed for that.

The code I gave you will show single files from that certain CPT you mentioned.

After you create a new Lesson Plan, obviously you will have that url of the single "post" from that CPT, unless you will define your CPT to be not visible, I don't know what options your plugin has, I usualy write code to render CPT, and not using plugins, so I customize everything to the needs.

More than that, it might be a bit different in your case, since your theme has a lot of theme specific functions, so it might be the reason why the 3 options I gave you did not work, or you just made something wrong, I can't tell.

Right now I really can't understand what you want to achieve and have.

2011-07-19

Romel Apuya answers:

i can fix this one up..