• Resolved gbennyb

    (@gbennyb)


    i am pretty new in PHP and WordPress coding (main area so far css, xml and html), but now i need a special homepage post display (in my case (p=51). i have the following code integrated in index-php, but only the title gets displayed. After 24 hours checking posts and google reults, i hope you can help me.

    <?php if (is_home()) {
    query_posts(‘p=51’);
    ?>

    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h3 id=”post-header”><?php the_title(); ?></h3>

    <div class=”entry”>
    <?php the_content(); ?>
    </div>

    <?php if ($post->post_excerpt != ”) { ?>

    <div class=”excerpt”>
    <?php echo $post->post_excerpt; ?>
    </div>
    <?php } ?>

    </div>
    <?php } ?>

    Why is only the the_title() getting displayed, but not the_content()?
    Any hint/idea?

    I know this is pretty low level, and i don’t want to annoy anybody, but i am really stuck with this issues.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You’ve omitted the Loop code!

    Try:

    query_posts('p=51');
    if ( have_posts() ) : while ( have_posts() ) : the_post();

    and don’t forget to finish off the Loop at the bottom:
    <?php endwhile; endif;?>

    Thread Starter gbennyb

    (@gbennyb)

    ok, i am sorry, i don’t get it, but this maybe, because i did not tell you the full story – so absolutely my fault, here is the full code of my index.php:

    <?php if (is_home()) {
    		query_posts('p=51');	?>
    
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h3 id="post-header"><?php the_title(); ?></h3>
    
    		<div class="entry">
    			<?php the_content(); ?>
    		</div>
    
    	<?php if ($post->post_excerpt != '') { ?>
    
    		<div class="excerpt">
    			<?php echo $post->post_excerpt; ?>
    		</div>
    		<?php } ?>
    
    	</div>
    	<?php } ?>
    
    		<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    		    <?php
              // check if the post belongs directly to the current category;
              // continue if not
              $current_categories = get_the_category();
              $found_category = false;
              foreach ($current_categories as $current_category) {
                if ($current_category->term_id == $cat) {
                  $found_category = true;
                  // counting level
                  $level = 0;
                  while ($current_category->parent != 0) {
                    $current_category = get_category($current_category->parent);
                    $level = $level + 1;
                  }
                  break;
                }
              }
              if (!$found_category) {
                continue;
              }
            ?> 
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<h3 id="post-header"><?php the_title(); ?></h3>
    
    				<div class="entry">
    					<?php the_content() ?>
    				</div>
    
    			<?php if ($post->post_excerpt != '') { ?>
    
    				<div class="excerpt">
    
    					<?php echo $post->post_excerpt; ?>
    
    				</div>
    			<?php } ?>
    
    			</div>
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    	</div>

    so basically in understand what you are trying to do, but i don’t understand where to integrate it, especially the “endwhile”. I tried it all, but i get errors all the time.

    Thread Starter gbennyb

    (@gbennyb)

    also strange is the fact the i get “the_title”, just not “the_content” and the “post_excerpt” – that really confuses me.

    There’s still no Loop in that first section though. But having seen the full code, rather than getting into multiple Loops, you might find it easier to use get_posts for the first post only and then a simple foreach loop (yes – even though there’s only 1 result in the array) to display the post content.

    So, instead of:

    query_posts('p=51');	?>
    	<div class="post" id="post-<?php the_ID(); ?>">
    	<h3 id="post-header"><?php the_title(); ?></h3>
    	<div class="entry">
    		<?php the_content(); ?>
    	</div>
    <?php if ($post->post_excerpt != '') { ?>
    	<div class="excerpt">
    		<?php echo $post->post_excerpt; ?>
    	</div>
    	<?php } ?>
    </div>
    <?php } ?>

    try:

    <?php
    $topposts = get_posts('p=51');
    foreach($topposts as $post) :
    setup_postdata($post);
    ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    	<h3 id="post-header"><?php the_title(); ?></h3>
    	<div class="entry">
    		<?php the_content(); ?>
    	</div>
    <?php if ($post->post_excerpt != '') { ?>
    	<div class="excerpt">
    		<?php echo $post->post_excerpt; ?>
    	</div>
    	<?php } ?>
    </div>
    <?php endforeach; ?>
    Thread Starter gbennyb

    (@gbennyb)

    you are the king! i will put this to resolved as this works magnifically.

    but i will have to check your functions still to understand it better.
    So setup_postdata is new to me and i never used “foreach” as well.

    But again, thank you very much esmi, you saved my weekend ??

    Thread Starter gbennyb

    (@gbennyb)

    sorry, that was maybe a little to enthusiastic. Now i am getting the post with id 51, but i am JUST getting this post, whatever other catehory i am clicking on, i can just see post 51.

    Any idea what could be wrong still?

    <div id="content">
    
    	<?php
    		$topposts = get_posts('p=51');
    		foreach($topposts as $post) :
    		setup_postdata($post);
    	?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    			<h3 id="post-header"><?php the_title(); ?></h3>
    			<div class="entry">
    				<?php the_content(); ?>
    			</div>
    			<?php if ($post->post_excerpt != '') { ?>
    			<div class="excerpt">
    				<?php echo $post->post_excerpt; ?>
    			</div>
    			<?php } ?>
    		</div>
    	<?php endforeach; ?>
    
    		<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    		    <?php
              // check if the post belongs directly to the current category;
              // continue if not
              $current_categories = get_the_category();
              $found_category = false;
              foreach ($current_categories as $current_category) {
                if ($current_category->term_id == $cat) {
                  $found_category = true;
                  // counting level
                  $level = 0;
                  while ($current_category->parent != 0) {
                    $current_category = get_category($current_category->parent);
                    $level = $level + 1;
                  }
                  break;
                }
              }
              if (!$found_category) {
                continue;
              }
            ?> 
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<h3 id="post-header"><?php the_title(); ?></h3>
    
    				<div class="entry">
    					<?php the_content() ?>
    				</div>
    
    			<?php if ($post->post_excerpt != '') { ?>
    
    				<div class="excerpt">
    
    					<?php echo $post->post_excerpt; ?>
    
    				</div>
    			<?php } ?>
    
    			</div>
    
    	<?php endwhile; else: ?>
    		<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    	<?php endif; ?>
    
    	</div>
    Thread Starter gbennyb

    (@gbennyb)

    what i tried to achieve, is that just on “is_home” the post with the ID=51 appears (and of course when i click on the category name “home”) but for the other categories, their content or if_empty their content of the subcategory (this is another issue) should be shown (what worked before).

    You’ve left out the if (is_home()) conditional.

    Thread Starter gbennyb

    (@gbennyb)

    of course, how stupid from me, sorry.

    Here is – as reference – the complete solution:

    <div id="content">
    
    	<?php if (is_home()) {
    
    		$topposts = get_posts('p=51');
    		foreach($topposts as $post) :
    		setup_postdata($post);
    	?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    			<h3 id="post-header"><?php the_title(); ?></h3>
    			<div class="entry">
    				<?php the_content(); ?>
    			</div>
    			<?php if ($post->post_excerpt != '') { ?>
    			<div class="excerpt">
    				<?php echo $post->post_excerpt; ?>
    			</div>
    			<?php } ?>
    		</div>
    	<?php endforeach; ?>
    
    <?php } ?>
    
    		<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    		    <?php
              // check if the post belongs directly to the current category;
              // continue if not
              $current_categories = get_the_category();
              $found_category = false;
              foreach ($current_categories as $current_category) {
                if ($current_category->term_id == $cat) {
                  $found_category = true;
                  // counting level
                  $level = 0;
                  while ($current_category->parent != 0) {
                    $current_category = get_category($current_category->parent);
                    $level = $level + 1;
                  }
                  break;
                }
              }
              if (!$found_category) {
                continue;
              }
            ?> 
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<h3 id="post-header"><?php the_title(); ?></h3>
    
    				<div class="entry">
    					<?php the_content() ?>
    				</div>
    
    			<?php if ($post->post_excerpt != '') { ?>
    
    				<div class="excerpt">
    
    					<?php echo $post->post_excerpt; ?>
    
    				</div>
    			<?php } ?>
    
    			</div>
    
    	<?php endwhile; else: ?>
    		<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    	<?php endif; ?>
    
    	</div>

    You can replace the p=51 with the post id you’d prefer to get displayed.

    Thank You esmi for your support!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘is_home issue with the_content display’ is closed to new replies.