• Hey,

    I’m trying to echo 2 categories and in max 5 posts. I got this code, but when i load the page, nothing appears. Could someone please go over it and tell me where the error is. I just can’t find it.

    <?php get_header(); ?>
    <?php get_sidebar();?>
    
    <div id="content">
    
    <div class="grid_8">
    <div class="room">
    <h2 class="h2">Aktuelle Diskussionsbeiträge</h2>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query( array( 'category__and' => array( 429, 1335 ), 'showposts=5'.'&paged='.$paged));
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="grid_8">
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Dauerhafter Link auf <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    
    <?php the_excerpt(); ?>
    </div><?php endwhile; ?>
    <?php endif; ?>
    
    <div class="grid_8">
    <?php if (function_exists('postbar')) postbar(); ?>
    </div>
    
    <div class="text grid_8">
    
     <?php wp_reset_query();
    if( have_posts() ) : while( have_posts() ) : the_post();
     the_content();
    endwhile; endif; ?> 
    
    </div>
    </div></div>
    <div class="grid_4 kontakt">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Seitenleiste Medienecho") ) : ?>
    <?php endif; ?>
    </div>
    </div><!-- close #content -->
    <div class="clear"></div>
    
    <?php get_footer(); ?>

    Thank you very much in advance!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You’re using mixed array and string arguments in your query.
    http://codex.wordpress.org/Function_Reference/query_posts#Passing_variables_to_query_posts

    Try it with [untested]:

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'category__and' => array( 429, 1335 ),
      'posts_per_page' => 5,
      'paged' => $paged;
    );

    Thread Starter manuel90

    (@manuel90)

    Doesn’t work

    <?php
    /*
    Template Name: Medienecho
    */
    ?>
    <?php get_header(); ?>
    <?php get_sidebar();?>
    
    <div id="content">
    
    <div class="grid_8">
    <div class="room">
    <h2 class="h2">Aktuelle Diskussionsbeiträge</h2>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'category__and' => array( 429, 1335 ),
      'posts_per_page' => 5,
      'paged' => $paged;
    );
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="grid_8">
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Dauerhafter Link auf <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    
    <?php the_excerpt(); ?>
    </div><?php endwhile; ?>
    <?php endif; ?>
    
    <div class="grid_8">
    <?php if (function_exists('postbar')) postbar(); ?>
    </div>
    
    <div class="text grid_8">
    
     <?php wp_reset_query();
    if( have_posts() ) : while( have_posts() ) : the_post();
     the_content();
    endwhile; endif; ?> 
    
    </div>
    </div></div>
    <div class="grid_4 kontakt">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Seitenleiste Medienecho") ) : ?>
    <?php endif; ?>
    </div>
    </div><!-- close #content -->
    <div class="clear"></div>
    
    <?php get_footer(); ?>

    But thank you for your effort!

    Moderator keesiemeijer

    (@keesiemeijer)

    but when i load the page, nothing appears.

    Do you see a blanco page, or does your layout appear, or do you see only posts from the first or second loop, or none. Do you have posts that have both these categories?

    Thread Starter manuel90

    (@manuel90)

    Yes i got posts of these categories. I see a blank page without layout.

    Moderator keesiemeijer

    (@keesiemeijer)

    There are some errors (one is mine, sorry). Try it with this:

    <?php
    /*
    Template Name: Medienecho
    */
    ?>
    <?php get_header(); ?>
    <?php get_sidebar();?>
    
    <div id="content">
    
    <div class="grid_8">
    <div class="room">
    <h2 class="h2">Aktuelle Diskussionsbeiträge</h2>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
      'category__and' => array( 429, 1335 ),
      'posts_per_page' => 5,
      'paged' => $paged
    );
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="grid_8">
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Dauerhafter Link auf <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    
    <?php the_excerpt(); ?>
    </div><?php endwhile; ?>
    
    <div class="grid_8">
    <?php if (function_exists('postbar')) postbar(); ?>
    </div>
    
    <div class="text grid_8">
    
     <?php wp_reset_query();
    if( have_posts() ) : while( have_posts() ) : the_post();
     the_content();
    endwhile; endif; ?> 
    
    </div>
    </div></div>
    <div class="grid_4 kontakt">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Seitenleiste Medienecho") ) : ?>
    <?php endif; ?>
    </div>
    </div><!-- close #content -->
    <div class="clear"></div>
    
    <?php get_footer(); ?>

    Yes i got posts of these categories.

    Do you have posts that have both these categories, that’s what you’re querying for.

    Thread Starter manuel90

    (@manuel90)

    Oh no, I’d like to display the posts which are listed at least in one of the categories. sorry for this !

    The code works fine for both categories. Thank you so for.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    <?php
    /*
    Template Name: Medienecho
    */
    ?>
    <?php get_header(); ?>
    <?php get_sidebar();?>
    
    <div id="content">
    
    <div class="grid_8">
    <div class="room">
    <h2 class="h2">Aktuelle Diskussionsbeiträge</h2>
    <?php
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $wp_query->query('cat=429,1335&posts_per_page=5&paged='.$paged); ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div class="grid_8">
    <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Dauerhafter Link auf <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    
    <?php the_excerpt(); ?>
    </div>
    <?php endwhile; ?>
    <?php $wp_query = null; $wp_query = $temp; ?>
    <?php wp_reset_postdata(); ?>
    
    <div class="grid_8">
    <?php if (function_exists('postbar')) postbar(); ?>
    </div>
    
    <div class="text grid_8">
    
    <?php wp_reset_query();
    if( have_posts() ) : while( have_posts() ) : the_post();
     the_content();
    endwhile; endif; ?> 
    
    </div>
    </div></div>
    <div class="grid_4 kontakt">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Seitenleiste Medienecho") ) : ?>
    <?php endif; ?>
    </div>
    </div><!-- close #content -->
    <div class="clear"></div>
    
    <?php get_footer(); ?>

    http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Categories and showposts? Can somebody check my code?’ is closed to new replies.