logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Toggle on blog page for the_excerpt and the_content

Is there a way that I can have a link at the top of my blog page that allows a user to toggle how the blog page is displayed?

That way there will be 2 links at the top of the page, one for full text (the_content) and one for a summary (the_excerpt). Clicking either, would toggle all posts on the page to show either content or excerpt.

I know there's probably an ajax solution around somewhere but was hoping to not do it that way.

This question has been answered.

Dan | gteh | 05/12/11 at 12:01pm Edit


The experts have suggested, on average, a prize of $20 for this question.

(2) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    05/12/11
    12:07pm
    Kailey Lampert says:

    Something like this:


    <?php if (isset($_GET['fulltext'])) { ?>
    <a href="/blog/">View Excerpts</a>
    <?php } else { ?>
    <a href="/blog/?fulltext">View Full Text</a>
    <?php } ?>


    Then

    <?php
    if (isset($_GET['fulltext'])) {
    the_content();
    } else {
    the_excerpt();
    }
    ?>

    Previous versions of this answer: 05/12/11 at 12:07pm

    • 05/12/11 12:08pm

      Dan | gteh says:

      thanks I'll give that a shot shortly.

    • 05/12/11 12:33pm

      Dan | gteh says:

      What if I wanted to make this as an option in the admin panel instead? A setting the admin change to choose to display either or?

    • 05/12/11 12:46pm

      Kailey Lampert says:

      I've actually got a plugin that will add a full-text/summary option to the Settings > Reading page.

      A small snippet will still have to placed in the theme in order to use the appropriate function based on the selection.

      I've included a screenshot of the admin screen, let me know if that sounds like what you want, then I can give you the code.

      Attached Image

    • 05/12/11 12:58pm

      Dan | gteh says:

      that is exactly what I need. For the posts on the blog page.

    • 05/12/11 1:05pm

      Kailey Lampert says:

      It's attached. Unzip and upload to your plugins folder, then activate it through the plugins page of your WordPress installation.

      The snippet that will go into your theme looks like this:

      switch(get_option('fulltext_or_summary','summary')) {

      case 'fulltext' :
      the_content();
      break;
      case 'summary' :
      the_excerpt();
      break;
      default :
      the_excerpt();

      }

      Attached Image

    • 05/12/11 1:06pm

      Dan | gteh says:

      thanks. so that code goes in place of where I have the_content right now?

    • 05/12/11 1:08pm

      Kailey Lampert says:

      Right - and if your version of the_content() has something extra - like

      the_content("Read more...");


      You can add your fancier version in place of the plain one in my snippet

    • 05/12/11 1:25pm

      Dan | gteh says:

      thanks worked perfectly.

  • avatar
    Last edited:
    05/12/11
    12:20pm
    AdamGold says:

    If you want a solution with no refresh / page changing (and not AJAX), I suggest you to do something like this:
    1. In single.php, I don't know exactly how your file looks but just copy the post DIV and paste it right below, and add display attribute & change the_content to the_excerpt. Example:
    This is your post DIV:

    <div id="post<?php get_ID(); ?>" class="post">
    <?php the_content(); ?>
    </div>

    Place below:
    <div id="post<?php get_ID(); ?>" class="post excerpt" style="display: none">
    <?php the_excerpt(); ?>
    </div>

    And add full-content class to the original post div:
    <div id="post<?php get_ID(); ?>" class="post full-content">


    2. Add the following code in where you want the buttons to be:

    <script type="text/javascript">
    jQuery(document).ready( function() {
    jQuery('#full_content').click( function() {
    if( jQuery('.full-content').is(':hidden') ) {
    jQuery('.excerpt').fadeOut(400, function() {
    jQuery('.full-content').fadeIn();
    });
    }
    });
    jQuery('#excerpt').click( function() {
    if( jQuery('.excerpt').is(':hidden') ) {
    jQuery('.full-content').fadeOut(400, function() {
    jQuery('.excerpt').fadeIn();
    });
    }
    });
    });
    </script>
    <a href="#" id="full_content">Show full content</a>
    <a href="#" id="excerpt">Show excerpt</a>

    • 05/12/11 12:33pm

      Dan | gteh says:

      What if I wanted to make this as an option in the admin panel instead? A setting the admin change to choose to display either or?

    • 05/12/11 12:38pm

      AdamGold says:

      Ah so you don't want the users to choose? You just want to choose whether to display full content or excerpts? I suggest you to add somewhere in your admin (if you don't know how, please raise the prize and I'll tell you) the following:

      <?php
      if( isset($_POST['save']) ) {
      if( !empty($_POST['toggle-view']) ) {
      update_option('toggle_view', $_POST['toggle-view']);
      }
      ?>
      <form method="post" action="">
      <label for="toggle-view">Posts view</label>
      <select name="toggle-view" id="toggle-view">
      <option value="full">Full Content</option>
      <option value="exc">Excerpts</option>
      </select>
      </form>


      Then in your page that displaying the posts, replace the_content() (or the_excerpt) with:

      if( get_option('toggle_view', 'full') ) {
      the_content();
      } else if( get_option('toggle_view', 'exc') ) {
      the_excerpt();
      }

    • 05/12/11 12:39pm

      AdamGold says:

      Sorry, first code should be:

      <?php
      if( isset($_POST['save']) ) {
      if( !empty($_POST['toggle-view']) ) {
      update_option('toggle_view', $_POST['toggle-view']);
      }
      }
      ?>
      <form method="post" action="">
      <label for="toggle-view">Posts view</label>
      <select name="toggle-view" id="toggle-view">
      <option value="full">Full Content</option>
      <option value="exc">Excerpts</option>
      </select>
      </form>

    • 05/12/11 12:45pm

      Dan | gteh says:

      thanks.. my client changed his mind afterward. I'll give this a shot and if I need help I'll raise the prize and ask you.

This question has expired.



Dan | gteh voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.