• 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 visiter to a second page.

    Website: http://www.must.ee/uusmust/ad

    <?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 our projects and project specific data',
    		'public'        => true,
    		'menu_position' => 5,
    		'supports'      => array( 'title', 'excerpt'),
    		'has_archive'   => true,
    	);
    	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 );
Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter mmetsalu

    (@mmetsalu)

    I addedd

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

    to my argument and

    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');

    to my index.php file but the custom post is not showing up.

    Hi, mmetsalu.
    This kind of functions must be in functions.php of active theme, not in index.php.

    The right answer how to include CPT into loop depends on the source of theme, how the query of posts has been created, this is new WP_Query object or main query in index.php.
    Standart action could be

    function add_projects_to_query( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'post_type', array( 'post', 'project' ) );
        }
    }
    add_action( 'pre_get_posts', 'add_projects_to_query' );

    or (not is main query)
    if ( $query->is_home() && ! $query->is_main_query() ) {

    For WP_Query possible to add parameter like
    $my_query = new WP_Query( array( 'post_type' => array( 'post', 'project' ) );

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
    http://codex.wordpress.org/Class_Reference/WP_Query
    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

    Thread Starter mmetsalu

    (@mmetsalu)

    Hi vjpo.

    The project is a separate file called project.php and it’s called in functions.php file:

    <?php
    
    require 'project.php';
    
    ?>

    I 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();
      }
    }
    
        ?>
       </div>
    
        <?php get_sidebar(); ?>
        <?php get_footer(); ?>

    When I inspect the page I see that the title is actually brought in but for some reason it’s height is 0px. Plus the content from the editor box is not showing up. Do I need to call it like the_title(); was called?

    Yep, the_title() is correct. I found in the home page html warning in this place

    <!-- Start Project -->Testing!<br />
    Warning:  Missing argument 1 for the_editor(), called in index.php on line 312

    Argument 1 for the_editor function is $content – textarea content.

    Without the source it’s difficult to say something else.

    Thread Starter mmetsalu

    (@mmetsalu)

    I’ve added the relevant code on github here. And when I write the_editor($content); it brings in the whole editor metabox. How do I get it show only the content?

    “How do I get it show only the content?” you don’t need to call the_editor() , this is not mandatory function. You can try the_content() function instead.
    The problem could be in specific of your theme. Personally I never had a deel with the_editor() function on front end. Perhaps the theme has some filter that attaches a visual editor to post. If you will find this filter/function, you can remove filter for this “projects query” or to adapt the code (e.g. call before the loop remove_filter, remove_action or by another convenient way). It could resolve the warning that breaks the loop.

    Did you try to add the_content() right after the_title() ?

    Thread Starter mmetsalu

    (@mmetsalu)

    My bad, adding the_content() works as it should. I appreciate you helping out. Many thanks! Custom post types are not my speciality. I’ve got one last question which is not really related to the issue but how would you add a class to the custom post type so you could style it?

    For standard, multi-paged themes there is body_class() function, that adds body styles automatically.
    http://codex.wordpress.org/Function_Reference/body_class
    http://www.wpbeginner.com/wp-themes/wordpress-body-class-101-tips-and-tricks-for-theme-designers/

    Simplest way to add classes in custom project section in index.php is to do it right in the loop. E.g.

    while ($projects->have_posts()) {
        $projects->the_post(); ?>
        <div class="project-title"><?php the_title(); ?></div>

    This theme is grid based, use numbered span (from grid.css) to stylise it. Look how stylised photos of team, and I’am pretty sure that theme already has styles those you need for projects section. Look how is the posts section organised in the original index.php

    And it always possible to insert post type, slug, id, term into a final html output as a CSS class.
    <div class="span-3 post-<?php the_ID(); ?>">

    Thread Starter mmetsalu

    (@mmetsalu)

    Thanks again vjpo. I got it to look like I need it to by doing the following:

    <?php	  
    
    $args = array(
      'post_type' => 'project');
    $projects = new WP_Query($args);
    if ($projects->have_posts()) {
    
      while ($projects->have_posts()) {
        $projects->the_post(); ?>
    
    		  <div class="post item span4 isotope-item strategy">
    		  <a href="<?php bloginfo('template_url'); ?>/img/header1.jpg" class="project-wrp fancybox" rel="group1" title="Project!"><div class="profile-photo"><div class="profile-icon">f</div><?php echo types_render_field( "project-image", array( "alt" => "Project image",
    		  "width" => "370","proportional" => "true" )); ?></div> 
    
    		  <div class="project-name"><?php echo types_render_field( "project-title", array( "alt" => "Project title")); ?></div>
    			<div class="project-client"><?php echo types_render_field( "project-client", array( "alt" => "Project client")); ?></div></a></div>
    
    	<?php
       }
      }
      ?>

    But now the image is opening the templates sample image, it’s something I’ll look into. Now the only thing I’m still struggeling with are the categories I created in the projects admin panel. I have this just before the post code:

    <?php
    
    $orderby = 'name';
    $show_count = 0; // 1 for yes, 0 for no
    $pad_counts = 0; // 1 for yes, 0 for no
    $hierarchical = 1; // 1 for yes, 0 for no
    $taxonomy = 'genre';
    $title = '';
    
    $args = array(
      'orderby' => $orderby,
      'show_count' => $show_count,
      'pad_counts' => $pad_counts,
      'hierarchical' => $hierarchical,
      'taxonomy' => $taxonomy,
      'title_li' => $title
    );
    ?>
    <ul>
    <?php
    register_taxonomy( 'project_category', 'project', $args );
    wp_list_categories($args);
    ?>
    </ul>

    But no errors or nothing.

    The second code is messy. register_taxonomy() uses the same $args variable that contains arguments for wp_list_categories().
    All functions those works not only in index.php, but on all theme templates must be placed in functions.php. So put register_taxonomy() and register_post_type() into the functions.php.

    About preview images – http://codex.wordpress.org/Post_Thumbnails
    I don’t know if your theme supports it already, if yes, there must be something like
    add_theme_support( 'post-thumbnails' ); in the code.

    I would like to start a post with a reader comment and follow with administrator content (versus admin content followed by reader comments/replies).

    Can a similar approach be used to do this?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Custom post type and displaying it on your main page.’ is closed to new replies.