Forum

Thread tagged as: Question, Problem, Blog

How do I tell the blog function to get the latest post?

How do I tell the blog function to get the latest post? I'm hoping for something like this:

<?php 

?s="latestpost";
perch_blog_post(perch_get('s')); 

?>

I should preface by saying I don't really know php or the terminology.

I know I could use perch_blog_custom(); but the real reason I want to do this, is so the search results will replace the blog post when the search button is pressed. In other words, there is a search form on the same page as the blog. When someone searches, the blogpost disappears and the search results show up in its place. This only seems to work when the blog post is originally loaded with perch_blog_post(perch_get('s')); The rub is that no blog post comes up with this custom function unless the url contains ?s="blog-title-here". Hope that makes sense. Thanks in advance!

Daniel Lynch

Daniel Lynch 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Like this:

<?php perch_blog_recent_posts(1); ?>

Thanks Drew. That function will give me a list with a link to the most recent post. I'm looking for a way to pass the most recent post to this function when the page loads:

perch_blog_post(perch_get('s'));

This is what I'm using now:

 <?php  


            perch_blog_custom(array(
                'count' => 1,
                'template' => 'post_askdarbyfox.html',
                'sort' => 'postDateTime',
                'sort-order' => 'DESC',



                    ));

        ?>

The problem is that when the search feature is used, it just adds the search results above the blog post instead of replacing it. This works on the regular post pages, but not this one. Here is the url:

https://beta234.darbyfox.com/blog/askdarbyfox-blog.php

Many Thanks!!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok. What do you have the in page? You are in control of what is displayed.

<?php include 'header-blog.php'; ?>

           <div class="grid clr-1">

            <section class="col-3-12 blog-sidebar">

                    <button class="question-button"><i class="fa fa-paper-plane clr-2"></i> &nbsp;I HAVE A QUESTION</button>
                    <hr>

                    <div id="question-form-wrapper">

                        <div id="question-form">

                        <div id="close-x" class="clr-4"><i class="fa fa-arrow-up clr-4"> Close</i> </div>

                        <img src="img/asklogo_ro.svg" width="75px">

                        <p class="clr-4">
                        Please ask anything related to child and adolescent issues. If your question is chosen, you'll find your question and my reply featured on this website. I look forward to hearing from you! <span class="clr-5">All questions are anonymous. Your question may be edited for style and grammar.</span>

                        </p>

                        <form>
                            <input type="text" maxlength="100" placeholder="Main Question: Max 100 Characters"></input> 
                            <textarea type="text" maxlength="500" placeholder="Explanation (optional): Max 500 Characters"></textarea> 
                        </form>

                        <button>
                            <i class="fa fa-paper-plane clr-2"></i> Send
                        </button>

                        </div>

                    </div>

                      <?php perch_search_form(); ?>
                    <hr>
                    <?php perch_blog_recent_posts(10); ?>
                    <br>
                    <?php perch_blog_tags(); ?>
            </section> 

            <div id="search-results" style="width:75%;float:left;" >
                <?php 
                    $query = perch_get('q');  // 'q' query string argument e.g. search.php?q=apples
                    perch_content_search($query);
                ?>
            </div>

            <div id="tag-archive-custom" style="width:75%;float:left;">

            <?php           
                // defaults for all modes
                $posts_per_page = 0;
                $template       = 'post_in_list.html';
                $sort_order     = 'DESC';
                $sort_by        = 'postDateTime';

                // Have we displayed any posts yet?
                $posts_displayed = false;

                /* 
                    perch_get() is used to get options from the URL.

                    e.g. for the URL 
                        /blog/archive.php?cat=news

                    perch_get('cat') would return 'news' because cat=news.


                    The code below looks for different options in the URL, and then displays different types of listings based on it.
                */



                /* --------------------------- POSTS BY TAG --------------------------- */
                if (perch_get('tag')) {
                    echo '<h1>Archive of: '.perch_blog_tag(perch_get('tag'), true).'</h1>';

                    perch_blog_custom(array(
                            'tag'        => perch_get('tag'),
                            'template'   => $template,
                            '10'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            ));

                    $posts_displayed = true;
                }



                /* --------------------------- DEFAULT: ALL POSTS --------------------------- */

                if ($posts_displayed == false) {

                    // No other options have been used; no posts have been displayed yet.
                    // So display all posts.

                    echo '';

                    perch_blog_custom(array(
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            ));

                }

            ?>
        </div>

            <div style="width:75%;float:left;" >


             <?php  


                    perch_blog_custom(array(
                    'count' => 1,
                    'template' => 'post_askdarbyfox.html',
                    'sort' => 'postDateTime',
                    'sort-order' => 'DESC',



            ));

            ?>



            </div>


            </div>


<?php include 'footer-blog.php'; ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so if you don't want the post to display when there's a search, just test for the search parameter.

if (!perch_get('q')) {
    ... no search query, so show the blog stuff ...
}

Thank you sir. Works perfectly!