• Resolved Cskinner387

    (@cskinner387)


    Hey guys,

    Just wondered if anyone can help me please,
    i have created a custom post called “book” and have entered content on that custom type i am trying to display the 4 most recent posts on my front page along with their thumbnails but what every i seem to add makes my front page break what am i doing wrong?

    this is my code currently that works

    <div id=”container”>
    <div id=”content”>
    <?php
    $type = ‘book’;
    $args = array (
    ‘post_type’ => $type,
    ‘post_status’ => ‘publish’,
    ‘paged’ => $paged,
    ‘posts_per_page’ => 4,
    ‘ignore_sticky_posts’=> 1
    );
    $temp = $wp_query; // assign ordinal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    if ( $wp_query->have_posts() ) :
    while ( $wp_query->have_posts() ) : $wp_query->the_post();
    echo ‘<h2>’;
    the_title();
    echo ‘</h2>’;
    echo ‘<div class=”entry-content”>’;
    the_content();
    echo ‘</div>’;
    endwhile;
    else :
    echo ‘<h2>Not Found</h2>’;
    get_search_form();
    endif;
    $wp_query = $temp;
    ?>
    </div><!– #content –>
    </div><!– #container –>

    i really want to add the thumbnail above the <h2> code but i cant seem to make it work any way i do it

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi there,

    I put together a quick custom post type called ‘test_post’ and pulled 3 of them, with their featured images, titles and excerpts on the front page.

    Here is what it looks like:

    I didn’t work too much on the styling and the image sizes need work, but you get the idea! ??

    Here is the code that I used to display it on the front-page:

    Hope this is helpful to you!

    Thread Starter Cskinner387

    (@cskinner387)

    Thanks redrambles

    the code you gave me works fantastically well, i take it i can add this as many times i need for pulling in custom posts on my front page by just changing the post type slug? for eg testimonials or books or cars so on.

    Also i am having problems pulling the posts into line unless i am using the wrong div to pull them into line with each other, how did you manage to do yours?

    Many thanks for your kind help

    Hi there,

    Yes – you should be able to use that code block as much as you like -> as long as you don’t forget to reset the query (wp_reset_query) you should be all set.

    As for the styling – here is some of what I used (there is other styling applied to the outermost divs, but this following might still be helpful to you.

    Happy WordPressing! ??

    Thread Starter Cskinner387

    (@cskinner387)

    Ok just one last question, i have tried to attach a link to the post so you can click the thumbnail to got to the post but it wont add the link properly for some odd reason.

    sorry for all the questions i have started working for a charity that uses wordpress and wants me to update their website for them but i am a complete newbie to wordpress

    was trying to do it like this but i lose the images and it wont link to anything

    <figure><a href="<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail('thumbnail'); ?>"></a></figure>

    `

    You were very close! Try this:

    <figure>
    	<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    </figure>

    And for the record – you are creating custom post types and custom queries to pull in specific content on specific pages – this is definitely not ‘complete newbie to WordPress’ material! You are doing great! Good luck with the site.

    Thread Starter Cskinner387

    (@cskinner387)

    Hey there redrambles
    just wondered if you could help me again qiuckly if possible
    i have this piece of code from a website that im working on but i have a cuatom post like above that i want to add to it but can figure out where to enter the details for it as it has been written completly different to yours above.

    <div class="container" role="main">
            <div class="<?php echo apply_filters( 'tc_column_content_wrapper_classes' , 'row column-content-wrapper' ) ?>">
    
                <?php do_action( '__before_article_container'); ##hook of left sidebar?>
    
                    <div id="content" class="<?php echo tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' ) ?> article-container">
    
                        <?php do_action ('__before_loop');##hooks the heading of the list of post : archive, search... ?>
    
                            <?php if ( tc__f('__is_no_results') || is_404() ) : ##no search results or 404 cases ?>
    
                                <article <?php tc__f('__article_selectors') ?>>
                                    <?php do_action( '__loop' ); ?>
                                </article>
    
                            <?php endif; ?>
    
                            <?php if ( have_posts() && !is_404() ) : ?>
                                <?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
                                    <?php the_post(); ?>
    
                                    <?php do_action ('__before_article') ?>
                                        <article <?php tc__f('__article_selectors') ?>>
                                            <?php do_action( '__loop' ); ?>
                                        </article>
                                    <?php do_action ('__after_article') ?>
    
                                <?php endwhile; ?>
    
                            <?php endif; ##end if have posts ?>
    
                        <?php do_action ('__after_loop');##hook of the comments and the posts navigation with priorities 10 and 20 ?>
    
                    </div><!--.article-container -->
    
               <?php do_action( '__after_article_container'); ##hook of left sidebar ?>
    
            </div><!--.row -->
        </div><!-- .container role: main -->

    Thanks in advance

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Looping custom posts on front page’ is closed to new replies.