WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Getting rid of page title when looping posts (13 posts)

  1. olofiano
    Member
    Posted 2 years ago #

    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

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    Why not just omit the page title output within your Loop?

  3. olofiano
    Member
    Posted 2 years ago #

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

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  5. olofiano
    Member
    Posted 2 years ago #

    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?

  6. olofiano
    Member
    Posted 2 years ago #

    [ 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!

  7. esmi
    Forum Moderator
    Posted 2 years ago #

    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.

  8. olofiano
    Member
    Posted 2 years ago #

    Thank you, I'll look in to that.

  9. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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 -->
  10. olofiano
    Member
    Posted 2 years ago #

    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?

  11. olofiano
    Member
    Posted 2 years ago #

    Edit: used wp_reset_postdata(); without any improvements :)

  12. olofiano
    Member
    Posted 2 years ago #

    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.

  13. olofiano
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic