WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type Query (1 post)

  1. winnard
    Member
    Posted 1 year ago #

    Hi Guys,

    So I am creating a theme for a client. I am fairly new to WordPress, I know how to create basic themes and to get them to work using the default wordpress way of pages, posts etc.

    This client however requires that their home page has three content blocks which they can alter. I have set this up by creating a custom post type called 'content-block'. I have then registered taxonomy called 'content-position' for my content to go in 'left-block', 'center-block' and 'right-block'. There are three blocks on the page aligned next to each other left, center and right.

    I have everything working beautifully, allowing the user to add a featured image, title and snippet of text.

    My issue is the economy of the code I am using to drag that info onto the page. At the moment I am querying the database three times to pull posts with left-block, center-block, right-block onto the page like the following:

    <br />
    <section class="content-blocks-container"><br />
    		<div class="left-block"><br />
    			<?php $content_block = new WP_Query(array('post_type'=>'content-block',<br />
    			  'posts_per_page'=>1, 'content-position'=>'left-block'))?><br />
    			<?php if($content_block->have_posts()): $content_block->the_post(); ?><br />
    			<?php the_post_thumbnail(); ?><br />
    			<h3><?php the_title();?></h3><br />
    			  <?php the_content();?><br />
    			<?php endif; ?><br />
    		</div><br />
    		<div class="center-block"><br />
    			<?php $content_block = new WP_Query(array('post_type'=>'content-block',<br />
    			  'posts_per_page'=>1, 'content-position'=>'center-block'))?><br />
    			<?php if($content_block->have_posts()): $content_block->the_post(); ?><br />
    			<?php the_post_thumbnail(); ?><br />
    			<h3><?php the_title();?></h3><br />
    			  <?php the_content();?><br />
    			<?php endif; ?><br />
    		</div><br />
    		<div class="right-block"><br />
    			<?php $content_block = new WP_Query(array('post_type'=>'content-block',<br />
    			  'posts_per_page'=>1, 'content-position'=>'right-block'))?><br />
    			<?php if($content_block->have_posts()): $content_block->the_post(); ?><br />
    			<?php the_post_thumbnail(); ?><br />
    			<h3><?php the_title();?></h3><br />
    			  <?php the_content();?><br />
    			<?php endif; ?><br />
    		</div><br />
    		<br class="clear"><br />
    	</section><br />

    I was wondering if there was a cleaner way to do this where by I could run this query once, and then in each container put for example get_taxonomy('left-block')??? That way I am only querying the database once but pulling out the right content to go in the right position??

    Any help would be great. I know it works fine as it is but I would like a cleaner solution if there is one to pull this info out into the correct position.

    Many thanks in advance

    Dan

    [No bumping, thank you.]

Topic Closed

This topic has been closed to new replies.

About this Topic