• I am trying to customize a child theme from TwentyTwelve by making database query using the get_template_part() function in my index file that references another php file named “welcome.php.” The goal is to get users to see a customized message when they come to my index.php page of my site. I was watching a tutorial on this and I cannot replicate the above goal. My site is a local site off of MAMP, so I cannot show you a live version. My apologies.

    Here is my code from the index file that located in the child theme folder:

    <?php get_template_part( 'welcome' ); ?>

    Here is my code for the welcome.php located in the child theme folder:

    <!-- Welcome message to be displayed on the front index page -->
    
    <div id="front-page-intro" class="clear">
    
    	<article id="intro-elements">
    
    		<div class="entry-content">
    
    			<p>PLEASE WORK PLEASE WORK PLEASE WORK</p>
    
    		</div><!-- .entry-content -->
    
    	</article><!-- #intro-elements -->
    
    </div><!-- #front-page-intro -->

    Any ideas?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Normally any database queries that you’d do would be done through the WPDB class.

    Without knowing what data you want to get from the database it’s hard to suggest something more, but as a very basic example, this can be done:

    <?php
        global $wpdb;  // You need to make this variable global inside a function
    
        $name = $wpdb->get_var( 'SELECT first_name FROM ' . $wpdb->users . ' WHERE ID = ' . get_current_user_id() );
        echo $name;
    ?>
    Thread Starter webwatchman

    (@webwatchman)

    “Without knowing what data you want to get from the database it’s hard to suggest something more,” <==== I am confused. I stated I wanted the data from my “welcome.php” file. Is that not part of the database, I understand what the database is, its the tables needed to assemble the site. When I make a php file and call from it, doesn’t that add to the database?

    I understand declaring the global variable, but I am not clear on what do within the argument. Where do I call the “welcome” file in there?

    Do you have a lynda.com account where I can show you which tutorial I am looking at by chance? The video is approximately 7 mins. Its entitled in the Course “WordPress 3: Building Child Themes, session: Displaying page content within an index page”

    I think that you’ve got slightly the wrong idea about databases and how they work.

    Setting up a PHP page doesn’t add, edit or delete anything to/from the database – unless you tell it to do that and give it some data to work with. All that the databaase does is store data. It doesn’t know about PHP, it doesn’t work directly with pages, and it you don’t add to it by creating new pages. For what you want to do, you don’t need the database, so forget about that because you’ll only confuse yourself more.

    I don’t have a Lynda account, but from what you’ve said I think that I can guess what you’re looking to do. With what you’ve got there, there’s no reason at all why it wouldn’t be showing. You did say that you’re trying to include it in your index.php page, but the biggest question is what page/post/etc type are you trying to call that form, and are there any other template files in yoour theme that would get used first before your index.php file?

    WordPress works on a template hierarchy, so if you’re displaying a post, it looks for tempalte files in a set order, and uses the first one that it finds. For more details you should look at this page.

    Thread Starter webwatchman

    (@webwatchman)

    I am thankful for you providing clarity on how WP interacts (and doesn’t interact) with the database. I know I am putting the right code into the right index.php file that is in my theme since I am grabbing content from my “FRONT PAGE” page that I made and pasting it into my “BLOG” page that I made. However, thats not what I want. What I want, is to pull form my FRONT PAGE and paste that info into my home page. I think I will refer to the author of that lesson for further help. Thank you for your insight.

    Though I am familiar with WP hierarchy,
    Do you know where I can further learn about databases for wordpress?

    Everything that you’ll need to know about the WPDB class (how WordPress talks to the database):

    http://codex.wordpress.org/Class_Reference/wpdb

    Also, you should have a look at how templates work in WordPress, because you’re trying to put things in the wrong places. The index.php file of your theme is really only a last-resort backup file if no other template file can be found to handle the request.

    http://codex.wordpress.org/Template_Hierarchy

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Making a database query’ is closed to new replies.