WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom fields for index.php? (8 posts)

  1. keviokevio
    Member
    Posted 4 years ago #

    Is it possible to have a custom field for index.php?

    Each of my pages have a little description in the sidebar, and it changes according to what I put in the "custom fields". However, since index.php (or the homepage) doesn't fall into the "Pages" category and can't be edited as one, how can I still give it a custom field, or at least have it do something with a similar effect.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    You could edit index.php and add your custom text directly to the template file. Or you could create a widget-capable area within index.php and add your custom content via a Text widget.

    http://codex.wordpress.org/Widgetizing_Themes

  3. keviokevio
    Member
    Posted 4 years ago #

    Is there a way to do it using php?

    Sorry I didn't make myself clearer, the text would be on sidebar.php, a page separate from index.php.

    I have almost no coding experience in php (heheh), but I was looking for something like:

    if (index.php is displayed)
    show "blahblahblah"
    if else
    show nothing

    Can anyone translate that into something usable?

    Thanks!

  4. syncbox
    Member
    Posted 4 years ago #

    If you are specifically meaning for a homepage -- then you could use:

    if (is_front_page()) {
    echo 'wahtever you want here'; }
    else {}

    I use that all the time. It is not required that your "front page" use a page.php or index.php template file... put it in both places if you are unsure which is being used... otherwise, if you are specifying your home page to be one showing your categories, then put it into the index.php file

    HTH

    D. Casey

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    <?php if( is_home() || is_front_page() {
    echo '<p>blahblahblah</p>';
    }
  6. syncbox
    Member
    Posted 4 years ago #

    BTW, it *can* have associated custom fields... (your homepage) if you create it as a Page.

    Page templates can display one or more categories of posts...it can also have multiple loops. There's nothing different about page.php or index.php... the defaults simply include programming to access the most commonly used elements, like "comments" in your index.php file because comments are associated with posts.

    But you can edit either or both or create custom files to use.

    For example, make you home content a Page (home) and add the intro text there or use whatever custom fields you need. Then, to get that content, use the standard loop.

    To get your category content, add a custom query with categories desired and use another loop, eg:

    <?php $my_query = new WP_Query('cat=14&showposts=1'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <p>Read the rest of "<?php the_title(); ?>" in <?php the_category(' &raquo; '); ?></p>
    <p class="postmetadata"><?php edit_post_link('Admin Edit', '', ''); ?></p>
    <?php endwhile; ?>

    The key to this method is

    A) the main content loop comes first -- but doesn't not finish the loop with the standard <?php endwhile; endif; ?> --- just the endwhile; statement.

    Note that the custom query also uses an endwhile; statement.

    B) after you finish all loops, close out with the endif; statement.

    C) then point your homepage setting to the static home "Page"

    D) the custom query can use more than one category; just list them separated by commas, per the usual and indicate the number of posts to display, etc.

    HTH

  7. keviokevio
    Member
    Posted 4 years ago #

    Hey it worked! (Well syncbox's method anyway.) Thanks a lot both of you!

  8. syncbox
    Member
    Posted 4 years ago #

    Glad to have helped. I use WordPress as a CMS for about 95% of what I do... in the years I've been using it, I have realized that there is often more than one way to get the job done.

    Note that by using Pages, you can easily point to Page templates... so you can easily customize what displays on various sections of your site by altering the page.php file to be something like page-home.php or page-products.php etc if you need an even wider variation of conditionals.

    As well, you can add the (is_tree()) function to your functions.php file and you can conditionally add programming or markup for a template used by ALL of the sub-pages (immediate children... doesn't apply to "grand-child" pages) to avoid having to write arrays or tons of (is_page()) statements.

    Do a search for (is_tree()) in the documentation to find out how to do that.

    Here's to a great week!

Topic Closed

This topic has been closed to new replies.

About this Topic