0

I am working with underscores me trying to build a template by my own.

I have build a homepage template and also a single-post file for the custom post types.

What I am trying to get its to display in the homepage a kind of gallery with the featured images from each custom post type with title and tags.

Have tried different ways, i guessed it would be something with wp_get_archives but still didn't get it.

If you have some suggestions would be awesome.

Thanks!

2 Answers 2

0

Using wp_get_archives will return a pre-defined template for the results. You need your own custom query and template to do that. Since i don't know what exactly you are looking for, I'll have a basic code suggestion for your situation:

// Get a list of categories
$terms = get_terms(array('taxonomy' => 'category')); 
// Run a query for each category
foreach ($terms as $term){
    $args = array(
    'post_type' => 'custom_type', 
    'posts_per_page' => '1',
    'order_by' => 'date', 
    'order' => 'ASC', 
    'cat' => $term->ID
    );
    $new_query = new WP_Query ($args);
    if ($new_query->have_posts()) {
        while($new_query->have_posts()){
            $new_query->the_post();
            // Post's title
            the_title();
            // Post's featured image
            the_post_thumbnail('thumbnail');
        }
    }
    wp_reset_postdata();
}

This will list 1 post out of each category, and output the featured image and its title. However it will make a lot of queries if you have a lot of categories. ( like 50 or so ). I suggest you use it with a cache plugin.

You can also get a list of posts and view them in a showcase, alongside their category name:

$args = array(
'post_type' => 'custom_type', // Your custom post type
'posts_per_page' => '8', // Change the number to whatever you wish
'order_by' => 'date', // Some optional sorting
'order' => 'ASC', 
);
$new_query = new WP_Query ($args);
if ($new_query->have_posts()) {
    while($new_query->have_posts()){
        $new_query->the_post();
        the_title();
        the_post_thumbnail('thumbnail');
        // Get a list of post's categories
        $categories = get_the_category($post->ID);
        foreach ($categories as $category) {
            echo $category->name;
        }
    }
}
wp_reset_postdata();
0

You can use the wordpress loop which is available in any theme out there. Here is the basic code snippet. Copy this in your homepage file and see if the outcome is as expected:

<?php if( have_posts() ) : ?>

  <?php while (have_posts()) : the_post(); ?>

    <div class="blog">
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <hr />          
        <div class="text-box">

          <?php
            if( has_post_thumbnail() ) { 
                echo '<div style="float:left;">';
                the_post_thumbnail('thumbnail');
                echo '</div>';
            }

          ?>

        </div>
    </div>

  <?php endwhile; ?>

<?php endif; ?>

It's the code from my child theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.