• I create a custom post and a page to post a specific content.

    My functions.php:

    add_action( ‘init’, ‘create_post_fotos’ );

    function create_post_fotos() {
    register_post_type( ‘Fotos’,
    array(
    ‘labels’ => array(
    ‘name’ => __( ‘Fotos’ ),
    ‘singular_name’ => __( ‘Fotos’ )
    ),
    ‘public’ => true,
    ‘has_archive’ => true,
    ‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’ ),
    ‘taxonomies’ => array( ‘category’, ‘post_tag’ )

    )
    );

    }

    I can post the content and usually create any category, but when I open my site and I click on the category you just created, nothing is displayed.

    This problem is occurring ONLY on custom post that I created. The categories that create in index work perfectly.

    How can I solve this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • “Category” pages that are constructed using the category.php template only look at regular ‘posts’, not custom post types.

    If you want to show both your ‘fotos’ posts along with blog posts if they share the same ‘category’ that’s assigned, then you can try this:

    Open category.php – back it up in some method before modifying it in case you make a mistake, change your mind, or don’t like how it behaves.

    below the “if have posts’ statement, you’ll create a new query that looks for both posts and fotos like so:

    <?php if (have_posts()) :
    $args = array(
     'post_type' = array('post','fotos'),
    );
    $the_query = new WP_Query( $args);
    ?>

    Below that look for your ‘while have posts’ stuff and modify it to use your new query (“the_query”) like so:

    while ( $the_query->have_posts() ) :
    	$the_query->the_post();

    Then it should continue on with how it displays the posts.

    Post back if this doesn’t work but be specific about your results.

    without editing theme templates and without changing the query, try to use the 'pre_get_posts' action;
    https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    related https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts#Include_Custom_Post_Types_in_Search_Results

    example code to include a custom post type into category archive page (code goes into functions.php of your (child) theme):

    function custom_post_in_category_archive($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_category) {
          $query->set('post_type', array( 'post', 'fotos' ) );
        }
      }
    }
    
    add_action('pre_get_posts','custom_post_in_category_archive');

    (not tested)

    Thanks Michael – that’s a much better solution!

    Thread Starter gtw7375

    (@gtw7375)

    Thanks Michael, it worked!

    But, why I should create a page category.php? What is the purpose of it?

    The template file ‘category.php’ is what WP uses to display a list of all posts in (any given) category.

    SO if someone clicks on a category link (from a menu perhaps, or a list of categories like those at the end of a post) in order to view all content that is ‘categorized’ with that specific category, WP shows them a page using the category.php template file.

    The code Michael supplied is a cleaner way to add your custom post type (‘fotos’) to the default loop query that WP does when gathering the posts to display on the category page. Without adding your CPT, WP will just display normal ‘posts’.

    Thread Starter gtw7375

    (@gtw7375)

    Thanks for the explanation. I also will search more about it to learn more and more (;

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Taxonomies not display the content’ is closed to new replies.