简体   繁体   English

在每个页面上显示页面内容的开始部分-Wordpress

[英]Display the beginning part of a page content on every page - wordpress

I have built a Twentyeleven template site in wordpress, I only use pages in CMS (no posts). 我在wordpress中建立了一个二十一模板网站,我只在CMS中使用页面(无帖子)。 I have a page called "Classes", its page content will be updated regularly. 我有一个名为“ Classes”的页面,其页面内容将定期更新。

I'd like to add a square on the right hand corner of every page which displays the beginning part of the 'Classes' page, and a 'more' link underneath can lead people to the 'Classes' page. 我想在每个页面的右上角添加一个正方形,以显示“类”页面的开始部分,下面的“更多”链接可以将人们带到“类”页面。

At the moment, I just added a html div in the content-page.php as I am learning. 此刻,我在学习时刚刚在content-page.php中添加了一个html div。 Is there a plugin can achieve what I am aiming? 是否有插件可以实现我的目标? Or can you please give me some directions on how to code this (some suggested code or learning links would be helpful)? 还是可以给我一些如何编写代码的指导(一些建议的代码或学习链接会有所帮助)?

Thank you. 谢谢。

If you are trying to do it in PHP you can try something like this 如果您尝试使用PHP进行操作,则可以尝试这样的操作

<?php         
    $page_id = 11; //set page ID
    $page_data = get_page($page_id);
    $title = $page_data->post_title;
    $permalink = get_permalink($page_id);
    echo '<a href="'. $permalink .'">'. $title . '</a>';
    echo '<p>' . the_excerpt() . '<a href="'. $permalink . '">Read more</a></p>';
?>

If you need the page ID for this script you can use this 如果您需要此脚本的页面ID,则可以使用此

<?php
     $pages = get_pages();
     foreach ( $pages as $page ) {
         echo 'ID: '. $page->ID . ' title: ' . $page->post_title.'<br>';
     }
?>

If you are trying to do it based on a widget you need to ensure these steps. 如果您尝试基于小部件执行此操作,则需要确保执行以下步骤。

Inside the Twenty Twelve theme you have the section called Main Sidebar in the Widgets section. 在“十二十二”主题内,“ 小部件”部分中有一个名为“ 主边栏 ”的部分。 From this area you would add your widget that you want and ensure that all the pages you want the widget to be in is set to the " default theme " and not "Full-width Page Template, No Sidebar". 在此区域中,您将添加所需的窗口小部件,并确保希望窗口小部件进入的所有页面均设置为“ 默认主题 ”,而不是“全角页面模板,无侧边栏”。

Also I do not know if you have made any changes to your code but if you have, you need to ensure that your wp-content/themes/twentytwelve/index.php still has this prior to the footer section. 另外,我不知道您是否对代码进行了任何更改,但是如果有更改,则需要确保在页脚部分之前,您的wp-content / themes / twentytwelve / index.php仍然具有此更改。

<?php get_sidebar(); ?> 

If you want to widen your widget area you can tamper with the CSS that controls the width but be warned that this could affect the stability of the items inside your container. 如果要扩大窗口小部件区域,则可以篡改控制宽度的CSS,但要注意这可能会影响容器内项目的稳定性。

wp-content/themes/twentytwelve/style.css wp-content / themes / twentytwelve / style.css

Line 1446 - 1449 1446-1449行

Edit the Width of 编辑的宽度

.widget-area {
    float: right;
    width: 26.041666667%;
}

You would also need to edit the Main site-content to have the proper proportioned percentage if the prior value is edited. 如果编辑了先前值,则还需要编辑主站点内容以具有适当的比例百分比。

Line 1437 - 1440 1437-1440行

.site-content {
    float: left;
    width: 65.104166667%;
}

You don't have to use a plugin 您不必使用插件

The thing you are trying to achieve is quite easily done by editing your template files, depending on your needs. 您可以根据需要通过编辑模板文件来轻松实现您想要实现的目标。 I will suggest the simplest solution, but perhaps you may transform this snippet into a widget for a more dynamic/maintainable usage. 我将建议最简单的解决方案,但也许您可以将此代码段转换为小部件,以实现更动态/可维护的用法。

  1. Edit the page.php template file in order to make something appear in every page of your Wordpress installation. 编辑page.php模板文件,以使某些内容出现在Wordpress安装的每个页面中。

  2. Set up the desired layout for your box (perhaps <div> with some float: right ). 为您的盒子设置所需的布局(也许<div>带有一些float: right )。

  3. Get the page you desire and store it into a php variable, like so: <?php $page = get_page( $page_id ); ?> 获取所需的页面并将其存储到php变量中,如下所示: <?php $page = get_page( $page_id ); ?> <?php $page = get_page( $page_id ); ?>

  4. Get the position of the <!--more--> tag, using stripos . 使用stripos获取<!--more--> stripos <!--more-->标记的stripos

  5. Get only the part before the <!--more--> tag, using substr . 使用substr仅获取<!--more--> substr <!--more-->标记之前的部分。

  6. Apply the_content filters and then echo . 应用the_content过滤器,然后echo

Your code will look a bit like this: 您的代码将如下所示:

<div class="right-info">
    <?php
    $page = get_page(id);
    $more_tag = stripos( $page->post_content, '<!--more-->' );
    $excerpt = substr( $page->post_content, 0, $more_tag ); 
    echo apply_filters('the_content', $excerpt);
    ?>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM