skip to Main Content

I need help to solve something that i cannot do 🙁

I am creating a simples website with HTML5/CSS/PHP (static pages, without DB/Mysql), and it is almost complete. I just need to shows 3 posts as suggested articles at the end of page (footer).

Here is the Post page where the users will see all posts. This is the articles.php
(https://phpout.com/wp-content/uploads/2023/12/gkkek.png)

After clicking, user will be redirected to page read-article.php in order to read the post. Here is the read-article.php page
(https://phpout.com/wp-content/uploads/2023/12/b7R32.png)

The articles.php page is already done! I just want to load its content inside read-article.php page, but SHOWING ONLY 3 posts.

This is an example about post’s code of articles.php:

<section class="pg-articles">
    <ul>
        <li class="title-1-of-this-article">
            <div class="head-articles">
                <img src="<?= root_url() ?>/media/img/slider/slide1.jpg">
                <h2><a href="article/title-1-of-this-article">Title 1 of this post</a></h2>
            </div>
        </li>
       <li class="title-2-of-this-article">
            <div class="head-articles">
                <img src="<?= root_url() ?>/media/img/slider/slide2.jpg">
                <h2><a href="article/title-2-of-this-article">Title 2 of this post</a></h2>
            </div>
        </li>
<li class="title-3-of-this-article">
            <div class="head-articles">
                <img src="<?= root_url() ?>/media/img/slider/slide3.jpg">
                <h2><a href="article/title-3-of-this-article">Title 3 of this post</a></h2>
            </div>
        </li>
<li class="title-4-of-this-article">
            <div class="head-articles">
                <img src="<?= root_url() ?>/media/img/slider/slide4.jpg">
                <h2><a href="article/title-4-of-this-article">Title 4 of this post</a></h2>
            </div>
        </li>
    </ul>
</section>

So on…

I was planning on loading this piece of code by using

** include "articles.php"** at footer inside read-article.php. BUT I DONT WANT TO LOAD all the posts, JUST 3 POSTS. I.E:

      <li class="title-2-of-this-article">
            <div class="head-articles">
                <img src="<?= root_url() ?>/media/img/slider/slide2.jpg">
                <h2><a href="article/title-2-of-this-article">Title 2 of this post</a></h2>
            </div>
        </li>
<li class="title-3-of-this-article">
            <div class="head-articles">
                <img src="<?= root_url() ?>/media/img/slider/slide3.jpg">
                <h2><a href="article/title-3-of-this-article">Title 3 of this post</a></h2>
            </div>
        </li>
<li class="title-4-of-this-article">
            <div class="head-articles">
                <img src="<?= root_url() ?>/media/img/slider/slide4.jpg">
                <h2><a href="article/title-4-of-this-article">Title 4 of this post</a></h2>
            </div>
        </li>

By knowing these are static pages, i would write manually, but it is anoying to do that every time i publish a post. So, in this way, i WANT only to insert new post in articles.php, and through PHP, automatically getting the 3 latets posts.

Can anyone help me? 🙂

2

Answers


  1. Chosen as BEST ANSWER

    No DB, because that was difficult to find a solution. I needed to hide the same article (active url) on the related boxes. I found myself a workaround:

    1- I created a page to get the same title of article in order that domain.com/article/THE-NAME-OF-PAGE is: the-name-of-page.php which is loaded with:

    <?php
    function URL($key = null)
    {
        $arr = explode("/", trim($_GET['url'] ?? 'index', "/"));
        if (!is_numeric($key))
            return $arr;
    
        return $arr[$key] ?? '';
    }
    
    $file = URL(0) . '.php';
    
    if (file_exists($file)) {
        require $file;
    } else {
        $baseurl = rtrim(root_url(), "/");
        header("Location:$baseurl/pagina-nao-encontrada");
    }
    ?>
    

    At the footer of the article.php i wrote:

      <li class="THE-NAME-OF-PAGE">
                    <div class="head-articles">
                        <img src="<?= root_url() ?>/media/img/slider/slide1.jpg" alt="Núcleo de Psicologia do Ano"
                            title="Núcleo de Psicologia do Ano">
                        <div class="head-padding">
                            <h2 title="Núcleo de Psicologia do Ano"><a
                                    href="artigo/os-problemas-na-vida-sua-funcao-e-como-solucionar">os-problemas-na-vida-sua-funcao-e-como-solucionar</a>
                            </h2>
                        </div>
                    </div>
                </li>
    

    And i used:

    <?php if (isset($page)) {
        ?>
        <style>
            .headline-pg-article ul [class*="<?= $page ?>"] {
                display: none;
            }
        </style>
    <?php } ?>
    

    It worked awhile. But i have to put the latest 4 articles {li} manually. The script above will hide the 3 active article and will show only 3. So that everytime i wrote new article, a just copy & paste the newest article block to this page.


  2. not database?
    where do you get that post from?

    If is no db
    Wouldn’t it be a good idea to just handle it with css?

    .pg-articles ul li:nth-last-child(-n+3){ display: none;}
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search