WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Combining two small scripts (7 posts)

  1. lklawless
    Member
    Posted 1 year ago #

    Hi there -

    I have two working scripts that I would like to combine into one.

    This one prevents posts in subcats from displaying on top-level category archive:

    <br />
    <?php<br />
    if ( is_category() ) {<br />
      $cat = get_query_var('cat');<br />
      query_posts(array('category__in' => array($cat)));<br />
    }<br />
    ?><br />

    And this one alphabetizes the posts:

    <br />
    <?php if (is_category())<br />
    { $posts = query_posts($query_string . '&orderby=title&order=asc'); } ?><br />

    I can get either one of these working at a time, but not both together. Is there any way to combine them?

    Thanks!

    [ Moderator note: your other post was deleted as duplicates or topics that point to other topics is not helpful. The forum you were using was fine BTW. ]

  2. arrakis83
    Member
    Posted 1 year ago #

    Hi,
    You can try:

    <?php
    if ( is_category() ) {
      $cat = get_query_var('cat');
      query_posts( array( 'category__in' => array($cat), 'orderby' => 'title', 'order' => 'ASC' ) );
    }
    ?>
  3. lklawless
    Member
    Posted 1 year ago #

    Hi -

    Thank you. That does work for the alphabetizing and hiding, but it does something weird to the pages: the first 10 posts are listed correctly, but then when I click the next button, it lists the same 10 posts on page 2 and all subsequent pages.

  4. arrakis83
    Member
    Posted 1 year ago #

    You need the page argument too:

    if ( is_category() ) {
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $cat = get_query_var('cat');
      query_posts( array(
      	'category__in' => array($cat),
      	'orderby' => 'title',
      	'order' => 'ASC',
      	'posts_per_page' => 10,
      	'paged' => $paged
      ));
    }
  5. lklawless
    Member
    Posted 1 year ago #

    Hmm, no. It's still alphabetized, but it stopped hiding subcats, plus it printed your code at the top of the page.

  6. arrakis83
    Member
    Posted 1 year ago #

    If it printed my code, I think that's because you did a copy/paste and not enclosed the code in <?php ...... ?>

  7. lklawless
    Member
    Posted 1 year ago #

    Oops, you're right.

    That did it - everything is working now exactly like I want it to. Thank you!!

Topic Closed

This topic has been closed to new replies.

About this Topic