• Resolved deeltje

    (@deeltje)


    The code I’m using right now is displaying the 6 newest posts, but I’d like to have it auto-detect the category the post is in, so in single.php and category.php the code would display the posts in that specific category.

    Can someone help me with this?

    <?php $my_query = new WP_Query('showposts=6');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>
    
    <div id="contentslider_menu"><div class="contentslider_menubalk_streep"><div class="contentslider_menubalk"><ul><li><a href="<?php the_permalink(); ?>" class="toc">
    
    <p class="wit"><?php the_title(); ?></p>
    </a></li></ul></div></div></div>
    <?php endwhile; ?>
Viewing 15 replies - 1 through 15 (of 22 total)
  • I think you are looking for display related posts. If yes, there are some plugins which do that.

    Just search on google for display related posts wordpress plugin

    Thread Starter deeltje

    (@deeltje)

    Isn’t there a way to do this with, excluding category if category = 1 …. or something like that.

    I really don’t like to use to many plugins for these kinda things.

    You’ll need a function or something to automate it, a plugin is just a series of functions, classes, variables etc…

    Find a basic one that covers what you want, and strip the code out if you don’t want to use it as a plugin..

    Agreeing with kapiljain.in and t31os, but for your educational use:

    In a category template this returns the current queried category:

    $cat = get_query_var('cat');

    Returns first category on a post:

    $cats = wp_get_post_categories($post->ID);
    $first_cat = $cats[0];

    Thread Starter deeltje

    (@deeltje)

    @michaelh:

    Thank you for the educational bit, but I have to admit that I know nothing or very little about implementing PHP code.

    <?php $my_query = new WP_Query('showposts=6');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>

    Where in this bit should I try to implement

    $cat = get_query_var('cat');

    ?

    If you are using a category template, WordPress will handle the query for you.

    But just in case you need it:

    $cat = get_query_var('cat');
    $args=array(
       'cat__in' => array($cat),
       'showposts'=>6
       );
    $my_query = new WP_Query($args);

    See template tag, query_posts()

    Thread Starter deeltje

    (@deeltje)

    <?php
    $cat = get_query_var('cat');
    $args=array(
       'cat__in' => array($cat),
       'showposts'=> 6
       );
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>
    <div class="contentslider_menu">
    <div class="contentslider_menubalk_streep">
    <div class="contentslider_menubalk">
    <ul><li>
    <a href="<?php the_permalink(); ?>" class="toc">
    <span class="knop_titel"><?php the_title(); ?></span>
    <span class="knop_datum"><?php the_time('j F Y H:i'); ?></span>
    <span class="knop_category"><?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';} ?></span>
    </a>
    </li></ul>
    </div></div></div>
    <?php endwhile; ?>

    I’m using this now, but it still gives me the 6 newest posts from all categories. I’m using this code in the header.php because I want to minimize my code, I wanted it to work on category.php and single.php

    You’ll want that in a category template.

    Thread Starter deeltje

    (@deeltje)

    @michaelh:

    It doesnt work, it still gets me all news posts of all categories combined, but that kinda makes sense, it shouldn’t matter if its in the header of single or category.php right?

    I don’t want to make a catergory-1.php category-2.php etc. i’m really trying to minimalize the code.

    This is wrong

    $args=array(
       'cat__in' => array($cat),
       'showposts'=> 6
       );

    change to

    $args=array(
       'cat' => $cat,
       'showposts'=> 6
       );

    Of course, I don’t know why you are showing posts for the given category in a category template–seems repetitive.

    Thread Starter deeltje

    (@deeltje)

    @michaelh:

    It did the trick for categories but with single posts it still shows the 6 newest posts of all categories.

    The reason I want this to work, is because i’m using a content-slider in the header. I want it to show the posts in the specific categories and of specific categories. On the homepage it does what it has to do, showing all the newest posts of all categories.

    [link moderated]

    I’m sorry for not being clear enough ….

    For a single post template, this should work:

    if ( is_single() ) (
    $cats = wp_get_post_categories($post->ID);
    $args=array(
       'cat' => $cats[0],
       'showposts'=> 6
       );
    }

    Thread Starter deeltje

    (@deeltje)

    <?php
    if ( is_single() ) (
    $cats = wp_get_post_categories($post->ID);
    $args=array(
       'cat' => $cats[0],
       'showposts'=> 6
       );
    }
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>

    I’ve got that right now, but this doesn’t work.
    I don’t understand the } closing bracket… isn’t this suppose to be a ) ?

    I’ve also tried to close the ) and replace the last } with a ), but that doesn’t work either.

    <?php
    if ( is_single(); ); (
    $cats = wp_get_post_categories($post->ID);
    $args=array(
       'cat' => $cats[0],
       'showposts'=> 6
       );
    );
    $my_query = new WP_Query($args);
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    ?>

    P.S.: Would you be so kind to remove the URL I mentioned some posts above this one?

    Sorry, this should be:

    if ( is_single() ) {

    Thread Starter deeltje

    (@deeltje)

    @michealh:

    Perfect, but now it only detects on single.php, on the index.php and category.php it doesn’t detect any posts anymore ??

    Maybe an -else- somewhere?

    (Thanks very much for removing the url btw!)

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Auto-detect Category’ is closed to new replies.