• Resolved colophonius

    (@colophonius)


    Hi Folks,

    I hope there is someone out there, who is able to help me…

    Here is what I want:
    I want to display post on my pages. But only post from one category on the page with the same title…

    Through this code, that I found in this forum, I was able to call up posts on the pages.

    <?php query_posts('category_name=straminke-eins'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php the_ID(); ?>
    <?php if ( function_exists('jr_post_image') ) { jr_post_image($id); } ?>
    <?php the_date('F Y'); ?>
    <a href="<?php the_permalink()?>"><?php the_title()?></a>
    <?php the_excerpt(); ?>
    <?php endwhile; endif; ?>

    But in this case it only shows post from category ‘straminke-eins’ on every page…
    What I want is to make ‘category_name= ‘ variable so that every page only shows post from the category with the same name as the page title… Is this possible? Ans is it even understandable what I want?

    Nevertheless here is a link to the site I’m working on…

    Your help would really be appriciated

    Lots of Thanks in advance,
    colo

Viewing 14 replies - 1 through 14 (of 14 total)
  • If this is the second query/loop on the page, you should be using WP_Query – not query_posts.

    Thread Starter colophonius

    (@colophonius)

    Hey esmi,

    Thanks for the quick response.
    Could you post here how it’s done, because I honestly have no clue…
    And would that solve my problem?

    First replace query_posts() with WP_Query() and re-post the new code.

    Thread Starter colophonius

    (@colophonius)

    So that would be the new code

    <?php WP_Query('category_name=straminke-eins'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php the_ID(); ?>
    <?php if ( function_exists('jr_post_image') ) { jr_post_image($id); } ?>
    <?php the_date('F Y'); ?>
    <a href="<?php the_permalink()?>"><?php the_title()?></a>
    <?php the_excerpt(); ?>
    <?php endwhile; endif; ?>

    But it creates an error…

    What error?

    Thread Starter colophonius

    (@colophonius)

    it says that:

    Fatal error: Call to undefined function WP_Query() in /homepages/16/d163559452/htdocs/5435oe/wp-content/themes/kiekoever/page.php on line 26

    Thread Starter colophonius

    (@colophonius)

    Yes, but I don’t really understand it and I would need to engage my self with all this to really dig it. And I didn’t have the time to do so yet.
    So I figured to ask here.

    Try looking at some of the code examples on that page. <?php WP_Query('category_name=straminke-eins'); ?> is wrong.

    Thread Starter colophonius

    (@colophonius)

    ok. I’ll try that…
    Thanks for your help!

    Thread Starter colophonius

    (@colophonius)

    I think i got it working now with this code:

    <?php
    // The Query
    $the_query = new WP_Query( 'category_name=binnen'  );
    
    // The Loop
    while ( $the_query->have_posts() ) {
    	$the_query->the_post();
    	echo '<li>' . get_the_content() . '</li>';
    }
    
    /* Restore original Post Data
     * NB: Because we are using new WP_Query we aren't stomping on the
     * original $wp_query and it does not need to be reset.
    */
    wp_reset_postdata();
    
    ?>
    <?php get_footer(); ?>

    But I still dont know what to fill in after ‘category_name=’ to get it variable and responding to the page title.
    I know I could creat a own templat for each page. But I’d prefer a automated solution…

    Is this secondary Loop inside the main loop or outside?

    Thread Starter colophonius

    (@colophonius)

    I’d say its the secondary loop outside the main loop. But thats just a guess:

    <?php get_header(); ?>
    <link href="style.php" rel="stylesheet" type="text/css" />
    
        <div id="blog" class="shadow">
            <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    
            <div class="post">
    
                <div class="entry">
                <?php the_content(); ?>
                </div>
    
            </div>
    
    <?php endwhile; ?>
    
        <div class="navigation">
            <?php posts_nav_link(); ?>
        </div>
    
    <?php endif; ?>
    </div>
    
    <div id="post">
    
     </div>
    <?php
    // The Query
    $the_query = new WP_Query( 'category_name=binnen'  );
    
    // The Loop
    while ( $the_query->have_posts() ) {
    	$the_query->the_post();
    	echo '<li>' . get_the_content() . '</li>';
    }
    
    /* Restore original Post Data
     * NB: Because we are using new WP_Query we aren't stomping on the
     * original $wp_query and it does not need to be reset.
    */
    wp_reset_postdata();
    
    ?>
    <?php get_footer(); ?>

    Thats the whole page.php

    Thread Starter colophonius

    (@colophonius)

    I did it!
    So here is the code that worked for me:

    <?php
    // The Query
    $the_query = new WP_Query( 'category_name='. get_the_title( $query2->post->ID ) . '&echo=0'  );
    
    // The Loop
    while ( $the_query->have_posts() ) {
    	$the_query->the_post();
    	echo '<li>' . get_the_content() . '</li>';
    }
    
    /* Restore original Post Data
     * NB: Because we are using new WP_Query we aren't stomping on the
     * original $wp_query and it does not need to be reset.
    */
    wp_reset_postdata();
    
    ?>
    <?php get_footer(); ?>

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Showing posts on pages depending on category/page name’ is closed to new replies.