WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Auto-detect Category (23 posts)

  1. deeltje
    Member
    Posted 5 years ago #

    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; ?>
  2. kapiljain.in
    Member
    Posted 5 years ago #

    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

  3. deeltje
    Member
    Posted 5 years ago #

    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.

  4. Mark / t31os
    Moderator
    Posted 5 years ago #

    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..

  5. MichaelH
    Member
    Posted 5 years ago #

    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];
  6. deeltje
    Member
    Posted 5 years ago #

    @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');

    ?

  7. MichaelH
    Member
    Posted 5 years ago #

    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()

  8. deeltje
    Member
    Posted 5 years ago #

    <?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

  9. MichaelH
    Member
    Posted 5 years ago #

    You'll want that in a category template.

  10. deeltje
    Member
    Posted 5 years ago #

    @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.

  11. MichaelH
    Member
    Posted 5 years ago #

    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.

  12. deeltje
    Member
    Posted 5 years ago #

    @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 ....

  13. MichaelH
    Member
    Posted 5 years ago #

    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
       );
    }
  14. deeltje
    Member
    Posted 5 years ago #

    <?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?

  15. MichaelH
    Member
    Posted 5 years ago #

    Sorry, this should be:

    if ( is_single() ) {

  16. deeltje
    Member
    Posted 5 years ago #

    @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!)

  17. MichaelH
    Member
    Posted 5 years ago #

    come to think of it, aren't you putting that code in single.php?

    Related:
    Template Hierarchy

  18. deeltje
    Member
    Posted 5 years ago #

    No I'm putting the code in header.php, which worked so far, the only thing that didn't work was the category detection on single.php.

    The reason I started this post was because I wanted to use minimalized code, sure I could easely put the code in index.php/category.php and single.php instead, but if it's possible not to, then that would be just a whole bunch greater ;)

    Or isn't this possible because single is in a different hierachy then category.php? And is that the reason why index.php would just take all the new posts also?

  19. deeltje
    Member
    Posted 5 years ago #

    And again, here I go :)

    How do I display posts per specific user ?

    something with the_author ?

    (The user is a subscriber)

  20. MichaelH
    Member
    Posted 5 years ago #

    To present links to an author's posts:
    1. Use the template tag, wp_list_authors(), in your sidebar or a Widget.
    2. or, use template tag, the_author_posts_link(), in your loop.

    To customize and author template read Author Templates

  21. deeltje
    Member
    Posted 5 years ago #

    Yeah I know links to authors_posts, but I want to show the posts of specific authors.

  22. MichaelH
    Member
    Posted 5 years ago #

    Use the template tag, query_posts(), and the author= argument.

  23. deeltje
    Member
    Posted 5 years ago #

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

    I'm beginning to understand this ;)

    @MichaelH,

    Thanks a LOT for all your help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.