GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Need Wordpress Help (https://gfy.com/showthread.php?t=1113490)

nexcom28 06-25-2013 02:43 AM

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 06-25-2013 03:08 AM

What are you wanting to change?

nexcom28 06-25-2013 03:17 AM

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.

vdbucks 06-25-2013 03:34 AM

Quote:

Originally Posted by nexcom28 (Post 19685482)
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.

nexcom28 06-25-2013 03:42 AM

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.

nexcom28 06-25-2013 03:45 AM

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

vdbucks 06-25-2013 03:55 AM

Quote:

Originally Posted by nexcom28 (Post 19685501)
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")

nico-t 06-25-2013 04:01 AM

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


vdbucks 06-25-2013 04:05 AM

Quote:

Originally Posted by nico-t (Post 19685510)
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...

nico-t 06-25-2013 04:07 AM

Quote:

Originally Posted by vdbucks (Post 19685511)
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 :winkwink:

vdbucks 06-25-2013 04:13 AM

Quote:

Originally Posted by nico-t (Post 19685512)
I have no idea about the technical stuff

I'm aware of that :P

I was just letting you, and the OP know heh

vdbucks 06-25-2013 04:18 AM

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


nexcom28 06-25-2013 04:26 AM

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.

vdbucks 06-25-2013 04:32 AM

Quote:

Originally Posted by nexcom28 (Post 19685528)
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(); ?>

nexcom28 06-25-2013 04:40 AM

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?

vdbucks 06-25-2013 04:52 AM

Quote:

Originally Posted by nexcom28 (Post 19685539)
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.

nexcom28 06-25-2013 05:09 AM

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

vdbucks 06-25-2013 05:16 AM

Quote:

Originally Posted by nexcom28 (Post 19685555)
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

All1 06-25-2013 08:35 AM

Fuck with the usernames.


All times are GMT -7. The time now is 02:19 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123