1

I want to have functionality like this: A random quotes appears in particular section of the website (say sidebar) after a particular amount of time(say everyday).

I want to power it up using a new table in the database.

So maybe I will have these columns-

  • Quote_Text
  • Quote_Timestamp
  • Quote_weblink (if any)
  • Quoted_by

etc.

Can you help me get started on the code?

I know sql and can learn requisite php (I know other languages like java, c++ etc). But I don't have any experience with CSS (apart from some basic syntax).

8
  • 2
    Ok then I will remove that part and ask for the coding help.
    – rents
    Nov 11, 2014 at 4:47
  • 2
    Start with custom post types and custom fields, no need for a new table.
    – Milo
    Nov 11, 2014 at 5:00
  • 1
    Please stop using the wordpress.org tag Nov 11, 2014 at 5:06
  • 1
    how will it give you extra flexibility? there's already a whole API built to work with the native content types. spend your time on parts that aren't already built for you.
    – Milo
    Nov 11, 2014 at 5:25
  • 1
    To get you properly started on this site, please read How to Ask and feel free to take a tour. Also, for any additional info, please visit help center Nov 11, 2014 at 8:24

2 Answers 2

2

From your comments I don't really know why you should not make use of custom post types and custom fields.

So what is you have extra flexibility from the above. IMHO, extra flexibility is always a plus when attempting something like this. The other plus points here are that you don't have to make use of extra functions to create these functionalities, neither would you need to create extra db fields (which I will not recommend you to do, I really don't see the need for extra db tables here)

I think the problem here is a lack of implementation. Here is a basic idea:

Your first step will be to register a custom post type. This will just be a basic custom post type which you can decide to exclude from search and having a separate single view.

The important part here would be to enable custom-fields in the supports argument

Here is a modified example from the codex

function codex_custom_init() {
    $args = array(
      'supports' => array( 'custom-fields' ),
      'label'    => 'Quotes'
    );
    register_post_type( 'quotes', $args );
}
add_action( 'init', 'codex_custom_init' );

Please see the link given above for more examples and information on registering custom post types

When you create a new quote, you can now create and set your custom fields accordigly. Name your custom fields according to the fields in your question. The values will be the information that you will enter that you will need to display. Also, have a good look at the link provided to learn how to use custom fields. You might also be interested in this post

When it comes to displaying, you will need to make use of a custom query and transients. We will make use of WP_Query for the custom query which will display the quotes and the Transient API which will rotate the quote on a daily basis. Again, go and read and play with examples from the links given.

Here is a concept query (modified from the codex, untested)

// Check for transient. If none, then execute WP_Query
if ( false === ( $quotes = get_transient( 'daily_quotes' ) ) ) {

      $quotes = new WP_Query(
   array(
'post_type' => 'quotes',
'posts_per_page' => 1,
'orderby' = 'rand'
   ));

// Put the results in a transient. Expire after 24 hours.
set_transient( 'daily_quotes', $quotes, 24 * HOUR_IN_SECONDS );
} 

// Run the loop as normal
if ( $quotes->have_posts() ) {

   while ( $quotes->have_posts() ) { 
   $quotes->the_post(); 
       $data = get_post_meta( $post->ID );

       echo 'Quote Text: '. $data['Quote_Text'][0] . '</br>';
       echo 'Quote Timestamp: ' . $data['Quote_Timestamp'][0] . '</br>';
       // Continue with the rest

   }
   wp_reset_postdata();
}
4
  • A quick question though, suppose I want to have fields for say Date on which the quote was said, and a web link to store the whole story. Will your way support those 2 fields. I was planning to have 2 columns for them say QUOTE_DATE, QUOTE_WEBLINK
    – rents
    Nov 11, 2014 at 13:07
  • I think the custom_fields will help me for this.
    – rents
    Nov 11, 2014 at 13:14
  • Yes, just extend my code. I have only added two fields as an example. As suggested, just extend on that in the same way. Nov 11, 2014 at 13:20
  • I found a plugin and it used a custom database table which was what I was looking for. I will customize its code as the license allows to do so.
    – rents
    Nov 20, 2014 at 20:36
-1

I found a plugin and it used a custom database table which was what I was looking for. I will customize its code as the license allows to do so

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.