简体   繁体   中英

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). I have a page called "Classes", its page content will be updated regularly.

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. 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         
    $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

<?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.

<?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.

wp-content/themes/twentytwelve/style.css

Line 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

.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.

  2. Set up the desired layout for your box (perhaps <div> with some float: right ).

  3. Get the page you desire and store it into a php variable, like so: <?php $page = get_page( $page_id ); ?> <?php $page = get_page( $page_id ); ?>

  4. Get the position of the <!--more--> tag, using stripos .

  5. Get only the part before the <!--more--> tag, using substr .

  6. Apply the_content filters and then 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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