0

So in order to have some php code executed inside a page on wordpress there are 2 ways. One of them is to create a page template and when creating the new page in wordpress set the Page Attribute to this new page template.

This is what i have done. I have created a custom template called stockchart.php. I have copied the default content of page.php into it. Then i added my php code below the first line

But it still doesnt work i dont know why?

Here is my custom template:

<?php /* Template Name: StockChart */ ?>

<?php print("hello") ?>

<?php
    $dbHost = "xxx";
    $dbDatabase = "xxx";
    $dbPasswrod = "xxx";
    $dbUser = "xxx";
    $mysqli = mysqli_connect($dbHost, $dbUser, $dbPasswrod, $dbDatabase);

/* Getting stock1 table data */
    $sql = "SELECT TradeDate as date, Lscore2 as L FROM levermann_kurz WHERE Stock_short='AAPL' ORDER BY TradeDate asc";
    $stock1 = mysqli_query($mysqli,$sql);
    $stock1 = mysqli_fetch_all($stock1,MYSQLI_ASSOC);
    $stock_dual = json_encode($stock1);
?>


<?php get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
        // Start the loop.
        while ( have_posts() ) : the_post();

            // Include the page content template.
            get_template_part( 'template-parts/content', 'page' );

            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) {
                comments_template();
            }

            // End of the loop.
        endwhile;
        ?>

    </main><!-- .site-main -->

    <?php get_sidebar( 'content-bottom' ); ?>

</div><!-- .content-area -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
  • 1
    please custom query see example link for codex :codex.wordpress.org/… – vikrant zilpe Apr 6 '19 at 5:52
  • 1
    What doesn't work? Your $stock_dual never seems to get used. Do you see "Hello" on the front-end? – Alexander Holsgrove Apr 6 '19 at 10:46
  • Is levermann_kurz the WordPress database or another one entirely? – Howard E Apr 6 '19 at 10:46
  • I don’t see hello on the frontend. And yes the levermann_kurz ist a Table on another database Server altogether. So this should I theory display hello on that page using that custom template? – AndiAna Apr 6 '19 at 12:42
  • I don't find stock_dual used anywhere, and instead of establishing database connection again, you should use $wpdb global variable. – Akshat Apr 6 '19 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.