WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Way to show content of a post, but if exceeds character limit revert to excerpt? (22 posts)

  1. Danny Albeck
    Member
    Posted 2 years ago #

    Hello,

    So I am working on a site for a client of mine. We have about 50+ posts filed in a category called "Articles". I created a custom page template that has a custom query to display all the posts in a bullet list on this page, and if you click on it you can go directly to the post to read more or open a PDF directly. Where I am hitting a road block is we have 2 specific articles that are more then just PDF's, they are about 3-6 paragraphs instead. SO I wanted to know if it is possible to display all the posts in the list like I am now, and if it exceeds around 100 characters then revert to the excerpt we made for those posts specifically.

    Currently my code looks like this:

    <?php
    				//for each child of category 16 (articles), show title of posts
    				$cat_args=array('child_of' => 16, 'orderby' => 'name', 'order' => 'DESC');
    				$categories=get_categories($cat_args);
    				  foreach($categories as $category) {
    				    $args=array('showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1);
    				    $posts=get_posts($args);
    				      if ($posts) {
    				        echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></h3> ';
    				        foreach($posts as $post) {
    				          setup_postdata($post); ?>
    				          	<!--<li><?php the_title_attribute(); ?></li>-->
    				          	<ul>
    				          		<li><?php echo get_the_content(); ?></li>
    				          	</ul>
    				          <?php
    				        } // foreach($posts
    				      } // if ($posts
    				    } // foreach($categories
    				?>
  2. vtxyzzy
    Member
    Posted 2 years ago #

    I think changing the code inside the li tags to this will do what you want:

    <li>
       <?php $content = apply_filters('the_content',get_the_content());
       if ( strlen($content) > 300 ) {  // Max 300 characters
          the_excerpt();
       } else {
          echo $content;
       }
       ?>
    </li>

    I added in the apply_filters to allow shortcodes and other filters to work in the content. If this is not what you want, remove the call to that function.

  3. Danny Albeck
    Member
    Posted 2 years ago #

    Vtxyzzy,

    I could be getting closer. I decided to change my code to this:

    <?php
    				//for each child of category 16 (articles), show title of posts
    				$cat_args=array('child_of' => 16, 'orderby' => 'name', 'order' => 'DESC');
    				$categories=get_categories($cat_args);
    				  foreach($categories as $category) {
    				    $args=array('showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1);
    				    $posts=get_posts($args);
    				      if ($posts) {
    				        echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></h3> ';
    				        foreach($posts as $post) {
    				          setup_postdata($post); ?>
    				          	<!--<li><?php the_title_attribute(); ?></li>-->
    				          	<ul>
    				          		<li><a href='<?php echo get_the_content(); ?>'><?php echo get_the_excerpt(); ?></a></li>
    				          	</ul>
    				          <?php
    				        } // foreach($posts
    				      } // if ($posts
    				    } // foreach($categories
    				?>

    So now the excerpt is showing which is fine, but I still need it to link to the content of the page. I really need it to work as if you click on the excerpt it will automatically open the PDF like it was earlier, or take you to the post (in the case of the 2 that had content other then a PDF). But the link is not generating correctly, its shoing this:

    http://domain.com/articles/<p&gt;Content here</p>

    now its showing the correct file url in the place of the "#" and the correct name of the content in place of the "Content here" that I put in the example above.

    Any ideas?

  4. vtxyzzy
    Member
    Posted 2 years ago #

    Please give this a try:

    <li>
       <?php $content = get_the_content();
       $output = ( strlen($content) < 300 ) ? $content : get_the_excerpt(); ?>
       <a href='<?php echo $content; ?>'><?php echo $output; ?></a>
    </li>
  5. Danny Albeck
    Member
    Posted 2 years ago #

    Awesome! Thank you so much! It appears that was exactly what I was looking for!

  6. Danny Albeck
    Member
    Posted 2 years ago #

    vtxyzzy,

    I may have jumped the gun on marking as solved. The code you provided works like a charm on the one page I was referencing above, I am trying to re-purpose it on another section that has a scroll bar that pulls the latest news "news post category". It basically just shows the date and then pulls the content. The same with the page I referenced above it will open directly to a PDF 95% of the time, but there are like 2 instances that the link is a post with no PDF and just has a little bit of copy.

    My code looks like:

    <ul>
    								<?php $recent = new WP_Query("cat=7&showposts=0"); while($recent->have_posts()) : $recent->the_post(); ?>
    									<li>
    										<span><?php the_date('F Y'); ?></span><br />
    										<?php $content = get_the_content();
    										$output = ( strlen($content) < 300 ) ? $content : get_the_excerpt(); ?>
    										<strong><a href='<?php echo $content; ?>'><?php echo $output; ?></a></strong>
    									</li>
    				                <?php endwhile; ?>
    								</ul>

    The problem I am getting is, that the posts that aren't just a linked PDF, aren't displaying the published date above them, and they are generating a bad link. Any ideas? Or is there a way to revert to the permalink if there is no content (PDF)?

    Thanks for the help!

  7. vtxyzzy
    Member
    Posted 2 years ago #

    The problem with the date may be that the_date() only returns the date for the first post on a given day. So, if you have multiple posts on one day, it will only show the date on the first one. Use get_the_date() instead.

    Is the number '300' in the code right for you? That was just a number I picked out of the air. You may need to adjust it.

    I don't know what 'showposts=0' is supposed to do. Showposts was deprecated in favor of posts_per_page, so you should use that instead.

    Finally, I can't even guess about the bad links without at least seeing what is generated, and maybe not without dumping the content.

  8. Danny Albeck
    Member
    Posted 2 years ago #

    Ok so I fixed the date issue, I ended up using the_time('F Y') instead.

    300 worked fine, I actually bumped it to 400.

    I deleted showposts.

    The link looks like:

    Working Link:

    http://domain.com/wp-content/uploads/12345.pdf

    Bad Link (Post that isn't just a link to a PDF but actually has content on the page):

    http://domain.com/Name of Post Here (where if it were pulling correctly would display more like: http://domain.com/2012/01/01/name-of-post-here/)

    But the code sample is saying to pull the content, so I understand why it wouldn't work for a post that has content on it instead of just a link to a PDF (which is why the others are working).

    I appreciate your help.. big time. I apologize I can't provide direct links but I simply replace the real domain with domain.com for privacy sakes to my client (lawyer) and also it is being developed with the assistance of using a hosts file to trick the machine in to thinking the domain is live. So you wouldn't be able to access it publicly anyway. Hope this helps! Thanks again!

  9. vtxyzzy
    Member
    Posted 2 years ago #

    I really can't do anything else without seeing the actual pages, code, links, etc.

  10. Danny Albeck
    Member
    Posted 2 years ago #

    I will try to provide the code here:

    <ul>
    								<?php $recent = new WP_Query("cat=53"); while($recent->have_posts()) : $recent->the_post(); ?>
    									<li>
    										<span><?php the_time('F Y') ?></span><br />
    										<?php $content = get_the_content();
    										$output = ( strlen($content) < 400 ) ? $content : get_the_excerpt(); ?>
    										<strong><a href='<?php echo $content; ?>'><?php echo $output; ?></a></strong>
    									</li>
    				                <?php endwhile; ?>
    								</ul>

    This is an example of post that is opening a PDF when you click on the link in the "Latest News" box on the home page:

    <a href="http://domain.com/wp-content/uploads/2012/05/No-to-Signal-Class-Says.pdf">This is my news post.</a>.

    This is an example of a post that has copy on the page and not just a link to the PDF like the above post example:

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna enim, tincidunt quis ultrices id, ultricies sed elit. Vestibulum blandit malesuada dignissim.</p>

    So the difference here is that there is a paragraph of text on this page, which is giving me a bad link. But the 1st sample I showed you is literally just a link to a PDF in the Media Library so it just opens the PDF. What I am trying to achieve here is if you we have a post that is just PDF like the first sample of code then it will open that up like it does now, and if there ins't a PDF and there is content like in the 2nd example, it will take you to that post to read it.

  11. vtxyzzy
    Member
    Posted 2 years ago #

    Both post examples you showed are less than 400 characters. Both would use the content as the link. It seems that you cannot use the length of the post to determine how to build the output.

    I will work on a different solution and get back to you.

  12. vtxyzzy
    Member
    Posted 2 years ago #

    Question: Will any of the posts that should link to the actual post contain any links to .pdf files?

    I am thinking about testing for a link to a .pdf file, but that will not work if there will be any links like that in posts that should link to the post and not the .pdf.

  13. vtxyzzy
    Member
    Posted 2 years ago #

    Please show me what the HTML output looks like for the two posts you mentioned above. The code you showed looks like it might not be correct even for the .pdf files.

    I need to see the actual output HTML, but with the domain names substituted. I just need to see the code for the two li elements.

  14. Danny Albeck
    Member
    Posted 2 years ago #

    The HTML output inside the news scroller looks like this:

    <ul>
    <li>
    										<span>April 2012</span><br>
    																				<strong><a href="<a href=&quot;http://domain.com/wp-content/uploads/2012/05/No-Magic-Words-are-Needed-to-Signal-Class-Arbitration-Internt-Court-Says.pdf&quot;>Achieve Major Victory Allowing for Class Action Arbitrations</a>."></a><a href="http://domain.com/wp-content/uploads/2012/05/No-Magic-Words-are-Needed-to-Signal-Class-Arbitration-Internt-Court-Says.pdf">Achieve Major Victory Allowing for Class Action Arbitrations</a>.</strong>
    									</li>
    </ul>

    That is one item that does work and opens the PDF.

    This is the HTML output for one that isn't opening properly because it doesn't contain a PDF:

    <li>
    										<span>December 2011</span><br>
    																				<strong><a href="<a href=&quot;http://domain.com/wp-content/uploads/2012/05/Color.pdf&quot;>$5 million jury verdict and settlement against DYFS for negligence</a>."></a><a href="http://domain.com/wp-content/uploads/2012/05/Color.pdf">$5 million jury verdict and settlement against DYFS for negligence</a>.</strong>
    									</li>

    To answer your question about the PDFs on all pages. This wont be the case. It will either just have a PDF on these pages or it will have copy on it without a PDF.

    Thank you for all of your help!

  15. vtxyzzy
    Member
    Posted 2 years ago #

    Actually, the pdf one is not correct. It has improperly nested a tags. See this section right at the front (see the second a tag inside the first?):

    < strong><a href="<a href=

    and this one in the middle:

    >Achieve Major Victory Allowing for Class Action Arbitrations</a>."></a><a href="http://domain.com

    Those will probably cause trouble in some browsers.

    And, in the HTML you showed for the broken link, I did not see the code from the post you showed earlier as failing!

    I can't stress enough that I must see the EXACT contents of the post and the EXACT MATCHING HTML. Without that I am just wasting my time.

    Next question: What do you want to show as the link text when there is no pdf. The way the code is written, it would show the entire contents or excerpt of the post as the link text.

    Another question: Since checking for the length will not work, do you always want to show the excerpt when there is no pdf?

  16. Danny Albeck
    Member
    Posted 2 years ago #

    Let me ask you, is their a more private method I can use to discuss with you? Skype? Email? Then I could help give you more info to be able to dig around on the site.

  17. Danny Albeck
    Member
    Posted 2 years ago #

    This is right from the source:

    <ul>
    																	<li>
    										<span>April 2012</span><br />
    																				<strong><a href='<a href="http://domain.com/wp-content/uploads/2012/05/No-Magic-Words-are-Needed-to-Signal-Class-Arbitration-Internt-Court-Says.pdf">M S Achieve Major Victory Allowing for Class Action Arbitrations</a>.'><a href="http://domain.com/wp-content/uploads/2012/05/No-Magic-Words-are-Needed-to-Signal-Class-Arbitration-Internt-Court-Says.pdf">M S Achieve Major Victory Allowing for Class Action Arbitrations</a>.</a></strong>
    									</li>
    				                									<li>
    										<span>April 2012</span><br />
    																				<strong><a href='<a href="http://domain.com/wp-content/uploads/2012/04/20120530163040544.pdf">$38.5 Million class action settlement against Honda and Acura for defects in air conditioning systems.</a>'><a href="http://domain.com/wp-content/uploads/2012/04/20120530163040544.pdf">$38.5 Million class action settlement against Honda and Acura for defects in air conditioning systems.</a></a></strong>
    									</li>
    				                									<li>
    										<span>February 2012</span><br />
    																				<strong><a href='<a href="http://domain.com/wp-content/uploads/2012/05/20120329105507966.pdf">M S obtains Final Approval of Dentists’ National Class Action Settlement</a>.'><a href="http://domain.com/wp-content/uploads/2012/05/20120329105507966.pdf">M S obtains Final Approval of Dentists’ National Class Action Settlement</a>.</a></strong>
    									</li>
    				                									<li>
    										<span>January 2012</span><br />
    																				<strong><a href='<a href="http://domain.com/wp-content/uploads/2012/01/35-million.pdf">Appellate Division upholds denial of $35 million claim</a>'><a href="http://domain.com/wp-content/uploads/2012/01/35-million.pdf">Appellate Division upholds denial of $35 million claim</a></a></strong>
    									</li>
    				                									<li>
    										<span>January 2012</span><br />
    																				<strong><a href='A S once again in Top 100'>A S once again in Top 100</a></strong>
    									</li>
    				                									<li>
    										<span>January 2012</span><br />
    																				<strong><a href='D M once again in the Top 10'>D M once again in the Top 10</a></strong>
    									</li>
    				                									<li>
    										<span>January 2012</span><br />
    																				<strong><a href='M S once again has 5 Super Lawyers'>M S once again has 5 Super Lawyers</a></strong>
    									</li>
    				                									<li>
    										<span>January 2012</span><br />
    																				<strong><a href='M S once again selected as a top plaintiff’s law firm by Benchmark Publications.'>M S once again selected as a top plaintiff’s law firm by Benchmark Publications.</a></strong>
    									</li>
    				                									<li>
    										<span>December 2011</span><br />
    																				<strong><a href='<a href="http://domain.com/wp-content/uploads/2012/05/Color.pdf">$5 million jury verdict and settlement against DYFS for negligence</a>.'><a href="http://domain.com/wp-content/uploads/2012/05/Color.pdf">$5 million jury verdict and settlement against DYFS for negligence</a>.</a></strong>
    									</li>
    				                								</ul>

    That is pulled from the News Scroll bar

  18. vtxyzzy
    Member
    Posted 2 years ago #

    Here is a pastebin link with sample code that I think will do what you want.

    Note that I used my own category id for testing. Change it to yours.

    Here is a description of what the code does inside the loop:

    Get the content into $content
    If $content contains a link to a .pdf file
       Echo the link
    Else
       Get the post title
       If the length of $content is greater than 400
          Replace $content with the excerpt
       Endif
       Echo the post title as a link to the post
       Echo $content in a paragraph
    Endif
  19. Danny Albeck
    Member
    Posted 2 years ago #

    I will try out tomorrow when I get in to the office. Thank you for all of your assistance. I will keep you posted tomorrow!

  20. Danny Albeck
    Member
    Posted 2 years ago #

    vtxyzzy thank you for all of your help. Below is my very slightly modified code:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    The last thing I would like to achieve if it is possible you will see I put:

    echo "$content";

    Is it possible to link $content to the permalink to the post?

    Thanks!

  21. Danny Albeck
    Member
    Posted 2 years ago #

    echo "<a href='#'>$content</a>";

    Sorry that is what it was supposed to read.

  22. Danny Albeck
    Member
    Posted 2 years ago #

    Alright, I created this pastbin snippet: http://pastebin.com/dtdBJcpd

    Anyway to the list the category title over each sections posts. Right now I have a News category, with sub categories by year. I just want to show the year over the proper posts in an h3 tag. I am so lost on this one, this site is definitely teaching me a few lessons.

    Thanks to anyone who can lend a hand!

Topic Closed

This topic has been closed to new replies.

About this Topic