1

I hope someone can help me out.

I'm working on a website where I installed Wordpress. On the frontpage I have a small div where I want to output the first 10 words from another page with pageID. At the moment it's almost working but instead of loading in 10 words it's loading in 10 letters.

Example:
Instead of: Hello my name is Erwin and this is a test
It loads: Hello my nam

What am I doing wrong?

I Use the following php in functions.php:

if(!function_exists('getPageContent'))
{
    function getPageContent($pageId,$num_words)
    {
        if(!is_numeric($pageId))
        {
            return;
        }
        global $wpdb;
        $nsquery = 'SELECT DISTINCT * FROM ' . $wpdb->posts .
        ' WHERE ' . $wpdb->posts . '.ID=' . $pageId;
        $post_data = $wpdb->get_results($nsquery);
        if(!empty($post_data))
        {
            foreach($post_data as $post)
            {
                $text_out=nl2br($post->post_content);
                $text_out=str_replace(']]>', ']]>', $text_out);
                $text_out = strip_tags($text_out);
                return substr($text_out,0,$num_words);

            }
        }
    }}



And I use the following string to load in the content:
(Where 89 is my Page ID)

echo getPageContent(89,10);
Erwin van Ekeren
  • 710
  • 4
  • 13
  • 36

4 Answers4

1

This will extract first 10 words of your string

$text_out = "Hello my name is Erwin and this is a test and this is another test";
$num_words = 10;
echo implode(' ', array_slice(explode(' ', $text_out), 0, $num_words));

Output

Hello my name is Erwin and this is a test
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
  • Thank you so much for your quick answer, but its a different problem. I know I have to change the 10 to 32 to load in the first 32 letters but what I want is that I can load in the first words. (because the text in the other page can be different everyday and I dont want to count the letters all the time.. I just want that the script automatic loads in the first words :) – Erwin van Ekeren Apr 08 '13 at 09:54
  • use `strlen` function it will automatically count the length of string, here is the manual http://php.net/manual/en/function.strlen.php – Sumit Bijvani Apr 08 '13 at 09:58
  • so you want only 1st word as output? – Sumit Bijvani Apr 08 '13 at 10:02
  • the first 10 words as output :) – Erwin van Ekeren Apr 08 '13 at 10:11
  • @ErwinvanEkeren check my edited answer, it will help you to extract first 10 words of string – Sumit Bijvani Apr 08 '13 at 10:40
0

There is a function from wordpress:

the_excerpt();

which does that exactly. It shows ... the excerpt of the article or page and a link at the end. You can customize the length using the argument the_excerpt($length) modifiing the function a bit.

Source: http://codex.wordpress.org/Function_Reference/the_excerpt

aleation
  • 4,796
  • 1
  • 21
  • 35
  • Thanks for your answer! I found that one too but somehow I can't connect it to a certain pageID. It just loads in the first 10 words of the page where i'm currently on and it doesn't ouput the content from another page – Erwin van Ekeren Apr 08 '13 at 10:13
  • because you probably have to load the file containing the function or something like that. – aleation Apr 08 '13 at 10:16
  • my skills with php are the low for what you just said haha! – Erwin van Ekeren Apr 08 '13 at 10:23
0

How to select first 10 words of a sentence? - same question. Just use implode(' ', array_slice(explode(' ', $text_out), 0, $num_words)); instead of substr($text_out,0,$num_words);

Community
  • 1
  • 1
Sergei Gorjunov
  • 1,789
  • 1
  • 14
  • 22
0

Here $num_words = 10

it means substr breaks the string from character 0 to 10

if you want whole string then you have to echo

echo getPageContent(89,32);
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82