Forum

Thread tagged as: Problem, Configuration

Post list on post.html not working

I want to show related posts on post.html page, filtered by current category, but for some reason is not showing anything...

Post.html:

<?php  
                        perch_blog_custom(array(
                                        'count'      => 10000,
                                        'template'   => 'proyectos.html',
                                        'sort'       => 'postDateTime',
                                        'sort-order' => 'DESC',
                                        'category' => perch_get('cat'),
                                        // I also tried this-------> 'category' => 'proyectos/'.perch_get('cat'),
                                    ));
                        ?>

template

<li>
    <a href='/proyecto.php?s=<perch:blog id="postSlug" />'>
        <figure>

            <img src='<perch:blog type="image" id="image_portada" label="Imagen portada" width="400" height="400" crop="true" suppress="false" />' alt='<perch:blog id="postTitle" type="text" label="Título (es)" divider-before="Título" required="true" size="xl autowidth" title="true" />' />

            <figcaption>
                <perch:if id="lang" value="es"><perch:blog id="postTitle" type="text" label="Título (es)" required="true" size="xl autowidth" title="true" /></perch:if>
                <perch:if id="lang" value="en"><perch:blog id="postTitle-en" type="text" label="Título (en)" required="true" size="xl autowidth" /></perch:if>
                <perch:if id="lang" value="fr"><perch:blog id="postTitle-fr" type="text" label="Título (fr)" required="true" size="xl autowidth" /></perch:if>
            </figcaption>
        </figure>
    </a>
</li>

diagnostic report:

Perch: 3.0.4, PHP: 5.4.4, MySQL: 5.5.25, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (3.0.4), assets (3.0.4), categories (3.0.4), perch_blog (5.5.1)
App runtimes: <?php $apps_list = [ perch_blog, ];
PERCH_LOGINPATH: /perch
PERCH_PATH: /Applications/MAMP/htdocs/optaarquitectos.com/perch
PERCH_CORE: /Applications/MAMP/htdocs/optaarquitectos.com/perch/core
PERCH_RESFILEPATH: /Applications/MAMP/htdocs/optaarquitectos.com/perch/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 64M, Total max file upload: 32M
F1: 0c66c2e1f82f9e0b7617b2cb8270f2c7
Resource folder writeable: Yes
HTTP_HOST: opta2:8888
DOCUMENT_ROOT: /Applications/MAMP/htdocs/optaarquitectos.com
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
Raul Serrano

Raul Serrano 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't use PHP functions in an HTML template. If you need the result output within the template, you'll need to get the result out on the page and then pass it in.

Excuse me Drew, i was meaning post.php

I don't get the problem here. I am just making a simple category filtering on post page... hmmm...

Simon Clay

Simon Clay 127 points

Hi Raul, is there a 'cat' value to get in the querystring at this point? There would need to be for that to work.

Simon Clay

Simon Clay 127 points

You'll need to use perch_blog_post_categories to get the category(s) from the current post.

This helpful article from Clive Walker will help you:

https://www.cvwdesign.com/blog/how-to-show-related-posts-with-perch-blog

Hi Simon, I am using this code

if (isset($_GET['cat']) && $_GET['cat']!='') {
      $cat = $_GET['cat'];
      $_SESSION['cat'] = $cat;
    }elseif (isset($_SESSION['cat'])){
      $cat = $_SESSION['cat'];
    }else{
      // default cat
      $cat = 'transporte';
    }
    PerchSystem::set_var('cat', $cat);

I tried but It didn't work as I adapted:

<?php  

                        //Get the categories for the current post 
                        $categories = perch_blog_post_categories(perch_get('s'), array(
                         'skip-template'=>true, ));

                        //The post may be in more than one category, so we need to loop through these and get the category slugs
                        if (count($categories)) {
                         $cat_slugs = array();
                         foreach($categories as $cat) {
                         $cat_slugs[] = $cat['catSlug'];
                         }

                        //Display two related posts, ordered by date, excluding the current post
                        perch_blog_custom(array(

                         'category' => $cat_slugs,
                         'sort' => 'postDateTime',
                         'sort-order'=> 'DESC',
                         'count'=> 10000,
                         'template'=> 'blog/proyectos.html',
                         ));
                         }

                        ?>
Simon Clay

Simon Clay 127 points

Going back to your original code, does this work?

<?php  
perch_blog_custom(array( 
'count' => 10000, 
'template' => 'proyectos.html', 
'sort' => 'postDateTime', 
'sort-order' => 'DESC', 
'category' => $cat,
)); 
?>

No, it doesn't...

Simon Clay

Simon Clay 127 points

what does

<?php echo($cat); ?>

output to the page?

It output catslug

Simon Clay

Simon Clay 127 points

Ok, so that's obviously not a category you're after.

Can you show your whole page code now, as I am unsure of what's in there at this point.

Sorry for delay Simon, I've being out for two weeks. Here is the full code

<?php

    include('perch/runtime.php');

    session_start();
    if (isset($_GET['lang']) && $_GET['lang']!='') {
      $lang = $_GET['lang'];
      $_SESSION['lang'] = $lang;
    }elseif (isset($_SESSION['lang'])){
      $lang = $_SESSION['lang'];
    }else{
      // default language
      $lang = 'es';
    }

    if (isset($_GET['cat']) && $_GET['cat']!='') {
      $cat = $_GET['cat'];
      $_SESSION['cat'] = $cat;
    }elseif (isset($_SESSION['cat'])){
      $cat = $_SESSION['cat'];
    }else{
      // default cat
      $cat = 'transporte';
    }

    PerchSystem::set_var('lang', $lang); // para blog multilang 
    PerchSystem::set_var('cat', $cat);
?>


<!DOCTYPE html>
<?php include('include/ascii.php'); ?>
<?php include('include/default-vars.php'); // Añadir variables que cambien a continuación (y $current) 

$current = "proyecto";

?>
<html lang="<?php echo $lang; ?>">

<head>
    <?php

            if (perch_get('s')) {  $titlePost = perch_blog_custom(array( 'template' => 'slug2.html', 'filter' => 'postSlug', 'match' => 'eq', 'value' => perch_get('s')), true) . "&bull; OPTA Arquitectos"; }

            $title = $titlePost;
            $ogtitle = $titlePost;

            include('include/head.php');

        ?>
</head>

<body id="proyecto">
    <div class="page-wrap">
        <div class="container">
            <header>
                <?php include('include/header.php'); ?>
                <?php include('include/nav-proyectos.php'); ?>
            </header>
            <article>
                <?php perch_blog_post(perch_get('s')); ?>
                <section class="otros">
                    <h3>Otros proyectos de </h3>
                    <ul>
                        <?php  

                            $current_post = perch_blog_custom(array(
                            'filter' => 'postSlug',
                            'match' => 'eq',
                            'value' => perch_get('s'),
                            'skip-template' => true,
                            ));
                            $cats = perch_blog_post_categories(perch_get('s'), array('skip-template'=>true));
                            $cats = array_map(function($item){return $item['catSlug'];}, $cats);


                            perch_blog_custom(array(

                            'template' => 'proyectos.html',
                            'sort' => 'postDateTime',
                            'sort-order' => 'DESC',
                            'count' => '200000',
                            'category' => $cat,
                            ));


                        ?>

                    </ul>
                </section>
            </article>
        </div>
        <!-- /.container -->
    </div>
    <!-- /.page-wrap -->
    <?php include('include/footer.php'); ?>
    <?php include('include/cargas.php'); ?>
</body>

</html>