1

Good Day. I am using a Gallery shortcode in my wordpress site to display images. Problem is that I wasnt the gallery to show only images from a certain category, for example category id 35. How do I specify it in the shortcode?

Shortcode:

 [custom_gallery style="1" source="**cat=%cat_id%**" link="image" description="0" size="200x200" limit="10"]

Now I have tried the following, but does not work -

cat=%35%
cat=%cat_id=35%

Any suggestions? Thank you

SHORTCODE code:

/**
 * Gallery posts shortcode
 */

function gallery_posts_func($atts, $content = null) {
    extract(shortcode_atts(array(
        "limit" => '5',
        "cat" => '',
        "thumb_width" => '',
        "thumb_height" => '',
    ), $atts));
    global $wp_query,$paged,$post;
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();

    $query .= '&posts_per_page='.$limit;
    $query .= '&post_type=gallery';
    $query .= '&taxonomy=gallery_cat';
    $query .= '&gallery_cat='.$cat;

    $wp_query->query($query);
    ob_start();
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <div class="gallery-holder fourthcol shortcode">
<?php

                $gogo_gallery_video_url = get_post_meta($post->ID, 'gogo_gallery_video_url', true);
                $gogo_gallery_links_to = get_post_meta($post->ID, 'gogo_gallery_links_to', true);
                $gogo_gallery_title_links_to = get_post_meta($post->ID, 'gogo_gallery_title_links_to', true);
                $gogo_gallery_custom_link = get_post_meta($post->ID, 'gogo_gallery_custom_link', true);

                if ($gogo_gallery_title && $gogo_gallery_video_url && $gogo_gallery_title_links_to=="gallery_title_links_image") {
                echo '<h5>';    
                echo '<a href="'.$gogo_gallery_video_url.'" rel="prettyPhoto[mixed]">';
                echo ''.get_the_title().'';
                echo '</a>';
                echo '</h5>';

                } elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_image") {
                echo '<h5>';    
                echo '<a href="'.$thumbnail[0].'" rel="prettyPhoto[mixed]">';
                echo ''.get_the_title().'';
                echo '</a>';
                echo '</h5>';

                } elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_content") {
                echo '<h5>';    
                echo '<a href="'.get_permalink().'">';
                echo ''.get_the_title().'';
                echo '</a>';
                echo '</h5>';

                } elseif ($gogo_gallery_title && $gogo_gallery_title_links_to=="gallery_title_links_link") {
                echo '<h5>';    
                echo '<a href="'.$gogo_gallery_custom_link.'">';
                echo ''.get_the_title().'';
                echo '</a>';
                echo '</h5>';

                } elseif ($gogo_gallery_title) {
                echo '<h5>';    
                echo ''.get_the_title().'';
                echo '</h5>';

                } else {
                echo '';
                }
        ?>  
<div class="gallery-box">
    <div class="gallery-image prettygallery">
        <?php if (has_post_thumbnail()) { ?>
            <?php
                $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');

                if ($gogo_gallery_video_url && $gogo_gallery_links_to=="gallery_links_image") { 
                echo '<a href="'.$gogo_gallery_video_url.'" rel="prettyPhoto[mixed]">';
                echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&amp;w='.$thumb_width.'&amp;h='.$thumb_height.'&amp;zc=1&amp;q=100&amp;s=1" alt="'.get_the_title().'" />';
                echo '</a>';

                } elseif ($gogo_gallery_links_to=="gallery_links_image") {
                echo '<a href="'.$thumbnail[0].'" rel="prettyPhoto[mixed]">';
                echo '<img   src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&amp;w='.$thumb_width.'&amp;h='.$thumb_height.'&amp;zc=1&amp;q=100&amp;s=1" alt="'.get_the_title().'" />';
                echo '</a>';

                } elseif ($gogo_gallery_links_to=="gallery_links_content") {
                echo '<a href="'.get_permalink().'">';
                echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&amp;w='.$thumb_width.'&amp;h='.$thumb_height.'&amp;zc=1&amp;q=100&amp;s=1" alt="'.get_the_title().'" />';
                echo '</a>';

                } elseif ($gogo_gallery_links_to=="gallery_links_link") {
                echo '<a href="'.$gogo_gallery_custom_link.'">';
                echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&amp;w='.$thumb_width.'&amp;h='.$thumb_height.'&amp;zc=1&amp;q=100&amp;s=1" alt="'.get_the_title().'" />';
                echo '</a>';

                } else {
                echo '<img src="'.get_template_directory_uri().'/timthumb.php?src='.$thumbnail[0].'&amp;w='.$thumb_width.'&amp;h='.$thumb_height.'&amp;zc=1&amp;q=100&amp;s=1" alt="'.get_the_title().'" />';
                }
            ?>
        <?php } ?>
    </div>
<?php if ($gogo_gallery_short_desc) { ?><em><?php echo $gogo_gallery_short_desc; ?></em><?php } ?>
</div>
</div>
    <?php endwhile; ?>


    <?php $wp_query = null; $wp_query = $temp;
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_shortcode("gallery_posts", "gallery_posts_func");
3
  • The default gallery shortcode for wordpress is [gallery]. So I suppose your shortcode [custom_gallery] is just what it sounds like: a custom shortcode. Without knowing from which plugin this comes from (maybe even a custom coding in functions.php?) it's hard to help.
    – s1lv3r
    Commented Feb 23, 2013 at 13:58
  • Can you post the Code for your shortcode? It is impossible to tell just from the shortcode.
    – fischi
    Commented Feb 23, 2013 at 13:58
  • ok will post it. Thanks people, appreciate your time and help Commented Feb 23, 2013 at 14:01

2 Answers 2

1

This may do..

[custom_gallery style="1" cat="35" link="image" description="0" size="200x200" limit="10"]
2
  • Hi Rajeev - Thank you for your answer, but I tried this, and does not work - I get this message:Custom gallery: "no attached images, or only one attached image…" - and cat 35 does have images Commented Feb 23, 2013 at 16:28
  • I made a mistake. I referenced the wrong cat. It works. Thank you for the help Commented Feb 23, 2013 at 17:37
1

This is not a complete answer. As I wrote, I kept noticing more and more issues or potential issues. Rather than waste the effort, here is what I have and I hope it helps.

First, you are registering your shortcode as gallery_posts but are using custom_gallery to try to implement it.

You are passing your category like this: source="**cat=%cat_id%**" but you aren't doing any further processing so this line: $query .= '&gallery_cat='.$cat; never actually gets set, and if it did it would end up looking like this: $query .= '&gallery_cat=**cat=%cat_id%**, which would be wrong. The attribute name/ID is source but you don't use that. You use cat instead.

extract(shortcode_atts(array(
    "limit" => '5',
    "cat" => '',
    "thumb_width" => '',
    "thumb_height" => '',
), $atts));

I would suggest something like this for your shortcode:

[custom_gallery style="1" cat="35" ...

(No idea why you have all those % in your code, not to mention the *. What are those supposed to be doing?) Now this line-- $query .= '&gallery_cat='.$cat;-- will come out like $query .= '&gallery_cat=35, but if you check the Codex, that (deprecated) {tax}= syntax expects a slug, not an ID. You need to rewrite your query like this:

$args = array(
  'posts_per_page' => $limit,
  'post_type' => 'gallery',
  'tax_query' => array(
    array(
      'taxonomy' => 'gallery_cat',
      'field' => 'id',
      'terms' => $cat
    )
  )
);
$wp_query = new WP_Query($args);

That should clear up some problems at least. I should note that 'gallery' is not a post a type that I am aware of. I assume this is something you created? If you are trying to use the WordPress galleries, that is not going to work. Take a look at how the WordPress gallery shortcode pulls data: http://core.trac.wordpress.org/browser/trunk/wp-includes/media.php#L671 It isn't a simple process.

2
  • Thank you for your answer. Appreciate. using cat="35" worked just fine Commented Feb 23, 2013 at 17:38
  • If that worked then the code you posted isn't accurate. For one, the name of the shortcode is different, as mentioned. I tried to run you code as is and the shortcode isn't processed at all. The string is spit back out just as it went in. That is why I noticed that issue. Secondly, even if it works you are using deprecated code, and it has been deprecated for awhile. It will eventually stop working.
    – s_ha_dum
    Commented Feb 23, 2013 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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