0
votes

I am working with a child theme of the twothousand-seventeen-theme because I would like to change a few things. But I've got a problem in realizing this one idea:

I have created a custom post type named 'video' but on the index-page (index.php) I want to show both types of posts, the default post-type and the custom 'video' post-type, ordered by publishing date.

Since I really like the way the index.php features the latest posts a don't really want to create a custom archive (I guess I have to keep the get_template_part() function for that). All I wanna do is to show both post-types on the index page.

<?php if ( have_posts() ) : ?>
    <header class="page-header">
        <?php
            the_archive_title( '<h1 class="page-title">', '</h1>' );
            the_archive_description( '<div class="taxonomy-description">', '</div>' );
        ?>
    </header><!-- .page-header -->
<?php endif; ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php
    if ( have_posts() ) : ?>
        <?php
        /* Start the Loop */
        while ( have_posts() ) : the_post();

            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'template-parts/post/content', get_post_format() );

        endwhile;

        the_posts_pagination( array(
            'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
            'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
        ) );

    else :

        get_template_part( 'template-parts/post/content', 'none' );

    endif; ?>

I've tried many ways but none of them worked for my idea... There's got to be a way to select both post-types in the wp-query and show them on the index page, ordered by publishing date and in the default content template. Like to select all posts (of post and video type) for the get_post_format() function (but i am not really into PHP)

I appreciate any idea.

Regards, Malte

1

1 Answers

3
votes

You can do like this, In your index.php

Put the below code where you want to display all the posts title and content.

<?php
$args = array(
        'post_type' => array( 'post', 'video' ),
        'orderby' => 'date',
    );
$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <div class="smal-sec">
        <h3><?php the_title();?></h3>
        <?php the_content();?>
    </div>
<?php
endwhile;
endif;
wp_reset_query();  // Restore global post data stomped by the_post().
?>

From this loop we will able to get all posts form default post and "video" custom post type. Posts will display according to date.