0

OK. I am going to try to rewrite this.

I am trying to use the Gridly theme as a mobile only theme for my site.

The problem is the theme pulls all post categories into the main feed when it is formatted for mobile devices.

I only want readers to see posts in two categories on the main page, with the option to select other categories from the menu up top.

There is no front page php file.

The theme only includes the following:

404 Template (404.php)
Comments (comments.php)
Footer (footer.php)
Theme Functions (functions.php)
Header (header.php)
Main Index Template (index.php)
admin-menu-mark.php
admin-menu.php
Page Template (page.php)
Single Post (single.php)

Styles

Stylesheet (style.css)

So I modified the index.php file to limit the post categories pulled into the homepage by writing query_posts( 'cat=79,120' ) above the start of the loop ( above: <?php if (have_posts()) : ?> ).

Doing so limits the main page to those two categories, but also does the same when I click on other categories in the menu. Since it is outside the loop it seems to be overwriting the rest of the rules (?).

Here is the code as is: (This is taken from the index file)

<?php get_header(); ?>

<?php if (have_posts()) : ?>
<div id="post-area">
<?php while (have_posts()) : the_post(); ?> 

         <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php if ( has_post_thumbnail() ) { ?>
         <div class="gridly-image"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'summary-image' );  ?></a></div>
          <div class="gridly-category"><p><?php the_category(', ') ?></p></div>

          <?php } ?>
                <div class="gridly-copy"><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p class="gridly-date"><?php the_time(get_option('date_format')); ?>  </p>

        <?php the_excerpt(); ?> 

               <p class="gridly-link"><a href="<?php the_permalink() ?>"></a></p>
         </div>
       </div>


<?php endwhile; ?>
</div>
<?php else : ?>
<?php endif; ?>

<?php next_posts_link('<p class="view-older">View Older Entries</p>') ?>


<?php get_footer(); ?>

Does anyone have any idea how I could only include two categories in the homepage feed without overwriting the rules for the rest of the site?

  • This really isn't enough info. What file is the above content taken from? You know about WordPress' template concept, right? What template files are there in this Gridly theme? And how/where did you use query_posts? – tfrommen Mar 28 '13 at 10:02
  • The template files are 404 Template (404.php), Comments (comments.php), Footer (footer.php), Theme Functions (functions.php), Header (header.php), Main Index Template (index.php), Page Template (page.php) Single Post (single.php)admin-menu-mark.php, admin-menu.php. That code is from index.php I placed query_posts( 'cat=79,120' ) (and the rest of it) above <?php if (have_posts()) : ?> It worked fine in limiting the feed to those two categories, but did the same thing on every other page. – Jonathan Mar 28 '13 at 10:15
  • If you put the mentioned line in the index.php file only, and yet you thereby changed the content of pages then there's something off with your theme. Or what did you mean by did the same thing on every other page? – tfrommen Mar 28 '13 at 10:24
  • yes, sorry. it did the same thing on every other page. meaning when I tried to view a page containing a specific category, the line overruled the rest of the loop. – Jonathan Mar 28 '13 at 10:30
  • Okay, then please tell me once again, what it is that you're trying to achieve. I'd suggest you reword the question itself. What I understood so far is that you tried to build a separate theme for mobile devices, and in that theme you want to exclude certain categories. Then, you only want that for the standard post archive. If so, you just need another template file, the front-page.php. But once again, please reword the question, and then I might provide you with an actual answer. – tfrommen Mar 28 '13 at 10:37
0

Okay, assuming you only want to restrict the categories for the front page, but still list all categories everywhere else, just add the according template file to your theme:

front-page.php

<?php get_header(); ?>

<?php $my_query = new WP_Query('cat=79,120'); ?>
<?php if ($my_query->have_posts()) : ?>
<div id="post-area">
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>   
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php if ( has_post_thumbnail() ) { ?>
                <div class="gridly-image"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'summary-image' );  ?></a></div>
                <div class="gridly-category"><p><?php the_category(', ') ?></p></div>
            <?php } ?>
            <div class="gridly-copy"><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p class="gridly-date"><?php the_time(get_option('date_format')); ?>  </p>
                <?php the_excerpt(); ?> 
                <p class="gridly-link"><a href="<?php the_permalink() ?>"></a></p>
            </div>
        </div>
    <?php endwhile; ?>
</div>
<?php next_posts_link('<p class="view-older">View Older Entries</p>'); ?>

<?php get_footer(); ?>

Now you may completely change whatever you like for the front page, without affecting other archives.

However, if really all you need to change, is listing either just the two or all categories, you could also stick with your index.php, and check the HTTP request and act upon that:

if ('/' === $_SERVER['REQUEST_URI']) {
    $my_query = new WP_Query('cat=79,120');
    if ($my_query->have_posts()) :
        echo '<div id="post-area">';
    while ($my_query->have_posts()) : $my_query->the_post();
} else {
    if (have_posts()) :
        echo '<div id="post-area">';
            while (have_posts()) : the_post();
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.