How to show post meta on custom post type in home.

  • Author
    Posts
  • #122713

    Here a picture show post meta, top one is default and bottom one is custom post type.

    I’m using custom snippet to display custom post type on home, here the code.

    add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
     
    function add_my_post_types_to_query( $query ) {
        if ( is_home() && $query->is_main_query() )
            $query->set( 'post_type', array( 'post', 'rilisan' ) );
        return $query;
    }
    #123411
    Zed
    Cryout Creations mastermind

    Hi,

    You might need to duplicate the theme’s content/content.php file and rename it to match your post type and then edit the if ( 'post' == get_post_type() ) check in its code to include your post type.


    If you like our creations, help us share by rating them on WordPress.org.
    Please read the available documentation and search the forums before posting.
    #123532

    Hello again,

    I already duplicate content/content.php to content/rilisan.php and edit post type to match to match it, if ( 'post' == get_post_type() ) to if ( 'rilisan' == get_post_type() ) and result still same, only work on custom archive not for home archive.

    But when i look at index.php and edit <?php get_template_part( 'content/content', get_post_format() ); ?> to <?php get_template_part( 'content/rilisan', get_post_format() ); ?> result are normal post didn’t show post meta and custom post type show post meta.

    Here picture, after editing index.php
    mantra1

    Is it possible to get multiple template part for home in index.php?
    So I can add default post and custom post content in content folder.

    #123533
    Zed
    Cryout Creations mastermind

    The default behaviour is to handle the default post type. If you edit the existing code and replace the checks for your custom post type you’re then excluding the default post type.

    In any instances you’re editing type checks, you’d need to include your post type next to the default ‘post’ one, for example by replacing if ( 'post' == get_post_type() ) with
    if ( in_array( get_post_type(), array( 'post', 'rilisan' ) ) )

    You could also give this plugin a try in case it accomplishes the same result without the need for manual code modifications.

    Otherwise, it may be needed to also make use of WordPress’ template hierarchy, which lets you define separate archive-SLUG.php and/or category-SLUG.php files (and others) for each post type.


    If you like our creations, help us share by rating them on WordPress.org.
    Please read the available documentation and search the forums before posting.
    #123537

    Okay, I replace if ( 'post' == get_post_type() ) with if ( in_array( get_post_type(), array( 'post', 'rilisan' ) ) ) in content/content and it works, I guess I need read more about wordpress codex.
    mantra2

    Thank you.

Viewing 5 posts - 1 through 5 (of 5 total)

You need to log in to reply to this topic.