WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get specific post in a template (2 posts)

  1. grosbouff
    Member
    Posted 6 years ago #

    Hello, I would like to show a specific post (I know its ID) in a template.
    I was using the plugin get-a-post, but there is a problem when the rich text editor when I use it.
    I read in the CODEX that there were a "get_post" function in wordpress, I've read the doc but it's seems a little vague.
    I would like to know if you could tell me how I can use it.

    My previous template code (with get-a-post) was :

    get_a_post(145);
    	?>
    
    	<div class="post-intro" id="intro">
    		<h3><?php the_title();?></h3>
    		<?php the_content(); ?>
    		<p class="postmetadata"><?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    
    	</div>

    How can I do the same without the get-a-post plugin ?
    (this is outside the loop)

    I also see there is a query_posts function. But I think if I use it, the other "default" posts will not be shown anymore, i'm I wrong ? I think this function restricts the query to one post, and what I want is one specific post on the top of the page, then the others...

    Thanks !

    BenoƮt

  2. grosbouff
    Member
    Posted 6 years ago #

    Ok, I founded how, with the query_posts function.
    This is how I made to have a specific page on the top of any category.
    The idea is to add another loop on the top of the code, in the template.
    So we have two loops : one to show the wanted page, and the other to show the regular posts.

    We use the query_posts function to tell the two loops what it has to load.

    The trick is, if you use query_posts to show one specific page; the query is now restricted and will not show the other posts anymore.
    So we have to use this function twice : once to catch the post and once to "reset" the query.
    Here, I want to have a specific post for each category.
    So I have to redefine the query once the post has been show, to fetch the category posts again.
    Here's my code :

    <?php
    	if (is_home()) {
    		$post_intro = 121;
    	} elseif (is_category('3')) {
    		$post_intro = 121;
    	} elseif (is_category('11')) {
    		$post_intro = 145;
    	} elseif (is_category('12')) {
    		$post_intro = 146;
    	} elseif (is_category('13')) {
    		$post_intro = 142;
    	} elseif (is_category('17')) {
    		$post_intro = 127;
    	}	
    
    	if ($post_intro) :
    		query_posts('page_id='.$post_intro);
    
    		if (have_posts()) : 
    
    			while (have_posts()) : the_post();?>
    
    			<div class="post-intro" id="intro">
    				<h3><?php the_title();?></h3>
    				<?php the_content(); ?>
    				<p class="postmetadata"><?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    
    			</div>
    			<?php endwhile;
    		endif;
    
    		query_posts('cat='.$_GET['cat']); //this is for reseting the former loop
    	endif;
    
    	?>
    
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post();
    
    		?>

Topic Closed

This topic has been closed to new replies.

About this Topic