WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] 2.5 How to create a category template for a gallery ? (16 posts)

  1. lylo2005
    Member
    Posted 6 years ago #

    Hello,

    after reading the following post http://wordpress.org/development/2008/03/wordpress-25-rc2/ about the new gallery feature, Matt linked an example of this new system on his website: http://ma.tt/category/gallery/
    However, there's no documentation on how to display all the galleries like this (the title of the gallery, a thumbnail of a chosen photo, and a short description).
    How can I be able to reproduce this?
    The tag [gallery] does not have the option to choose a specific photo.

    Thanks in advance.

  2. Austin Matzko
    Member
    Posted 6 years ago #

    I'm not privy to Matt's code, but here's how I would do it.

    First, http://ma.tt/category/gallery/ looks to be a category template. That means that it's probably a file named something like category-14.php, where 14 is the ID of the "gallery" category. Here's the gist of what's in that category template:

    <?php add_filter('gallery_style',create_function('$a','return "";')) ?>
    <?php while (have_posts()) : the_post(); ?>
            <?php $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
            if ( ! is_array($attachments) ) continue;
            $count = count($attachments);
            $first_attachment = array_shift($attachments); ?>
            <h2><a href="<?php the_permalink() ?>"><?php echo single_cat_title(); ?>: <?php the_time('Y-m-d') ?></a></h2>
            <a href="<?php the_permalink() ?>">
                    <?php echo wp_get_attachment_image($first_attachment->ID); ?>
            </a>
            <?php the_excerpt() ?>
            <p>This album contains <strong><?php echo $count ?> items</strong>.</p>
    <?php endwhile; ?>

    As with all templates, this one uses the Loop to print out posts. So there is the standard Loop while loop, within which we get the first attachment info for each post and print the excerpt of the post as the description.

    The 'gallery_style' filter function just keeps the gallery CSS from showing up.

  3. lylo2005
    Member
    Posted 6 years ago #

    coooool, thanks for this nice and clean code! It worked perfectly.

  4. Tommie Hansen
    Member
    Posted 6 years ago #

    The problem with this approach is that it isn't really intuitive and user friendly at all. There would be better to have some sort of plugin to do this automatically and and a tab to the admin interface etc.

    However it do work well if your the only one using the system and know all about it...

    I just don't like the idea to have to write manuals for each function for each WP-site that i won't run myself. :)

  5. Neil Guevara
    Member
    Posted 6 years ago #

    So where exactly would you enter that piece of code?

  6. fixepah
    Member
    Posted 6 years ago #

    I would like to know as well.

  7. Neil Guevara
    Member
    Posted 6 years ago #

    I tried to enter this in my index.php but got nothing but white screen...plus in this code there shows nothing of where to put the id of the category.

  8. Neil Guevara
    Member
    Posted 6 years ago #

    Is there anyone who has a good fix for this and an idea of how to implement it easily?

  9. slothboi
    Member
    Posted 6 years ago #

    The text above needs to be saved as a separate file, with the title of 'category-x.php', where x is the id # of your gallery category.

    This file needs to be saved into your wp-includes folder.

  10. bulletproof
    Member
    Posted 6 years ago #

    I didn't want to start a new post about this but i would like someone to help me with what i am trying to do.

    I am using this code filosofo provided above for my gallery category and it works just fine.

    But i want some help adding it to my index.php as well because with my current code the thumb doesn't work (just shows the gallery shortcode).That is because i am using thumbs and excerpts on my index.php for regular posts.

    So if i understand right i have to add a if function exists code for the gallery php filosofo gave , so when i post something in my gallery it shows normally on fronpage as well.I am not sure how to do it .Can someone help me please with the code.

    So in a few words i need php help to make this:
    If function exists > filosofo's code (gallery) > else my regular index.php

    I hope someone can help me.

    Thank you in advance

  11. bulletproof
    Member
    Posted 6 years ago #

    Sorry to bump this but can someone help me? Still haven't figured it out.

  12. arminbw
    Member
    Posted 6 years ago #

    <?php $attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));

    One question regarding Matt´s code cited by filosofo: What is menu_order supposed to be? It´s always set to zero. Or is there some way to give the images different values by using the WordPress admin backend?

    thnx,
    arminbw

  13. bulletproof
    Member
    Posted 6 years ago #

    arminbw i wish i could help you but i am kinda lost with the gallery code myself.

    If someone has better knowledge please help us by answering those questions. I haven't yet added a gallery to my site because i can't find a solution to my problem.

    Thank you.

  14. Neil Guevara
    Member
    Posted 6 years ago #

    Bulletproof, i cant answer all your questions but i can add the code here i use for my galleries. My blog is all wordpress and if you want your galleries looking something like Matt's then give this code a try. Make sure and change the category ID to fit your blog ;)

  15. skaufmann
    Member
    Posted 5 years ago #

    I've implemented this code and took it another step further. I came up with a easy hack to display the attachments as a separate standalone gallery even when the post contain text. Ma.tt gets away with this by only posting galleries and blogs separately. That wasn't wasn't an option I was willing to use.

    Here is what I did
    http://wordpress.org/support/topic/191230

  16. websta
    Member
    Posted 5 years ago #

    Head's up: Neil, the "give this code a try" link returns an error:

    "Unknown post id, it may have expired or been deleted"

Topic Closed

This topic has been closed to new replies.

About this Topic