【问题标题】:"Read More" button stopped working in PHP, MySQL CMS“阅读更多”按钮在 PHP、MySQL CMS 中停止工作
【发布时间】:2018-04-28 06:25:34
【问题描述】:

我想在用户访问我的博客时显示少量文本,这就是为什么我制作了一个“阅读更多”按钮来通过切断文本的长度来显示少量文本,并且该按钮直到今天都可以正常工作我通过 TinyMCE 编辑器添加了一个新帖子,我看到“阅读更多”按钮停止工作。在我禁用该功能时检查错误,然后“阅读更多”按钮激活,并且发布的链接工作正常,但我无法切断长度,请帮我解决这个问题。

我使用 TinyMCE 作为编辑器来添加新帖子。 这是 index.php 文件。

<?php include("includes/db.php"); ?>
<?php include("includes/header.php"); ?>

    <!-- Navigation -->

    <?php include("includes/navigation.php"); ?>

    <!-- Page Content -->
    <div class="container">

        <div class="row">

            <!-- Blog Entries Column -->
            <div class="col-md-8">

                <?php 

                    $per_page = 3;

                    if(isset($_GET['page'])) {

                        $page = $_GET['page'];
                    }
                    else {

                        $page = "";
                    }

                    if($page =="" || $page == 1) {

                        $page_1 = 0;
                    }
                    else {

                        $page_1 = ($page * $per_page) - $per_page;
                    }

                    $post_query_count = "SELECT * FROM posts";
                    $find_count = mysqli_query($connection, $post_query_count);
                    $count = mysqli_num_rows($find_count);

                    $count = ceil($count / $per_page);

                    $query = "SELECT * FROM posts LIMIT $page_1, $per_page";
                    $select_all_posts_query = mysqli_query($connection, $query);

                    while($row = mysqli_fetch_assoc($select_all_posts_query)) {

                        $post_id = $row['post_id'];
                        $post_title = $row['post_title'];
                        $post_user = $row['post_user'];
                        $post_date = $row['post_date'];
                        $post_image = $row['post_image'];
                        $post_content = $row['post_content'];
                        $post_status = $row['post_status'];

                        if($post_status == 'published') {

                 ?>   

                <!-- First Blog Post -->
                <h2>
                    <a href="post.php?p_id=<?php echo $post_id; ?>"><?php echo $post_title; ?></a>
                </h2>
                <p class="lead">
                    by <a href="index.php"><?php echo $post_user; ?></a>
                </p>
                <p><span class="glyphicon glyphicon-time"></span> <?php echo $post_date; ?></p>
                <hr>
                <a href="post.php?p_id=<?php echo $post_id; ?>">
                <img class="img-responsive" src="images/<?php echo $post_image; ?>" alt="">
                </a>
                <hr>
                <?php echo string_length_cutoff($post_content, 320, '........'); ?>
                <a class="btn btn-primary" href="post.php?p_id=<?php echo $post_id; ?>">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>

                <hr>

                <?php  

                    } }

                 ?>


            </div>

            <!-- Blog Sidebar Widgets Column -->

            <?php include("includes/sidebar.php"); ?>

        </div>
        <!-- /.row -->

        <hr>

        <ul class="pager">

            <?php 

                for ($i = 1; $i <= $count; $i++) { 

                    if($i == $page) {

                        echo "<li><a class='active_link' href='index.php?page={$i}'>{$i}</a></li>";
                    }
                    else {
                        echo "<li><a href='index.php?page={$i}'>{$i}</a></li>";                        
                    }
                }
             ?>
        </ul>

 <?php include("includes/footer.php"); ?>

下面这个函数存放在functions.php文件中

function string_length_cutoff($post_content, $limit, $subtext = '...') {
        global $connection;
        $query = "SELECT * FROM posts";
        $select_all_posts_query = mysqli_query($connection, $query);
        while($row = mysqli_fetch_assoc($select_all_posts_query)) {
            $post_content = $row['post_content'];
        }
    return (strlen($post_content) > $limit) ? substr($post_content, 0, ($limit-strlen('subtext'))).$subtext : $post_content;
    }

实际问题的一些截图

Read More Button not working

Inspecting read more

read more inspection

【问题讨论】:

    标签: php mysql content-management-system


    【解决方案1】:

    我认为不需要数据库查询。删除它。

    将您的功能更改为:

    function string_length_cutoff($post_content, $limit, $subtext = '...') {
        $post_content = strip_tags($post_content, '<img>');
        return ((strlen($post_content) > $limit) ? substr($post_content, 0, $limit).$subtext : $post_content);
    }
    

    【讨论】:

    • 更新功能。
    • 我已经更新了您提到的功能,但问题仍然存在部分!在一些帖子中,阅读更多按钮处于非活动状态!
    • 必须是你帖子的内容。
    • 我正在从一些随机博客复制内容,这些博客有自己的内容格式,然后我通过 TinyMCE 编辑器发布这些内容。复制粘贴这些内容,更改“阅读更多”按钮的格式。
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多