在模板page_list中获取图像属性Concrete5

时间:2013-05-09 15:08:14

标签: php concrete5

我的以下代码基于 1.获取当前URL 2.浏览数组并检查url value = to array in array 这样做:

$on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
foreach ($city_array as $sandwich) {
    if (strpos($on_this_link, $sandwich) == true) {
        $sandwich = trim($sandwich, '/');
        $city     = $sandwich;
        if ($city == 'newyork') {
            foreach ($category_array as $double_sandwich) {
                if (strpos($on_this_link, $double_sandwich) == true) {
                    $double_sandwich = trim($double_sandwich, '/');
                    $category_is = $double_sandwich;
                    Loader::model('page_list');
                    $nh = Loader::helper('navigation');                  
                    $pl = new PageList();
                    $pl->filterByAttribute('city', '%' . $city . '%', 'like');
                    $pl->filterByAttribute('category','%'.$category_is.'%','like');                 
                    $pl->sortByDisplayOrder();
                    $pagelist = $pl->get();
                    foreach ($pagelist as $p) {
                    echo '<li> <a href="' . $nh->getLinkToCollection($p) . '">' .htmlspecialchars($p->getCollectionName()) . '</a> </li>';
                         ?>
                 }
           }
     }
}

所以它只显示与URL具有相同属性的页面 每个页面都有我想要显示的图像属性。 如何传递此图像属性?

2 个答案:

答案 0 :(得分:1)

查看页面列表块视图模板中的注释: https://github.com/concrete5/concrete5/blob/master/web/concrete/blocks/page_list/view.php#L33

您可以通过在foreach ($pagelist as $p)循环中添加这样的代码来获取图像属性:

    $img = $p->getAttribute('example_image_attribute_handle');
    if ($img) {
        //you could output the original image at its full size like so:
        echo '<img src="' . $img->getRelativePath() . '" width="' . $img->getAttribute('width') . '" height="' . $img->getAttribute('height') . '" alt="" />';

        //or you could reduce the size of the original and output that like so:
        $thumb = Loader::helper('image')->getThumbnail($img, 200, 100, false); //<--200 is width, 100 is height, and false is for cropping (change to true if you want to crop the image instead of resize proportionally)
        echo '<img src="' . $thumb->src . '" width="' . $thumb->width . '" height="' . $thumb->height . '" alt="" />';
    }

答案 1 :(得分:0)

谢谢,但我已经做到了另一种方式! 我没用过

  

装载机::模型( 'page_list');

相反,我使用了:

$blockType = BlockType::getByHandle('page_list');

硬编码了块!

$th = Loader::helper('text');
$ih = Loader::helper('image');
$page_current = Page::getCurrentPage();
$page_2 = $page_current->getCollectionHandle();
  

$ img = $ page-&gt; getAttribute('product'); $ thumb =   $ ih-&gt; getThumbnail($ img,240,150,false);

在我改变了我的代码之后:

 $on_this_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
    foreach ($city_array as $sandwich) {
        if (strpos($on_this_link, $sandwich) == true) {
            $sandwich = trim($sandwich, '/');
            $city     = $sandwich;
            if ($city == 'newyork') {
            foreach ($category_array as $double_sandwich) {
           if (strpos($on_this_link, $double_sandwich) == true) {
            $double_sandwich = trim($double_sandwich, '/');
            $category_is = $double_sandwich;
            $city_att = $page->getAttribute('city', '%' . $city . '%', 'like');
           $sub_cat_att = $page->getAttribute('category','%'.$category_is.'%','like');  ?>               
           <?php if($city == $city_att && $category_is == $sub_cat_att){  ?><li><img src="<?php  echo $thumb->src ?>" width="<?php  echo $thumb->width ?>" height="<?php  echo $thumb->height ?>" alt="" />
    <h3> <?php  echo $title ?></h3>
    <div class="product_description">
    <?php  echo $description ?>
    </div>
    <a href="<?php  echo $url ?>" target="<?php  echo $target ?>">Read More... </a>
    </li>  <?php } ?> <?php
                    }
                }   
    }

所以它正在发挥作用但仍然响应! Apreciate