WordPress.org

Ready to get started?Download WordPress

Forums

3 questions on using Custom Post Types (10 posts)

  1. Dan
    Member
    Posted 4 months ago #

    Hello,
    I am trying to use custom post types(CPTs) so as to create "sections" on my site.
    In the process, I encountered three issues:

    1/It appears I can add a post in a CPT format into a category. However, when I go to the category archive page (example.com/category/art), it displays only posts belonging to that category in the default post formats. Is it an inherent limitation or is there a way to fix it?

    2/I wish to customize the CPT archive page(ideally the header and sidebars). Is this impossible/difficult or is there an easy work around?

    3/It seems there is only one possible permalink option for posts with CPT. That is: example.com/cpt-name/post-name/
    On the otherhand, some WP experts advise using a permalink should contain atleast "year" (see here). I also remember reading a WP expert's comment on this forum that the "year/month/date" permalink is recommended.
    Is this an outdated concern or not a big deal? Or, will it be somehow offsetted by the use of CPTs?

    Thanx in advance.

  2. Dan
    Member
    Posted 4 months ago #

    One more thing: Is it possible to subscribe (via rssfeed or email) to a single CPT posts archive(example.com/cpt-name/feed/)?

  3. Koliu
    Member
    Posted 4 months ago #

    Hey Dan,

    1. You have to create custom taxonomy (in other words custom categories) for your post type. CPT cannot be assigned to default taxonomy such as post categories.

    2. If for example your CTP is called "portfolio", then you have to create a "archive-portfolio.php" file into your theme directory. Just copy the source from the default archive.php/index.php file and make the changes you have in mind.

    3. In my experience, it all depends of how popular your site is. The main problem with the pretty permalinks is that they make your website slower. If you don't have big traffic you normally won't need to worry about that. However, you should be able at least to add an ID to your permalinks.

    4. Yep, http://example.com/feed/?post_type=cpt-name

    Niki

  4. Dan
    Member
    Posted 4 months ago #

    Thank you very much for your help.
    However, I am confused on one point.

    *You said:

    "CPT cannot be assigned to default taxonomy".

    Did you mean I shouldn't? or a bad idea?

    Because, if you create a CPT using "Custom Post Type UI version: 0.8.2", there is "Advanced Options" - "Built-in Taxonomies".
    It lets you decide to enable the default categories and tags for CPT.

    Thank you again.

  5. Koliu
    Member
    Posted 4 months ago #

    Hey,

    No worries.

    As for #1, I misunderstood your question at first, so your confusion is actually my fault :) Yes, you can do that, it's not a bad idea, neither bad practice.

    Best,
    Niki

  6. Dan
    Member
    Posted 4 months ago #

    Thanx.
    Then, how do I make the CPT posts show up on the archive page of the default category?

    For example: I wrote a post and published it in a CPT and assigned it to a custom taxonomy called "painting" and also to a default category called "art".

    The post can be found on:
    * example.com/CPT-name/
    * example.com/custom_taxonomy/painting
    However, it is not visible on example.com/category/art

    How do I fix that?

  7. Koliu
    Member
    Posted 4 months ago #

    In order to fix that, you have to edit your theme's category.php file. If there's no such file, then you have to edit archive.php or index.php if there also isn't archive.php one.

    Find the following (or something similar to it):

    while ( have_posts() ) : the_post();

    and just before that line add the following piece:

    global $wp_query;
    $custom_args = array(
    	'post_type' => array( 'post', 'your-custom-post-type' )
    );
    query_posts( array_merge( $wp_query->query_vars, $custom_args ) );

    Hope that helps,
    Niki

  8. Dan
    Member
    Posted 4 months ago #

    I will try that and get back to you/this thread ASAP.

    Thank you for your kindness.

  9. Koliu
    Member
    Posted 4 months ago #

    No worries :)

  10. Dan
    Member
    Posted 3 months ago #

    Hello Koliu,
    I been trying the codes you gave me in the index.php (I am using theme piboard and in the wp-includes directory there is no category.php and archive.php.)

    global $wp_query;
    $custom_args = array(
    	'post_type' => array( 'post', 'your-custom-post-type' )
    );
    query_posts( array_merge( $wp_query->query_vars, $custom_args ) );

    However, it is not working for me.
    When I add it into the index.php, I see the codes themselves on the front page AND the category page don't display my CPTs.

    Is there is something I should have added above or below the codes?

    [Alternatively, can you give me a code I can use in the functions.php? For example, I added a simple code in the fnctions.php and it let me display CPTs in the home page without ado.]

Reply

You must log in to post.

About this Topic