0


I am posting Hadiths in my website and i post it in 3 translations: Arabic, Urdu, English. In Hadith's page, where Hadiths are showing one by one, It's showing Arabic excerpt text.
See my web, Here is my Website link: IF Islam

See this screenshot: enter image description here

I want here to show English Hadith excerpt text instead of Arabic and Urdu.
Which PHP code will do this?
Please help

Here is the content.php code:

<div class="col-lg-8 col-md-6">
    <div class="card-body">
       <a href="<?php the_permalink(); ?>" class="">
           <h2 class="single_post_title h3-responsive text-primary mb-1"><?php the_title(); ?></h2>
       </a>
       <div class="blog_meta">
           <time class="blog_meta_posted_on grey-text"><?php the_time('F j, Y g:i a'); ?></time>
           <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>" class="blog_meta_author grey-text"><?php the_author(); ?></a>
           <a href="<?php the_permalink(); ?>#responses" class="blog_meta_count_comments grey-text"><?php echo get_comments_number(); ?> Comments</a>
           <label class="blog_meta_tags_list">
                <?php echo get_the_tag_list( '', ', ', '' ); ?>
           </label>
       </div>
       <div class="blog_excerpt_content black-text m-0 mt-2">
           <?php the_excerpt(); ?>
       </div>
       <a href="<?php the_permalink(); ?>" class="btn btn-primary">Read More</a>
    </div>
</div>
Zain Shabir
  • 35
  • 1
  • 12
  • Hi, I think that you should construct the posts of the /hadith/ page by yourself, it's probably a category that you are showing, so you would need to create the corresponding Wordpress file for this category (something like hadith-category-slug.php). Then copy the contents of the default Wordpress category file into it. Then inside the code you need to change the way the code that showing every post of the category (the posts loop). There instead of showing the post (hadith) contents from the beginning as shown now in you current website... – Ermac Jul 30 '19 at 18:46
  • ... You need to show the text exactly from the "Hadith in English Translation" part, there are probably different solutions to get this part from the post contents, you could use [DOMDocument class](https://www.php.net/manual/en/class.domdocument.php) which can parse HTML, see [example](https://stackoverflow.com/questions/4702987/php-string-manipulation-extract-hrefs). But to keep things simpler you can try this code: `$position = strpos($post_full_contents, 'Hadith in English Translation'); $excerpt = strip_tags(substr($post_full_contents, $position));` – Ermac Jul 30 '19 at 19:05
  • How can i create seperate page for Hadiths category? I didn't understand brother – Zain Shabir Jul 31 '19 at 05:01
  • I've created seperate page for Hadiths "category-hadith.php" but how can i show there excerpt text of English Hadith? I have updated the code above, please check – Zain Shabir Jul 31 '19 at 05:42

2 Answers2

0

I think we need a little more information in order to answer your question correctly. For starters, could you show us your code that you use for showing the excerpts. Based on your question, i only can imagine that your literally typed Arabic text in the excerpt in your CMS.

DLS123321
  • 33
  • 5
  • Oke, nothing odd i can see there, what is your site-language? You can find this in your CMS under 'Settings'. Can you also make sure you didn't literally typed Arabic text in the excerpts of your posts? – DLS123321 Jul 31 '19 at 07:30
  • I have typed Hadiths in 3 languages: Arabic, Urdu and English. You can open my website link and can check post single page, then you'll get to know what i am trying to say – Zain Shabir Jul 31 '19 at 09:28
0

Try to replace

<?php the_excerpt(); ?>

With

<?php
$position = strpos(get_the_content(), 'Hadith in English Translation');
$excerpt = strip_tags(substr(get_the_content(), $position));
echo $excerpt;
?>

I think get_the_content() returns the post full contents in HTML. We constructed a new $excerpt starting from 'Hadith in English Translation' to the end of the post contents using the functions strpos and substr. We also stripped the excerpt from any HTML tags using strip_tags.

Of course you need to use the code only for the Hadith category (inside the "category-hadith.php" you added?) since you only need the modification there.

Check if it is working and note that the solution supposes that the post contents has always the string 'Hadith in English Translation' and the english translation comes in last position after the two others.

Ermac
  • 1,181
  • 1
  • 8
  • 12
  • It's working, But it's showing full English text. How can it be trim/short? – Zain Shabir Aug 01 '19 at 07:44
  • Also, I want it to show English Hadith text from next to "Hadith in English Translation" instead of showing text from Hadith in English Translation, – Zain Shabir Aug 01 '19 at 07:50
  • Hi, to shorten the text try `$excerpt = apply_filters('the_excerpt', $excerpt);`, to skip the title you can remove it with `$excerpt = str_replace('Hadith in English Translation', '', $excerpt);`. – Ermac Aug 01 '19 at 19:22