0

in my home page I'd like to display some pages and posts title using the custom fields.

Unfortunately my code prints only posts title.

If there is a key "highlights" print the title.

Here my code:

<?php while (have_posts()) : the_post(); ?>

    <?php if ( get_post_meta($post->ID, 'highlights', true)) { ?>

        <?php the_title() . "<br>"; ?>

    <?php } ?>

<?php endwhile; ?>

How can I print both? Posts and pages title?

Thank you

7
  • Not sure exactly what you're asking, are you using a static page for your home page? In that case I think you should take a look at get_posts (codex.wordpress.org/Template_Tags/get_posts). With get_posts you can list posts from a certain category or post_type on your home page.
    – Richard B
    Commented Nov 14, 2012 at 12:00
  • I'd like to print the title of the pages and the posts. In my code, I print only the posts title, not pages.
    – Ale
    Commented Nov 14, 2012 at 12:45
  • Where do you want to print the page titles? Alongside the post titles or in a separate area like a sidebar?
    – Richard B
    Commented Nov 14, 2012 at 12:52
  • Are you referring to your blog posts index page, or your site front page specifically? Are you using a custom query, or the main query? Commented Nov 14, 2012 at 12:55
  • I'd like to print the titles in the home page but not in a sidebar. Is it a problem? Do I have use a sidebar?
    – Ale
    Commented Nov 14, 2012 at 12:56

1 Answer 1

0

You're not being very specific so it's hard to give you any help. But if you would like to output both pages and posts in the same loop you need to customize your query so that it includes both posts and pages. Learn about http://codex.wordpress.org/Class_Reference/WP_Query.

Or if you want the posts to be outputted at one place and the pages in another place, http://codex.wordpress.org/Function_Reference/get_pages or maybe http://codex.wordpress.org/Function_Reference/wp_list_pages.

Update

To mix posts and pages in one loop you need to query both post types. Try adding query_posts like this:

<?php query_posts('post_type=post,page'); ?>
<?php while (have_posts()) : the_post(); ?>

    <?php if ( get_post_meta($post->ID, 'highlights', true)) { ?>

        <?php the_title() . "<br>"; ?>

    <?php } ?>

<?php endwhile; ?>
4
  • Yes, I'd like to output both pages and posts title in the same loop in my custom home page (index.php). With the code I write, my loop only outputs posts title. Sorry for my bad english.
    – Ale
    Commented Nov 14, 2012 at 13:32
  • I've updated my answer, try query_posts.
    – Richard B
    Commented Nov 14, 2012 at 14:59
  • Your code doesn't works but the idea is good, so i changed my code. Here is it: $args = array( 'post_type' => array('post','page'), 'posts_per_page' => 5);
    – Ale
    Commented Nov 14, 2012 at 15:52
  • Sweet. Please mark this as solved if you're happy with the answer.
    – Richard B
    Commented Nov 16, 2012 at 9:34

Your Answer

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

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