Forums

[resolved] PHP code in template tag (7 posts)

  1. daand
    Member
    Posted 2 years ago #

    <?php
     $postslist = get_posts('numberposts=10&orderby=title');
     foreach ($postslist as $post) :
        setup_postdata($post);
     ?>

    I need the php code beneath"

    <?php
    $category = get_the_category();
    echo $category[0]->cat_ID;
    ?>

    To be inserted in the code above.

    So i can display: get_posts('numberposts=10&category=.......&orderby=title');
    and replace ..... with the cateogry ID number.

    Does anyone understand this?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    How about:

    <?php
    $category = get_the_category();
    $cat_id = $category[0]->cat_ID;
    $postslist = get_posts('numberposts=10&orderby=title&cat=' . $cat_id);
    foreach ($postslist as $post) :
    setup_postdata($post);
    ?>
  3. MichaelH
    Volunteer
    Posted 2 years ago #

    Do you mean

    get_posts('numberposts=10&category=' . $category[0]->cat_ID . '&orderby=title');
  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Don't you have to use cat for category ids? Or have I missed something?

  5. MichaelH
    Volunteer
    Posted 2 years ago #

    Righto!

    get_posts('numberposts=10&cat=' . $category[0]->cat_ID . '&orderby=title');
  6. daand
    Member
    Posted 2 years ago #

    Esmis solution worked!!
    Thanks very much!!

  7. daand
    Member
    Posted 2 years ago #

    What if i want to exclude the current post in the get posts code?

    $postslist = get_posts('numberposts=1000&order=ASC&exclude=....&orderby=date&cat=' . $cat_id);

    In which case ... should be the current post ID.

    The current post ID template tag is; the_ID() I thought.

    I would very much appericiate it when someone could explain how it works, because i dont want to bother you every time..

    Thanks in advance!
    Daan

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.