ThemeShaper Forums » Thematic

[closed]

thematic_category_loop to pull specific category on a page template

(15 posts)
  • Started 8 years ago by ORyan
  • Latest reply from karenshayk
  • This topic is resolved
  1. ORyan
    Member

    I am trying to set up a couple of page templates to basically behave like a category page except only pull one specified category.

    for instance i have a bunch of posts, some are in a blog category some are in a portfolio category. I would like to set up a page for each that shows only the selected category.

    I have pulled a copy of the thematic_category_loop() into my functions file with the intent of creating a new function that can accept a parameter for the category id.

    This way i can simply include the new function in my template and pass it a value for the id:
    new_category_loop(3);

    But i'm still new at the whole child theme idea and can't quite figure it out. Is there an easier way to do this? Or am i missing something.

    Posted 8 years ago #
  2. elderberry
    Member

    whoops, duplicate

    Posted 8 years ago #
  3. elderberry
    Member

    I would love to see code for this also. I am not good with php, so what I ended up doing was creating a separate template file for each category, which is time consuming and means I will have to change it for the client any time they add a new category. I also have not found thematic_categoryloop anywhere - or I might be able to modify it in functions.php...

    Posted 8 years ago #
  4. Similar to this thread?
    http://forums.themeshaper.com/topic/displaying-posts-from-categories-on-pages-using-a-second-loop

    There is also a great plugin by Dennis Hoppe called Page-Post-Associator which does the job.

    Cheers!
    Chris

    Posted 8 years ago #
  5. well there is a BLOG page template. you can use it and simply modify the query at the top so that it only picks up a particular category.

    Posted 8 years ago #
  6. ORyan
    Member

    @krowchuk, yea i read through that thread and those linked there but no luck. The theme i am using (AutoFocus+) has a nice way of displaying it's archive and category lists, so i figured i could reuse that code. so i made my copies and figured out a way to specify a category but it outputs totally different, I am guessing it's because of the way thematic uses is_single and such.

    @helga, yea I am aware of the blog template but i'm not certain on how to modify it to only retrieve one category. i have tried modifying it to this with no avail... it just pulls in all posts.

    $wp_query = new WP_Query();
    $wp_query->query( array( 'posts_per_page' => get_option( 'posts_per_page' ),'category' => '1', 'paged' => $paged ) );
    $more = 0;

    I know thematic + child themes is supposed to be easier and give more flexibility but, I'm just not feeling it yet. The output code is so bloated and editing anything beyond CSS is such a hunt and pain. Not to mention the majority of Thematic sites I have checked out are extremely slow. I realize there is a learning curve, and i don't mind that but, so far it just seems easier to build what i need with a regular theme. It's faster and more efficient on me, my server, and the end user's browser.

    Not that I'm giving up, just not sure I like it or find the value yet.

    Posted 8 years ago #
  7. there IS a learning curve, for sure. but once you understand hooks and filters it is very powerful, b/c it is actually the same procedure for changing almost everything. making non-CSS edits is a matter of either placing new code in the position you want or finding the code that is outputting what you want to change... and the "guts" of thematic is actually all strikingly well-organized in the library/extensions folder. looking for code that relates to widgets, it is in widget-extensions. looking for loop code, in content-extensions and so on.

    and the problem you are having w/ query vars isn't a thematic issue at all.

    http://codex.wordpress.org/Function_Reference/query_posts

    if you check the codex you will see that 'category' is not a query parameter, so of course it won't work. try 'cat' => 4 or 'category_name' => 'bacon'

    as for slowness i would like to see Thematic combine some of its scripts and stylesheets and minify them. but slowness is also a function of whether or not the site is using caching and the biggest factor dependings on dedicated versus shared hosting. i'd guess if you are using a free framework you are much more likely to be on a shared server.

    Posted 8 years ago #
  8. ORyan
    Member

    Hmm i had tried 'cat' => 3 just before changing it to category but it wasn't working either... I shall check it again.

    hmm... still no dice with:

    $wp_query->query( array( 'posts_per_page' => get_option( 'posts_per_page' ),'cat' => 1, 'paged' => $paged ) );

    And that is in a copy of the template-page-blog.php from the thematic folder nothing else changed except the above code and the template name.

    I have written a custom function that basically doesn't use thematic as much and i can get it to work just fine. Which is frustrating. I would like to figure out how to make this work leveraging thematic.

    this is what i have maybe it may help shed some light on what i'm doing and if there is an easier way:

    function portBuild($prefix, $CatID, $Nposts, $offset){
    global $post;
    $args = array( 'numberposts' => $Nposts, 'offset'=> $offset, 'cat' => $CatID );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post);
    	// create posts list
    ?>
    	<div class="<?php echo $prefix ?>-entry p1" id="post-<?php the_ID() ?>">
            <h2 class="<?php echo $prefix ?>-entry-title" style=""><a>" title="<?php echo wp_specialchars(get_the_title(), 1) ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    		<div class="<?php echo $prefix ?>-entry-meta" style="">
    			<span class="<?php echo $prefix ?>-entry-title"><a>" title="<?php echo wp_specialchars(get_the_title(), 1) ?>" rel="bookmark"><?php the_title(); ?></a></span>
    		</div>
            <?php if ( has_post_thumbnail() ) { ?>
    					<a>" rel="bookmark" class="<?php echo $prefix ?>-image-container"><?php the_post_thumbnail('aflarge'); ?></a>
    			<?php } else { ?>
    					<a>" rel="bookmark" class="<?php echo $prefix ?>-image-container"><?php the_post_image('aflarge'); ?></a>
    		<?php } ?>
    		<div class="<?php echo $prefix ?>-entry-content" style="opacity: 0; margin-left: 0px;">
    			<?php thematic_content() ?>
    		</div><!-- .entry-content -->
    		<div class="<?php echo $prefix ?>-entry-utility">
    			<?php edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ?>
        	</div><!-- .entry-utility -->
    
        </div>
    	<?php endforeach;
    }// end portBuild()

    and here is how i implement it in the template file:

    portBuild('work', 3, 10, 0);

    specifying the prefix allows me to be more specific and lean with the way i write the CSS

    Posted 8 years ago #
  9. do you have the correct category ID #? b/c i just checked this locally and it works as expected. i copied the template into my child theme and edited

    $wp_query = new WP_Query();
    $wp_query->query( array( 'posts_per_page' => get_option( 'posts_per_page' ), 'paged' => $paged, 'category_name' => 'artists' ) );
    $more = 0;

    and it pulls up all the posts in my 'artists' category

    when you say change the name.. do you mean the file name or the bit in comments at the top of the code? i didn't change either- this way it just flat out overrides the existing template. but if you wanted to preserve the existing template you'd rename the file AND rename the name in the comments, otherwise WP will get confused by things having the same name.

    Posted 8 years ago #
  10. ORyan
    Member

    Yea I was trying to pull my 'blog' category which has the ID of 1.

    I changed the name in the comments just so i could make sure wordpress was actually using my file and not the default. But i just tried changing both the template name and the file name and same results.

    I also tried using the category_name instead of the ID like you suggested above and it's still doing the same thing, pulling in all categories. something else must be conflicting... No plug-ins installed though.

    here is the URL:
    http://oryanstudios.com/blog

    as it stands only one post is listed under 'blog' the rest should be listed as 'work'

    Posted 8 years ago #
  11. ORyan
    Member

    For the time being i will end up using my custom function for sake of time and sanity.
    Thanks for the help Helga!

    Posted 8 years ago #
  12. try copying this:

    http://pastebin.com/3PXAdQbK

    and saving it as template-page-category.php (or anything really, just to be different)

    then activate this template. i'm stumped b/c what i gave you works for me.

    Posted 8 years ago #
  13. ORyan, sorry I'm not familiar with autofocus. Is it a thematic child theme? I assume it is. Have you tried creating a new child theme and just adding your code to isolate whether it is a thematic or autofocus issue?

    I have a second loop on most of my static pages drawing posts from various categories, and on one page I have three extra custom loops working as I expected. I, however, use the new query method because it migrated with me from my old non thematic theme.

    `
    <?php
    $news1 = new WP_Query('category_name=News&posts_per_page=1');
    while ($news1->have_posts()) : $news1->the_post(); $permalink = get_permalink($news1->ID);
    ?>
    <h3>"><?php the_title(); ?></h3>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    `

    Posted 8 years ago #
  14. @helgatheviking, I've attempted to integrate the solution you outlined above at http://ascmps.com on the home page. I've activated a new page template, which I created using your code on pastebin. The category I want to display on the home page is "news". Can you take a look and tell me what I'm doing wrong?

    Posted 8 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.