Custom Post Types


Hi

I've been building basic themes for WP sites for a little while now but I've only just started trying to learn a framework and decided I would try Beans out before I spent money on a theme like Genesis. My developer skills are basic so I hope someone can help me get started here.

I've registered a custom post type via "functions.php" in my child theme but I can not get it to display on the frontend. I have read about including a custom template like "category-mycpt.php" or "archive-mycpt.php" but I'm not sure what code I would put in there and whether that is the most efficient way of doing it.

Currently if I click on any of the parent categories for my CPT I just get the "Whoops, no result found!" and the search bar. I would just like them to display like standard posts.

My main reason for trying to learn to use a framework is so I can try and take advantage of the built in resposiveness and SEO so it seems a bit counter intuitive to have to write a lot of my own code in a template file.

Any help to get started on this would be apprietiated thanks


You have to create custom wp_query for that ... There are many ways to achieve what you are trying to do ... The best way would be like this ...

beans_modify_action_callback( 'beans_loop_template', 'my_custom_loop' );

function my_custom_loop() {
  // write your custom querry here
}

Hey @sibience,

There's no need to create a custom loop to display custom post types.

After adding the code that register your custom post type, and having created one or two custom post types, make sure that you have refreshed your permalinks (Settings=>Permalinks=>click "Save").

Now, if you go to archive-mycpt.php, you should see your CPT.

Hope it helps.

Mathieu


Thanks for the replies.

I've already tried refreshing the permalinks with no change. All my custom post categories show up in the category widget but none of my custom posts attached to those categories apear.

I duplicated the index.php from the main Beans directory and tried renaming it to archive-mycpt.php and I also tried category-mycpt.php with no luck. Maybe I did something wrong? I am still very new to using frameworks.

If I go to the URLs for custom posts the posts are there but they don't seem to be attached to any categories. At the bottom where it says "filed under" it lists the categories the post is supposed to be attached to, but if I click one it still just shows me the "Whoops, no result found!" page.

I will look into CodeFactry's solution and see if i can get anywhere with that.

Thanks!


Ok after some time playing around with this I realised I needed to add my CPT to categories using this.

// Adds custom posts to categories
add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {
    if( !is_admin() ) {
        if ( !is_post_type_archive() && $query->is_archive() ) {
            $query->set( 'post_type', array( 'post', 'myCPT' ) );
        }
        return $query;
    }
}

One more unrelated question, is it possible to remove the archive link from the breadcrumb?


Hey Keith

I am not able to get my Custom Post Type categories and tags to show in the CPT category archive pages. Could you possibily share more details on how you got this working?

With below code I am able to show the default post meta. Such as date, author and add a comment but not the CPT categories and tags. https://webkonsulenten.com/nettmagasin-arkiv/

// Shows post meta on custom post archive/blog pages.
add_filter( 'beans_pre_post_meta', 'yourthemerpefix_keep_post_meta' );
function yourthemerpefix_keep_post_meta( $pre ) {
    // Don't short-circuit for 'YOURCUSTOMPOSTTYPE' post type.
    if ( 'movie' === get_post_type() ) {
        return false;
    }
    if ( 'nettmagasin' === get_post_type() ) {
        return false;
    }   
    return $pre;
}

Mathieu shared the above code this post: https://community.getbeans.io/discussion/post-meta-not-showing-up-on-frontend-with-custom-post-types/

Thank you.


Hey Paal

I'm no expert on this but I think it may have something to do with how your CPT is set up.

It's been a while since I built that site but I don't remember having to do anything to get categories and tags to show up with my CPT.

I guess you could try adding it manually in the archive or category template.


Hey Keith. I received some awesome help from Mat. Here is the solution that is working for the custom post type that I made.

I added two custom post types one called edwien and the other nettmagasin. (It's in Norwegian). Then added the following:

 // Don't short-circuit for 'job' post type.
add_filter( 'beans_pre_post_meta', 'example_pre_post_meta' );

function example_pre_post_meta( $pre ) {
  if ( 'edwien' === get_post_type() ) {
    return false;
 }

 if ( 'nettmagasin' === get_post_type() ) {
    return false;
 }

 return $pre;
}

The custom post type meta did not show up so Mat gave me the following code and I inserted the following:

// Add custom taxonomy.
add_action( 'beans_post_meta_prepend_markup', 'add_event_taxonomy' );
function add_event_taxonomy() {

   $edwiencategories = get_the_terms( get_the_id(), 'edwien-kategori' );
    $edwientags = get_the_terms( get_the_id(), 'edwien-stikkord' );
    $nettmagasincategories = get_the_terms( get_the_id(), 'nettmagasin-kategori' );
    $nettmagasintags = get_the_terms( get_the_id(), 'nettmagasin-stikkord' );

    if ( is_array( $edwiencategories ) ) {
        foreach ( $edwiencategories as $edwiencategory ) {

                echo '<li><a href="' . get_term_link( $edwiencategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $edwiencategory->name ) . '" ' . '>' . $edwiencategory->name.'</a></li> ';
        }
    }

    if ( is_array( $edwientags ) ) {
            foreach ( $edwientags as $edwientag ) {

                    echo '<li><a href="' . get_term_link( $edwientag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $edwientag->name ) . '" ' . '>' . $edwientag->name.'</a></li> '; 
        }
    }

    if ( is_array( $nettmagasincategories ) ) {
            foreach ( $nettmagasincategories as $nettmagasincategory ) {

                    echo '<li><a href="' . get_term_link( $nettmagasincategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $nettmagasincategory->name ) . '" ' . '>' . $nettmagasincategory->name.'</a></li> ';   
        }
    }

    if ( is_array( $nettmagasintags ) ) {
            foreach ( $nettmagasintags as $nettmagasintag ) {

                    echo '<li><a href="' . get_term_link( $nettmagasintag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $nettmagasintag->name ) . '" ' . '>' . $nettmagasintag->name.'</a></li> ';  
        }
    }
}

The above code then adds edwien category and a tag, and then the nettmagasin category and tag. I am still working on a few things around these custom post types and will also be producing a tutorial. I will then later on either edit this post or add another with a link to a tutorial.

Write a reply

Login or register to write a reply, it's free!