• На моем сайте у меня большие статьи и чтобы лучше пользователю было ориентироваться в статье я поставил плагин который показывает содержание в статье. плагин автоматически выводит содержание статьи после загаловка.
    Как мне сделать чтобы он выводил содержание после 1 абзаца? Может что то в плагине поменять ?
    Вот код самого плагина:

    <?php
    /*
    Plugin Name: Marafon.Contents
    Version: 0.1
    */
    
    define('MK_CONTENTS_DEEPEST_H', 3);
    define('MK_CONTENTS_AT_LEAST', 2);
    
    function mk_contents_get_last_ind($id) {
        $pos = strrpos($id, '-');
        if ($pos === false) {
            return false;
        }
        return intval(substr($id, $pos + 1));
    }
    
    function mk_contents_truncate_last_ind($id) {
        $pos = strrpos($id, '-');
        if ($pos === false) {
            return false;
        }
        return substr($id, 0, $pos);
    }
    
    function mk_contents_increment_last_ind($id) {
        $t = mk_contents_get_last_ind($id);
        if (!$t) {
            return false;
        }
        $id = mk_contents_truncate_last_ind($id);
        $id .= '-' . ($t + 1);
        return $id;
    }
    
    function mk_contents ( $content = '' ) {
        if ( is_single() ) {
            if (preg_match_all('|<h([0-9]+)([^>]*)>(.*?)</h|ims', $content, $m)) {
    
                $n = sizeof($m[0]);
    
                // Checks for the needed amount of headers.
                $cnt = 0;
                for ($i = 0; $i < $n; $i++) {
                    $m[1][$i] = intval($m[1][$i]);
                    if ($m[1][$i] <= MK_CONTENTS_DEEPEST_H) {
                        $cnt++;
                    }
                }
                if ($cnt < MK_CONTENTS_AT_LEAST) {
                    return $content;
                }
    
                $initial_content = $content; // Saves the initial content of the post. It is returned whenever some error occurs.
                $contents = ''; // For li-elements of the final contents.
    
                //print_r($m);
    
                $prev_h = 1;
                $id = 'part';
                for ($i = 0; $i < $n; $i++) {
                    $cur_h = $m[1][$i];
                    if ($cur_h > MK_CONTENTS_DEEPEST_H) {
                        continue;
                    }
    
                    // Defines the id (part-...).
                    if ($cur_h > $prev_h) { // deeper level
                        $id .= '-1';
                    } elseif ($cur_h == $prev_h) { // the same level
                        $id = mk_contents_increment_last_ind($id);
                    } elseif ($cur_h < $prev_h) { // higher level
                        for ($t = 0; $t < $prev_h - $cur_h; $t++) {
                            $id = mk_contents_truncate_last_ind($id);
                        }
                        $id = mk_contents_increment_last_ind($id);
                    }
                    if (!$id) { // some error
                        return $initial_content;
                    }
    
                    // Adds the link to the contents.
                    $contents .= '<li class="cont' . $cur_h . '"><a href="#' . $id . '">' . $m[3][$i] . '</a></li>' . "\n";
    
                    // Adds the 'id' attribute to the header.
                    /*$content = str_replace(
                        $m[0][$i],
                        '<h' . $m[1][$i] . ' id="' . $id . '"' . $m[2][$i] . '>' . $m[3][$i] . '</h',
                        $content
                    );*/
    
                    // Another approach - with anchors (JLady).
                    $content = str_replace(
                        $m[0][$i],
                        '<p><a name="'. $id . '"></a></p>' . "\n" . $m[0][$i],
                        $content
                    );
    
                    $prev_h = $cur_h;
                }
    
                $content = '<p><b><noindex>Содержание статьи:<noindex></b></p>' . "\n\n"
                    . '<noindex><ul class="contents">' . "\n" . $contents . '</ul></noindex>' . "\n\n"
                    . $content;
    
            }
        }
        return $content;
    }
    
    add_filter('the_content', 'mk_contents', 10);
    
    ?>

Просмотр 2 ответов — с 1 по 2 (всего 2)
  • Обычно используют тег Далее и всё… Если пользователю интересно,то он откроет всю статью.

    Ну правильно при написании статьи ставишь разделитель… статья будет отображаться полностью, а в Ленте будет краткий анонс… я ставлю разделитель примерно после 3-5 строк…. или ВОПРОС про что-то другое?

Просмотр 2 ответов — с 1 по 2 (всего 2)
  • Тема «Вопрос про содержание статьи в WP» закрыта для новых ответов.