skip to Main Content

can someone help me with this code.
I want to display multiple archives on my front page.

I have custom archives – "archive-gemer.php" and "archive-spis.php".
My page is currently set to "Your homepage displays Your homepage displays Your latest posts".
I would like to see the content from both archives on the main page.

I tried to create something like this:
(function.php)


function ql_is_front_page( $query = null ) {
    if ( ! $query ) {
        global $wp_query;
        $query = $wp_query;
    }
    $front_page_id = get_option( 'page_on_front' );
    return (bool) $front_page_id === (bool) $query->get_queried_object_id();
}

add_action( 'after_setup_theme', 'my_custom_front_page' );
function my_custom_front_page() {
    add_action( 'pre_get_posts', 'ql_set_as_front_page' );
}

function ql_set_as_front_page( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    if ( ql_is_front_page( $query ) ) {
        $query->set( 'page_id', '' );
        $query->is_page = false;
        $query->is_singular = false;
        
        if ( $query->is_post_type_archive( array( 'gemer', 'spis' ) ) ) {
            $query->set( 'post_type', array( 'gemer', 'spis' ) );
            $query->is_archive = true;
            $query->is_post_type_archive = true;

            ob_start();
            include( 'archive-gemer.php' );
            $archive_gemer_content = ob_get_clean();

            ob_start();
            include( 'archive-spis.php' );
            $archive_spis_content = ob_get_clean();

            echo '<div class="content-select-regions">' . $archive_gemer_content . $archive_spis_content . '</div>';
        }
    }
}

on front-page.php

<?php get_template_part( 'archive', 'gemer' ); ?>
<?php get_template_part( 'archive', 'spis' ); ?>

archive-gemer.php

<h1>Gemer Content</h1>

<?php if ( have_posts() ) : ?>
  <?php while ( have_posts() ) : the_post(); ?>
    <div>
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    </div>
  <?php endwhile; ?>
<?php endif; ?>

archive-spis.php

<h1>Spis Content</h1>

<?php if ( have_posts() ) : ?>
  <?php while ( have_posts() ) : the_post(); ?>
    <div>
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    </div>
  <?php endwhile; ?>
<?php endif; ?>

However, on the main page it only prints HTML tags, but nothing from wordpress.

Can someone help me how to set in function.php so that both archives are listed?

Thank you for answear.


Please signup or login to give your own answer.
Back To Top
Search