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 have created a custom post type called 'project'. But how do I get the custom post types to display only on my main template? It's a one page website and I don't want it to direct the visitor to a second page.

Website: http://www.must.ee/uusmust/ad
Wordpress v. 3.5.1

 <?php

function my_custom_post_project() {
$labels = array(
    'name'               => _x( 'Projects', 'post type general name' ),
    'singular_name'      => _x( 'Project', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'project' ),
    'add_new_item'       => __( 'Add New Project' ),
    'edit_item'          => __( 'Edit Project' ),
    'new_item'           => __( 'New Project' ),
    'all_items'          => __( 'All Projects' ),
    'view_item'          => __( 'View Project' ),
    'search_items'       => __( 'Search Projects' ),
    'not_found'          => __( 'No projects found' ),
    'not_found_in_trash' => __( 'No projects found in the Trash' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Projects'
);

$args = array(
    'labels'        => $labels,
    'description'   => 'Holds my projects and project specific data',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title','editor', 'excerpt'),
    'has_archive'   => false,
            'rewrite' => array('slug'=>'/','with_front'=>false),
);
register_post_type( 'project', $args );
}

add_action( 'init', 'my_custom_post_project' );

function my_taxonomies_project() {
$labels = array(
    'name'              => _x( 'Project Categories', 'taxonomy general name'    ),
    'singular_name'     => _x( 'Project Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Project Categories' ),
    'all_items'         => __( 'All Project Categories' ),
    'parent_item'       => __( 'Parent Project Category' ),
    'parent_item_colon' => __( 'Parent Project Category:' ),
    'edit_item'         => __( 'Edit Project Category' ),
    'update_item'       => __( 'Update Project Category' ),
    'add_new_item'      => __( 'Add New Project Category' ),
    'new_item_name'     => __( 'New Project Category' ),
    'menu_name'         => __( 'Project Categories' ),
);
$args = array(
    'labels' => $labels,
    'hierarchical' => true,
);
register_taxonomy( 'product_category', 'project', $args );
 }
 add_action( 'init', 'my_taxonomies_project', 0 );

I now changed the code in index.php:

   <?php get_header(); ?>


    <div id="posts" class="row isotope ">
     <!-- Start Project -->
    <?php   

   $args = array(
   'post_type' => 'project'
   );
   $projects = new WP_Query($args);
   if ($projects->have_posts()) {
   while ($projects->have_posts()) {
   $projects->the_post();
   the_title(); 
   get_the_content();
     }
   }

    ?>
   </div>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

The title is brought in but for some reason with a height of 0px. Is this related to the code or is it a completely different matter? Also the content from the editors metabox is not showing.

share|improve this question
    
You probably need to write a custom loop for displaying them. We'd need to see the home page template code for your theme in order to make specific recommendations. –  cale_b Jun 19 '13 at 13:50
add comment

1 Answer

up vote 1 down vote accepted

Very difficult to answer given the information provided but you probably want ...

'has_archive'   => false,
'rewrite' => array('slug'=>'/','with_front'=>false),

... as part of your arguments when you register the post type and then you can add the post type to the main query for that home/front page...

function add_cpt_to_home($qry) {
  if (is_front_page() || is_home()) {
    $post_type = (array)$qry->get('post_type');
    $post_type[] = 'post';
    $post_type[] = 'project';
    $post_type = array_unique(array_filter($post_type));
    $qry->set('post_type',$post_type);
  }
}
add_action('pre_get_posts','add_cpt_to_home');

Or write a secondary query to pull that post_type.

$args = array(
  'post_type' => 'project'
);
$projects = new WP_Query($args);
if ($projects->have_posts()) {
  while ($projects->have_posts()) {
    $projects->the_post();
    the_title(); // etc
  }
}

Not tested very well, but as I said it is hard to answer given the information provided.

share|improve this answer
    
I added the extra arguments to project.php and post type code to index.php but the post is not showing up. https://github.com/martin-metsalu/public –  Martin Metsalu Jun 19 '13 at 15:14
    
Please post only the relevant code-- edit your question. No offense, but I don't have time to read through your whole project on github. –  s_ha_dum Jun 19 '13 at 15:17
    
Sorry about that. I now included only the relevant code on Github. –  Martin Metsalu Jun 19 '13 at 15:55
    
My guess would be that the conditions are wrong. That is, the is_home and is_front_page parts are not correct. I was guessing. Try without any conditions at all and see if it works, but don't leave it that way. Also, you don't need that filter at all if you are creating a new WP_Query object. –  s_ha_dum Jun 19 '13 at 19:40
    
Okay the content wasn't showing because it was inside a div which height was 0px. Called the content in using add the_content(). Everything is working fine now. Many thanks s_ha_dum ! –  Martin Metsalu Jun 20 '13 at 21:47
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.