Need Wordpress Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nexcom28
    So Fucking Banned
    • Jan 2005
    • 3716

    #1

    Need Wordpress Help

    I did try searching but can't find the answer.

    I am just wanting to make a small change to one of my category pages however I don't have a category.php file

    Aside from category.php or archive.php where else might I make this change?

    I have

    entry.php
    navigation.php
    functions.php
    postinfo.php
    scripts.php

    And a few more
  • Pink Misfit
    xXxAffiliateReviews.com
    • Jun 2007
    • 1035

    #2
    What are you wanting to change?
    Available for VA assistance, customer service, sales and support, content writer, etc!

    Contact: [email protected]

    Comment

    • nexcom28
      So Fucking Banned
      • Jan 2005
      • 3716

      #3
      I am wanting random posts from one category only

      Looking to put this code in
      Code:
      query_posts(array(
      	'showposts' => 6,
      	'orderby' => 'rand',
      	'category_name' => 'News' //You can insert any category name
      ));
      if (have_posts()) : while (have_posts()) : the_post();
      or something similar.

      Really I just want to randomise the posts from one category. I would use a pplugin but I can't find one, they all seem to be widgets.

      Comment

      • vdbucks
        Monger Cash
        • Jul 2010
        • 2773

        #4
        Originally posted by nexcom28
        I am wanting random posts from one category only

        Looking to put this code in
        Code:
        query_posts(array(
        	'showposts' => 6,
        	'orderby' => 'rand',
        	'category_name' => 'News' //You can insert any category name
        ));
        if (have_posts()) : while (have_posts()) : the_post();
        or something similar.

        Really I just want to randomise the posts from one category. I would use a pplugin but I can't find one, they all seem to be widgets.
        if you have a home.php file, copy it and rename the copy to category.php. If you have no home.php file then copy index.php and rename the copy to category.php.

        You would then need to strip out any existing query args from the new category.php file you created, and replace it with something like this:

        Code:
        query_posts($query_string."&showposts=6&orderby=rand")
        I wouldn't recommend inserting the "'category_name' => 'News'" part because the category.php file is global and will cover any and all categories via "$query_string" in my example.
        Last edited by vdbucks; 06-25-2013, 02:38 AM.

        Comment

        • nexcom28
          So Fucking Banned
          • Jan 2005
          • 3716

          #5
          I found this in entry.php
          Code:
          query_posts($args);
          	}
          ?>
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          Which I presume is the right place, I tried to add
          Code:
          query_posts(array(
                  'showposts' => 6,
          	'orderby' => 'rand',
          	'category_name' => 'news' //You can insert any category name
          ));
          	}
          ?>
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          But it didn't make any differece.

          Comment

          • nexcom28
            So Fucking Banned
            • Jan 2005
            • 3716

            #6
            Thanks for this vd but there isn't a query args in home.php

            Comment

            • vdbucks
              Monger Cash
              • Jul 2010
              • 2773

              #7
              Originally posted by nexcom28
              Thanks for this vd but there isn't a query args in home.php
              Then simply add the following right before the "if (have_posts())" [or "while (have_posts())" if there is no if] line:
              Code:
              query_posts($query_string."&showposts=6&orderby=rand")

              Comment

              • nico-t
                emperor of my world
                • Aug 2004
                • 29903

                #8
                this works for me:

                Code:
                			<?php
                			global $post;
                			$myposts = get_posts('numberposts=6&category=3&orderby=rand');
                			foreach($myposts as $post) :
                			setup_postdata($post); ?>
                
                					<?php
                					global $more;
                					$more = 0;
                					?>
                
                			<div class="postboxmain" id="post-<?php the_ID(); ?>">
                			REST OF YOUR POST TEMPLATE HERE
                			</div>
                
                			<?php endforeach; 
                			wp_reset_query(); 
                			?>

                Comment

                • vdbucks
                  Monger Cash
                  • Jul 2010
                  • 2773

                  #9
                  Originally posted by nico-t
                  this works for me:

                  Code:
                  			<?php
                  			global $post;
                  			$myposts = get_posts('numberposts=6&category=3&orderby=rand');
                  			foreach($myposts as $post) :
                  			setup_postdata($post); ?>
                  
                  					<?php
                  					global $more;
                  					$more = 0;
                  					?>
                  
                  			<div class="postboxmain" id="post-<?php the_ID(); ?>">
                  			REST OF YOUR POST TEMPLATE HERE
                  			</div>
                  
                  			<?php endforeach; 
                  			wp_reset_query(); 
                  			?>
                  Unless you're running a loop inside of a loop, or working directly with a wpdb query then there is absolutely no reason to use a foreach loop...

                  Comment

                  • nico-t
                    emperor of my world
                    • Aug 2004
                    • 29903

                    #10
                    Originally posted by vdbucks
                    Unless you're running a loop inside of a loop, or working directly with a wpdb query then there is absolutely no reason to use a foreach loop...
                    I have no idea about the technical stuff, all i know is it works

                    Comment

                    • vdbucks
                      Monger Cash
                      • Jul 2010
                      • 2773

                      #11
                      Originally posted by nico-t
                      I have no idea about the technical stuff
                      I'm aware of that :P

                      I was just letting you, and the OP know heh

                      Comment

                      • vdbucks
                        Monger Cash
                        • Jul 2010
                        • 2773

                        #12
                        For the OP, here is an example of what one of my category.php files looks like... ignore the html parts of course if you're going to use it:

                        Code:
                        <?php get_header(); ?>
                        
                          <div class="large-9 columns">
                            <header class="site-header" role="banner">
                              <img src="/assets/img/header/<?php echo rand(1,15) ?>.jpg" />
                            </header>
                              <?php
                                if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
                                elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
                                else { $paged = 1; }
                        
                                query_posts($query_string."&showposts=30") 
                              ?>
                                <!-- Main Content -->
                                <h3 class="sectionTitle"><?php wp_title(''); ?></h3>
                                <ul class="small-block-grid-1 large-block-grid-3">
                                  <?php 
                                    if ( have_posts() ) :
                                      while ( have_posts() ) : the_post();
                                        if (is_user_logged_in()) {
                                          get_template_part('templates/members/content', 'tpgrid');
                                        } else {
                                          get_template_part('templates/tour/content', 'tpgrid');
                                        }
                                      endwhile; 
                                    endif; 
                                  ?>
                                </ul>
                                <?php if ( $wp_query->max_num_pages > 1 ) : // if there's more than one page turn on pagination ?>
                                    <?php wp_pagenavi(); ?>
                                <?php endif; ?>
                        
                                <!-- End Main Content -->
                        
                            </div><!-- End large-9 -->
                        <?php if (is_user_logged_in()) { get_sidebar('members'); } else { get_sidebar('tour'); } ?>
                        <?php get_footer(); ?>

                        Comment

                        • nexcom28
                          So Fucking Banned
                          • Jan 2005
                          • 3716

                          #13
                          I hate messing with themes, I tried adding the code you suggested so it looked like

                          Code:
                          <?php query_posts($query_string."&orderby=rand") if (have_posts()) : while (have_posts()) : the_post(); ?>
                          I took out the 6 posts bit.

                          Unfortunately it generated a syntax parse error.

                          Think I will just leave it.

                          Comment

                          • vdbucks
                            Monger Cash
                            • Jul 2010
                            • 2773

                            #14
                            Originally posted by nexcom28
                            I hate messing with themes, I tried adding the code you suggested so it looked like

                            Code:
                            <?php query_posts($query_string."&orderby=rand") if (have_posts()) : while (have_posts()) : the_post(); ?>
                            I took out the 6 posts bit.

                            Unfortunately it generated a syntax parse error.

                            Think I will just leave it.
                            You forgot a ; after the query_posts argument... Use the following:

                            Code:
                            <?php query_posts($query_string."&orderby=rand"); if (have_posts()) : while (have_posts()) : the_post(); ?>

                            Comment

                            • nexcom28
                              So Fucking Banned
                              • Jan 2005
                              • 3716

                              #15
                              Thanks for your help, it's actually working now but it's displaying all posts random. How do I edit that to only display random posts from a specific category?

                              Comment

                              • vdbucks
                                Monger Cash
                                • Jul 2010
                                • 2773

                                #16
                                Originally posted by nexcom28
                                Thanks for your help, it's actually working now but it's displaying all posts random. How do I edit that to only display random posts from a specific category?
                                Could you elaborate on what you want exactly?

                                Do you want only a single category of posts to be shown on the categories page? Doing this would render all other category links and pages inoperable if going this route.

                                Do you want to have a fully functional category page that will work with all links and posts on your site and only want posts in the "News" category to display randomly?

                                Do you want a separate page for "News"?

                                There are various ways your question can be interpreted, and many answers depending on what you want specifically so I need a little more info before I offer a solution.

                                Comment

                                • nexcom28
                                  So Fucking Banned
                                  • Jan 2005
                                  • 3716

                                  #17
                                  I have several categories, one of them being for example 'news'. I am wanting to random the posts within this category only.

                                  Currently all posts in all categories are random.

                                  Thanks

                                  Comment

                                  • vdbucks
                                    Monger Cash
                                    • Jul 2010
                                    • 2773

                                    #18
                                    Originally posted by nexcom28
                                    I have several categories, one of them being for example 'news'. I am wanting to random the posts within this category only.

                                    Currently all posts in all categories are random.

                                    Thanks
                                    I think the best way for that then would be to copy category.php to category-news.php (make sure the "news" portion matches the category slug), or to category-ID.php (where ID = the news category ID). You can find the info you need in wp-admin -> Posts -> Categories.

                                    Then, just remove the "query_posts($query_string."&orderby=rand");" from the base category.php file, and you should be good to go.

                                    And then, for any other categories you may want to display random posts for in the future, simply copy category-news.php (or category-ID.php) to category-slug.php (or category-ID.php, again specifying the numeric ID of said category).

                                    More info here: http://codex.wordpress.org/Category_Templates

                                    Comment

                                    • All1
                                      So Fucking Banned
                                      • May 2013
                                      • 215

                                      #19
                                      Fuck with the usernames.

                                      Comment

                                      Working...