WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Random quote in header

[Resolved] Random quote in header

  • I’m trying to get a random quote on my site. I have taken the code below from my legacy site but cannot get it to display anything within WordPress, regardless of the theme I am using. The code is as follows:

    <?php
    	/* Pull quote from file for header                           */
        $quote = file("localhost/assets/wp-content/themes/atahualpa34/inchrist.txt");
    	srand((double)microtime()*1000000);
    	$inquote = $quote[rand(1,count($quote))];
    	/* End pull quote                                       	*/
    ?>

    I suspect the problem is in the “$quote = file(“….); line. The text file with the quotes is in the theme’s root directory. I have tried file(“/inchrist.txt), file(“inchrist.txt), as well as the full path above. I have tried it with and without the plugin Exec-PHP.

    The quote should display with the single line of code:
    <?php echo $inquote; ?>

    Can anyone point me in the right direction, please?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael

    @alchymyth

    Forum Moderator

    i think the following code points to the root directory of the theme:
    <?php bloginfo('template_url'); ?>

    So you’d use..
    $quote = file(get_bloginfo('template_directory').'/inchrist.txt');
    or
    $quote = file(get_bloginfo('template_url').'/inchrist.txt');

    Or to make it a little easier to read..

    $quotepath = get_bloginfo('template_directory');
    $quote = file($quotepath.'/inchrist.txt');

    Thanks @alchymyth and @t31os. Got the syntax right with your help and its working. Much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Random quote in header’ is closed to new replies.
Skip to toolbar