17

我正在尝试使用 WordPress 中的颜色框创建概览功能。

让我稍微解释一下。在 WordPress 中,页面有通过以下代码查询的帖子:

$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); 
get_template_part( 'content', get_post_format() ); 
endforeach;

所以这将抓取 WordPress 数据库中的所有帖子。现在每个帖子都是一个产品,所以我想知道是否有一种方法可以向其中添加一些代码,以便为每个帖子设置一个值,一旦有人点击帖子图片,它就会发送该帖子的标题,所以它可以获取该特定产品的概述模板(我将制作的东西)。

更新:

这是单击任何图像后打开的 jQuery:

<link media="screen" rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/js/colorbox.css" />
<script src="<?php echo get_template_directory_uri(); ?>/js/jquery.colorbox-min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(function()
    {
        $('.item-post a').colorbox({opacity:0.3, href:"../overviewa512454dzdtfa"});
    });
</script>

我希望将与图像关联的帖子的标题发送到在颜色框中打开的文件。

4

2 回答 2

3

I would put a rel="<?= $post['title'] ?>" in your links, so that each link has the post title. (Sorry if $post['title']" isn't the right attribute for the WP Post, but you'll find that). Then, in your javascript, pass the title in the URL, like:

<script type="text/javascript">
    $(function()
    {
        $('.item-post a').bind('click',function() {
            event.preventDefault();
            var product_title = $(this).attr('rel');
            colorbox({opacity:0.3, href:"../overviewa512454dzdtfa?title=" + product_title});
        });
    });
</script>

You should either URL encode the title you're passing inside the rel=" tag, or do it with javascript when you pass it to the colorbox.

Then, the overview page, you can access the title with $_REQUEST['title'].

于 2012-12-24T21:49:27.930 回答
1

content-page.php、content-aside.php 等是 WP 主题中用于在循环中输出产品的文件,因为您的代码中有这一行:

get_template_part( 'content', get_post_format() ); 

所以基本上,您需要做的就是打开这些文件,并在输出每个帖子的内容的部分中,使用您的帖子的 ID 将帖子图像包围起来(<a href="#" id="post-<?php the_ID(); ?>">...

然后,您将能够使用 jQuery 查询中的 ID 轻松地“定位”适当的概述......

于 2012-12-19T08:57:28.553 回答