• Resolved olofiano

    (@olofiano)


    Hi,
    I’m pretty new to theme developing and trying to get my head around how The Loop works. I’ve read tons of posts and the full Codex tutourial but i can’t seem to find an answer to my problem. Maybe it’s because I’m trying to approach the problem from the wrong angle?

    So here is what im trying to do:
    The setup for the page I’m building is that all links in the navigation are WordPress pages, and some of the content in those pages that require alot of updates are made out of posts. With this setup I’ll be able to have some semi-static content and some that are more dynamic. (Is this the “right” way to do it?)

    Anyway, this is where the problem starts. One or more pages will consist of posts only, from different categorys (this way I can easier tell which post to go where on the page, again is there a better way?). When I’m trying to loop out say 3 different posts from different categorys it works fine, but for some reason the page title loops out aswell, 1 for every post. This is why i havn’t found the solotion in multiple loops tutourials, since i have no problems with repeating posts, but with the pagetitle.

    In this moment my code is a mess but I’ll be happy to provide it if necessary, but mosty I just wan’t to understand why this is happening and if I should change the approach of how to build this kind of homepage.

    Best regards,
    Olof

Viewing 12 replies - 1 through 12 (of 12 total)
  • Why not just omit the page title output within your Loop?

    Thread Starter olofiano

    (@olofiano)

    Mainly because I don’t like to make exceptions for bad things that are happening, but trying to prevent them from happening instead.

    Sorry – I don’t follow you… A standard query returns all of the post details – including it’s title. It’s up to you whether that title is displayed or not. There is no “bad thing happening” here.

    Thread Starter olofiano

    (@olofiano)

    Ok, maybe I havn’t been clear enough. I want to show everything from the posts I’m looping out, but for some reason it also loops out the page title. I want to get rid of page title, page content, and so on, and only show the posts in that page. πŸ™‚ follow?

    Thread Starter olofiano

    (@olofiano)

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    This is kind of pseudo-code for what im trying to achieve.

    <?php $do_not_duplicate = array(); ?>
    
    <?php if(have_posts()) : while (have_posts()) : the_post(); ?>
    	<!-- the page info that i don't want to show -->
    	<?php $do_not_duplicate[]= $post->ID; ?>
    	<?php endwhile; ?>
    
    	<!-- show post in cat 13 -->
    	<?php query_post('cat=13&showposts=1'); ?>
    	<?php while(have_posts()) : the_post();
    	if(!in_array($post->ID,$do_not_duplicate)) {
    	the_title();
    	the_content();
    	$do_not_duplicate[]= $post->ID; ?>
    	<?php }
    	endwhile; ?>
    
    	<!-- show post in cat 14 -->
        <?php query_post('cat=14&showposts=1'); ?>
    	<?php while(have_posts()) : the_post();
    	if(!in_array($post->ID,$do_not_duplicate)) {
    	the_title();
    	the_content();
    	$do_not_duplicate[]= $post->ID;?>
    	<?php }
    	endwhile; ?>
    
        <!-- show post in cat 15 -->
        <?php query_post('cat=15&showposts=1'); ?>
    	<?php while(have_posts()) : the_post();
    	if(!in_array($post->ID,$do_not_duplicate)) {
    	the_title();
    	the_content();
    	$do_not_duplicate[]= $post->ID;?>
    	<?php }
    	endwhile; ?>

    Right now this displays absolutely nothing. But maybe through this you have a better understanding on what I’m trying to do!

    For starters, I’d suggest you use either get_posts or WP_Query. query_posts is only meant to be used to modifying the main query – not for creating secondary queries.

    You might also want to review multiple Loops.

    Thread Starter olofiano

    (@olofiano)

    Thank you, I’ll look in to that.

    omitt this whole section:

    <?php if(have_posts()) : while (have_posts()) : the_post(); ?>
    	<!-- the page info that i don't want to show -->
    	<?php $do_not_duplicate[]= $post->ID; ?>
    	<?php endwhile; ?>

    because the first output is a page, it will never occur as duplicate in your posts.

    or, if you don’t want any output of that page data, omitt at least this section:

    <!-- the page info that i don't want to show -->
    Thread Starter olofiano

    (@olofiano)

    Thanks for the responses. After the suggestions and a bit of reading the code now looks like this:

    <?php if(have_posts()); ?>
    <?php $do_not_duplicate = array(); ?>
    	<!-- show post in cat 13 -->
    	<?php $myquery = new WP_Query();
    	$myquery->query('cat=13&showposts=1'); ?>
    
    	<?php while($myquery->have_posts()) : $myquery->the_post();
    	if(!in_array($post->ID,$do_not_duplicate)) {
    	the_title();
    	the_content();
    	$do_not_duplicate[]= $post->ID; ?>
    	<?php }
    	endwhile; ?>
    
    	<!-- show post in cat 14 -->
    
    	<?php $myquery1 = new WP_Query();
    	$myquery1->query('cat=14&showposts=1'); ?>		    
    
    	<?php while($myquery1->have_posts()) : $myquery1->the_post();
    	if(!in_array($post->ID,$do_not_duplicate)) {
    	the_title();
    	the_content();
    	$do_not_duplicate[]= $post->ID;?>
    	<?php }
    	endwhile; ?>
    
        <!-- show post in cat 15 -->
        <?php $myquery2 = new WP_Query();
    	    $myquery2->query('cat=15&showposts=1'); ?>
    
    	<?php while($myquery2->have_posts()) : $myquery2->the_post();
    	if(!in_array($post->ID,$do_not_duplicate)) {
    	the_title();
    	the_content();
    	$do_not_duplicate[]= $post->ID;?>
    	<?php }
    	endwhile; endif; ?>

    The page doesn’t load though so theres something wrong to it. Any ideas?

    Thread Starter olofiano

    (@olofiano)

    Edit: used wp_reset_postdata(); without any improvements πŸ™‚

    Thread Starter olofiano

    (@olofiano)

    Just figured out that the argument cat=13&showposts=1 doesnt work as intended. Tried to break it down into smaller pieces i noticed that when i change it to 2 posts it still only loops out one (the most recent) post, and the default page title.

    Thread Starter olofiano

    (@olofiano)

    Found the solution at http://digwp.com/2011/05/loops/ now by using the get_posts() template tag like this:

    <?php global $post;
    $args = array('numberposts'=>1, 'category' => 13, 'order' => 'ASC');
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post); 
    
    //all code for this post goes here
    
    endforeach;
    ?>

    and then make new static loops for each post I wanted.

    Thanks for help

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Getting rid of page title when looping posts’ is closed to new replies.