【问题标题】:Wordpress Gallery shortcode: Display one category ID onlyWordpress Gallery 简码:仅显示一个类别 ID
【发布时间】:2013-02-23 13:46:53
【问题描述】:

美好的一天。我在我的 wordpress 网站中使用图库短代码来显示图像。 问题是我不是画廊只显示某个类别的图像,例如类别 id 35。如何在短代码中指定它?

简码:

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

现在我尝试了以下方法,但不起作用 -

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

简码代码(因为它是自定义简码):

/**
 * 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");

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    您正在寻找的可能是:

    cat="35"
    

    【讨论】:

    • 感谢您的回答--但不起作用。它显示:自定义画廊:没有附加图像,或者只有一个附加图像......尽管我在该类别 ID 下有图像......我不能使用类别名称或 slug(即使该选项没有出现在短代码中
    • 查看我刚才添加到帖子中的简码。也许你可以在那里找出一些东西。我会看看我可以在那里改变什么
    • 谢谢伙计。它确实有效,我引用了错误的猫。感谢您的帮助
    猜你喜欢
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多