excerpt - How to use limit post content on archive page in WordPress?

admin2025-06-02  1

I am using the advanced_ads plugin on my WordPress website which allows me to create different types of ads including image ad, rich content ad etc. The plugin allows shortcodes usage for showing ads on my website.

I am using the rich content ad type on a custom template file which allows me to show all the ads just like blog posts. Now I want to limit down the number of content for each ad to 30-50 words, so I can only show a short description of the ad and user will then view the whole ad in a single page upon clicking on the Read More button.

So far, I am willing to use the_excerpt(); but advanced_ads allows [the_ad id] shortcode to place an ad in the content. In my code, I loop through all those ads and assign the id's of each ad to the shortcode like [the_ad id=".$post->ID."] and it works fine.

However, I am willing to integrate the_excerpt(); in my code for limiting down the number of words along with showing a button like Read More that will allow users to view the full content.

Here is my code:

<?php echo '<div class="advert-div">'; ?>
<h2 class="ad-title"><?php the_title(); ?></h2>
<?php
echo do_shortcode("[the_ad id=".$post->ID."]");
echo '</div>';

I am using the advanced_ads plugin on my WordPress website which allows me to create different types of ads including image ad, rich content ad etc. The plugin allows shortcodes usage for showing ads on my website.

I am using the rich content ad type on a custom template file which allows me to show all the ads just like blog posts. Now I want to limit down the number of content for each ad to 30-50 words, so I can only show a short description of the ad and user will then view the whole ad in a single page upon clicking on the Read More button.

So far, I am willing to use the_excerpt(); but advanced_ads allows [the_ad id] shortcode to place an ad in the content. In my code, I loop through all those ads and assign the id's of each ad to the shortcode like [the_ad id=".$post->ID."] and it works fine.

However, I am willing to integrate the_excerpt(); in my code for limiting down the number of words along with showing a button like Read More that will allow users to view the full content.

Here is my code:

<?php echo '<div class="advert-div">'; ?>
<h2 class="ad-title"><?php the_title(); ?></h2>
<?php
echo do_shortcode("[the_ad id=".$post->ID."]");
echo '</div>';
Share Improve this question asked Mar 5, 2019 at 16:45 teccraftteccraft 451 gold badge1 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

use wp_trim_words() like so:

<?php 
$moreLink = '<a href="' . get_the_permalink() . '">Read More</a>';
echo '<div class="advert-div">'; ?>
    <h2 class="ad-title"><?php the_title(); ?></h2>
<?php
echo wp_trim_words(do_shortcode("[the_ad id=".$post->ID."]"), 55, $moreLink);
echo '</div>';

Just substitute however you need to get the permalink for your add in to the $moreLink if needed.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748840136a314160.html

最新回复(0)