Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In the functions.php, I have the following code added:

add_post_type_support( 'page', 'excerpt' );

This allows pages to have excerpts. The reason I want an excerpt is because I want to call only pages with excerpts to be one of 3 modules on the home page.

On the front page, in my home.php file, these 3 modules will show the titles and excerpts of the pages in which I have added an excerpt. At anytime, the editor should be allowed to change the placement or page so that any existing page could theoretically become a module on the home page.

For example, I have an About, New Patients and Services page, all three of which I have added text to the excerpt field (I will leave all other excerpt fields blank on pages which don't need to appear on the front page). On home.php, in the order I just listed, I want the title and excerpt of each page to show up as three columns.

Here's how I intend to style it: http://i.stack.imgur.com/jOPb0.png

It's possible these 3 modules will either need an order change or replaced by another page, which is why I'm looking for dynamic modules.

I have created two custom fields, called home_widget and order. home_widget is equal to either true, whereas order is equal to the order they should appear on the page.

This code is wrong, but I'm looking for similar logic:

<?php
  $pageExcerpt = get_the_excerpt(); 
  if (is_page() && $pageExcerpt !== '') {
    the_excerpt();
  }
?>

In other words, if a page exists AND it has an excerpt, then call the_excerpt() but do it in the order I define by custom field entry. This allows for dynamic insertion as needed.

How do I write this code to work as I stated?

share|improve this question
add comment

3 Answers

Unless you have a particular reason to have meta box order values , that would be overkill.

Instead ditch the excerpt for pages, and add a meta box called "intro_content" (or whatever).

Then in a home.php query you can easily grab this info and order it however you want using html.

For instance:

// replace 1, 2, 3, with actual page ID's
$about_meta = get_post_meta( 1, 'intro_content');
$new_patients_meta = get_post_meta( 2, 'intro_content'); 
$service_meta = get_post_meta( 3, 'intro_content'); 

// div styles and echo the title + the above or whatever

http://codex.wordpress.org/Function_Reference/get_post_meta

Added this after your comment/edit.

To order the metabox you can add another one with a numerical value, or even better add a dropdown box ( for example called 'order_value_dropdown').

That way in your query you can query the order by value of the dropdown. For example :

 $order_meta = get_post_meta( 3, 'order_value_dropdown'); 
 if ($order_meta = 1 )
 //grab the intro_content meta from that post
 if ($order_meta = 2 )
 //grab the intro_content meta from that post
 // etc, etc
share|improve this answer
    
@Wyck Thank you for your reply! I think I wasn't completely clear in my initial question so I've updated it to reflect more closely what I'm looking for. Please let me know if you understand better what I should do, thanks! –  micah Jun 8 '11 at 2:09
    
I updated answer, should still use the same technique. Read this codex.wordpress.org/Custom_Fields –  Wyck Jun 8 '11 at 3:58
    
@Wyck Oh wow, when you talk about meta boxes, are you referring to this? codex.wordpress.org/Function_Reference/add_meta_box That's very overywhelming! I'm a front-end dev and this is putting me uncomfortably into the back-end world. haha I understand the concept but this is pretty intimidating. Will this whole thing take a ton of code? –  micah Jun 8 '11 at 4:19
    
Not really but you will need to know some basic php, you said you already added custom fields called order and home_widget, meta box is the same thing. You just need to get the values from them and do something with it which you were trying in the above code but this is a better way. I'm just pointing you in the right direction. Adding custom meta boxes, outside default ones you find in a post edit screen is actually kinda complex, but there are plenty of plugins for this. –  Wyck Jun 8 '11 at 4:24
    
@Wyck so would I need to create a custom loop inside the normal loop, query_posts(), then add your code? This is where I'm a little sketchy. I'm sorry if I need my hand held a little, I'm still a newb with PHP. I'm also not sure what you mean by "add a dropdown box" other than to do this in the functions.php file. –  micah Jun 8 '11 at 4:27
show 4 more comments
up vote 0 down vote accepted

Initially, Wyck told me to study the WP_Query article on WordPress's site. This was very overwhelming to try and pick up, and I searched the web for articles that might give me a quicker answer, but I couldn't find anything that was relevant.

This is similar but different than Wyck's answer, but I only figured it out by studying a new article just released on Smashing Magazine on How to Build a Media Site on WordPress. I would still prefer to query whether a page contains an excerpt or not, and only show that page, but this is a good solution in the interim.

<?php
    // Get the pages with excerpts
  $excerpt = new WP_Query(
    array(
        'post_type' => 'page', // Tell WordPress which post type we want
        'posts_per_page' => '3', // Show the first 3
        'orderby' => 'meta_value', // Order by meta_key, defined on next line
        'meta_key' => 'order', // Look for numerical value in this key
        'order' => 'ASC', // Ascending order
        'meta_query' => array( // Return only pages with home_widget meta key set to true
            array(
                'key' => 'home_widget',
                'value' => 'true',
                )
            )
        )
    );
?>

<ul class="excerpt">
    <?php //Start new loop looking for pages only with $excerpt from above 
          while ( $excerpt->have_posts() ) : $excerpt->the_post(); ?>
    <li class="module">
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p><?php the_excerpt(); ?></p>
    </li>
    <?php endwhile; ?>
</ul>
share|improve this answer
add comment

One problem:

"On the front page, in my home.php file".

The home.php template file is not the Front Page template; rather, it is the Blog Posts Index template. The Front Page template is front-page.php.

You should never see both is_home() and is_page() return true at the same time.

Thus, you should be using front-page.php as your Front Page template file, and you should be using is_front_page() as your conditional.

share|improve this answer
    
Interesting, I've never read anywhere about what you said. In fact, I referred to the example by the guys at DigWP. The difference between home.php and front-page.php is not very clearly explained in the Codex, either, and actually says the opposite of what you said. But, I really appreciate your feedback! –  micah Jun 9 '11 at 2:50
    
Actually, what I said contradicts neither DigWP (who strangely appear not even to mention Front Page/front-page.php), nor the Codex (which clearly indicates that front-page.php takes precedence for the Front Page). You can choose to believe me or not, but I promise: the home.php template file, and the is_home() conditional, apply to the Blog Posts Index, whether displayed on the Front Page, or on a static Page. –  Chip Bennett Jun 9 '11 at 11:44
    
Of course you absolutely know more than I do about this! :) On the Codex, front-page.php is your latest posts and you said home.php is blog posts index template, and these two statements could be one in the same by some logic. I wish the Codex, on the page I linked above, was clearer to explain when to use home.php versus front-page.php! I've always used home.php in the manner I've described not knowing any better and never having any issues turning it into a static front page. Anyway, thank you for pointing this out since you clearly know waaaaaay more than me! :) –  micah Jun 9 '11 at 18:26
    
@Micah: it can certainly be confusing, due to the unfortunate use of "home" as the name for the Blog Posts Index. This use pre-dates the consideration of using a static Page as the Front Page, so it's somewhat understandable. So long as the Front Page displays the Blog Posts Index, then "Home" and "Front Page" are the same. The issue arises when the Front Page is set to display a static Page, and the Blog Posts Index is moved to a different static Page (which is, I'm assuming, how you have your current setup). –  Chip Bennett Jun 9 '11 at 18:55
    
More or less, your description of my scenario is correct, although this project doesn't actually require a blog yet (so no need for blog posts index), just modular and dynamic content via pages and custom post types. Usually, if I am using home.php and I need a blog posts index, I'll just go into Settings > Reading and set the blog page there (without setting the front page since home.php would be filling that role). Thanks again for the clarification. –  micah Jun 9 '11 at 21:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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