祝你好运。我在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].\'&w=\'.$thumb_width.\'&h=\'.$thumb_height.\'&zc=1&q=100&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].\'&w=\'.$thumb_width.\'&h=\'.$thumb_height.\'&zc=1&q=100&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].\'&w=\'.$thumb_width.\'&h=\'.$thumb_height.\'&zc=1&q=100&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].\'&w=\'.$thumb_width.\'&h=\'.$thumb_height.\'&zc=1&q=100&s=1" alt="\'.get_the_title().\'" />\';
echo \'</a>\';
} else {
echo \'<img src="\'.get_template_directory_uri().\'/timthumb.php?src=\'.$thumbnail[0].\'&w=\'.$thumb_width.\'&h=\'.$thumb_height.\'&zc=1&q=100&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");
SO网友:s_ha_dum
这不是一个完整的答案。在我写作的过程中,我不断注意到越来越多的问题或潜在的问题。与其浪费精力,不如把我所拥有的东西放在这里,我希望它能有所帮助。
首先,您将您的短代码注册为gallery_posts
但是正在使用custom_gallery
尝试实施它。
您通过的类别如下:source="**cat=%cat_id%**"
但您没有进行任何进一步的处理,因此此行:$query .= \'&gallery_cat=\'.$cat;
从未真正设置过,如果设置了,最终会变成这样:$query .= \'&gallery_cat=**cat=%cat_id%**
, 这是错误的。属性名称/ID为source
但你不用那个。您使用cat
相反
extract(shortcode_atts(array(
"limit" => \'5\',
"cat" => \'\',
"thumb_width" => \'\',
"thumb_height" => \'\',
), $atts));
我建议您使用以下短代码:
[custom_gallery style="1" cat="35" ...
(不知道你为什么有这些
%
在您的代码中,更不用说
*
. 那些人应该在做什么?)现在这一行--
$query .= \'&gallery_cat=\'.$cat;
-- 结果会是
$query .= \'&gallery_cat=35
, 但是
if you check the Codex, (已弃用)
{tax}=
语法需要一个slug,而不是ID。您需要像这样重写查询:
$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);
这至少可以解决一些问题。我应该注意到,“画廊”并不是我所知道的那种类型的帖子。我想这是你创造的?如果你想使用WordPress图库,那是行不通的。看看WordPress gallery快捷码是如何提取数据的:
http://core.trac.wordpress.org/browser/trunk/wp-includes/media.php#L671 这不是一个简单的过程。