wordpress列数布局
WordPress is a great CMS, but implementing some features within your theme can require a little lateral thinking. The content for your page or post is usually output by the theme code using a single function call:
WordPress是一个很棒的CMS ,但是在主题中实现某些功能可能需要一些横向思考。 页面或帖子的内容通常是通过主题代码使用单个函数调用输出的:
the_content(args);
the_content( args );
But what if you need to split the content into two or more blocks? That might be necessary if your theme requires multiple columns or sections on the page. WordPress provides a get_the_content() function to return content as a PHP variable, but how do you determine where the divisions occur? There are a few solutions on the web, but most involve either:
但是,如果您需要将内容分成两个或多个块怎么办? 如果您的主题需要页面上有多个列或部分,则可能有必要。 WordPress提供了get_the_content()函数以将内容作为PHP变量返回,但是您如何确定分隔发生的位置呢? Web上有一些解决方案,但大多数解决方案涉及:
Splitting the content at HTML tags such as h2 headings. Unfortunately, that requires the content author to know a little HTML and it’s not very versatile — you couldn’t allow two headings in one column.
在HTML标签(例如h2标题)处分割内容。 不幸的是,这要求内容作者了解一点HTML,而且它不是很通用-您不能在一列中包含两个标题。
Using a WordPress shortcode. That’s more flexible, but it still puts the onus on the content editor to remember and use the right code. 使用WordPress简码。 这更灵活,但是它仍然使内容编辑器有责任记住和使用正确的代码。The WordPress <!--more--> tag may offer a better solution. It’s normally used to split a long article into two or more pages, but not all themes use that facility and it only works for WordPress posts by default (not pages). Using the <!--more--> tag offers several advantages:
WordPress <!--more--> more- <!--more-->标签可能会提供更好的解决方案。 通常用于将较长的文章分成两个或多个页面,但并非所有主题都使用该功能,并且默认情况下仅适用于WordPress帖子(不适用于页面)。 使用<!--more-->标签具有以下优点:
A “more” toolbar button is available in both the visual and HTML editing pane. 可视和HTML编辑窗格中均提供“更多”工具栏按钮。 Divisions can be placed anywhere in the content. 分区可以放置在内容中的任何位置。 It’s easy for non-technical users to understand how the content will be split. 非技术用户很容易理解如何拆分内容。To split your content, locate your theme folder (wp-content/themes), edit or create a functions.php file and add the following function within a <?php … ?> block:
要拆分内容,请找到主题文件夹(wp-content / themes),编辑或创建一个functions.php文件,并在<?php…?>块内添加以下函数:
// split content at the more tag and return an array function split_content() { global $more; $more = true; $content = preg_split('/<span id="more-d+"></span>/i', get_the_content('more')); for($c = 0, $csize = count($content); $c < $csize; $c++) { $content[$c] = apply_filters('the_content', $content[$c]); } return $content; }You now need to locate the theme files which call the_content() within the WordPress loop. You should find it in single.php and page.php since these are used to display single posts and pages respectively. It may also be found in index.php, archive.php and search.php, however, these normally show more than one article so be careful how multiple content blocks are handled.
现在,您需要在WordPress循环中找到调用the_content()的主题文件。 您应该在single.php和page.php中找到它,因为它们分别用于显示单个帖子和页面。 也可以在index.php , archive.php和search.php中找到它们 ,但是这些通常显示多于一篇文章,因此请注意如何处理多个内容块。
Once you’ve found the relevant code, comment out the_content() and call the split_content() function. It returns the content as an array; each element contains a single content block split at the <!--more--> tag, e.g. $content[0], $content[1], $content[2] … etc. The HTML can then be output as required, e.g.
找到相关代码后,注释掉the_content()并调用split_content()函数。 它以数组形式返回内容; 每个元素包含在<!--more-->标记处分割的单个内容块,例如$ content [0],$ content [1],$ content [2]…等。然后可以根据需要输出HTML,例如
< ?php // original content display // the_content('<p>Read the rest of this page »</p>'); // split content into array $content = split_content(); // output first content section in column1 echo '<div id="column1">', array_shift($content), '</div>'; // output remaining content sections in column2 echo '<div id="column2">', implode($content), '</div>'; ?>I hope you find it useful.
希望对你有帮助。
翻译自: https://www.sitepoint.com/split-wordpress-content-into-multiple-sections/
wordpress列数布局
相关资源:25个经典网站源代码