• surfitin10

    (@surfitin10)


    Hello, I am interested in using a single page template for all pages in my custom theme (page.php). Because a random number is assigned for each page, it is difficult to style the individual page titles given that the_ID() will vary for each page and might change when the theme is loaded to another wordpress installation:

    <div class="post" id="post-<?php the_ID(); ?>">
              <h2><?php the_title(); ?></h2>

    I believe what is needed is a more reliable CSS selector in order to style the title for an individual page that is rendered by the page.php template. I am relatively new to wordpress, please let me know if I am just thinking too static here 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • ambrosite

    (@ambrosite)

    You can target individual pages in CSS using the body class. But let me ask you: if the IDs might change when the theme is loaded to another WordPress installation, wouldn’t the page titles change as well?

    Thread Starter surfitin10

    (@surfitin10)

    Yes that’s true; however, the body tag is included in my header. I could create 2 headers and put the body tag into page.php perhaps.

    I’m not sure about how the post ID to title mapping works on the backend. When I move wordpress to a new server (i.e. load my dev work to a production web host), will all of the post IDs be exactly the same in the new installation? I intended this question to be more of a general ‘how would you do this’ inquiry as I am really unfamiliar with what to expect when I move my dev work to a live server.

    Thread Starter surfitin10

    (@surfitin10)

    ambrosite

    (@ambrosite)

    There shouldn’t be any need to create two headers. Your post IDs will be preserved from one server to another as long as you use one of the procedures explained here to copy your database:

    http://codex.wordpress.org/Backing_Up_Your_Database

    If the page title is fixed, regardless of the page id then you can implement http://codex.wordpress.org/Template_Tags/body_class

    Add extra filter to body_class() to output the page title or page slug.

    Thread Starter surfitin10

    (@surfitin10)

    Thanks to both of you!

    So I can probably use the postID, but I need to clean up the db first as it has a ton of sample posts/comments that I want to remove and not take to the live site. This data was added simply for development testing. Are you aware of a fast way to remove data like comments/posts without removing theme/wp settings from the db?

    Otherwise, the body_class function might work. So basically, I would call this php in my custom contact page for example:

    <?php body_class( $custom_contact ); ?>

    Then that updates the body tag to:

    <body class="custom_content">

    From there, I can create additional CSS for the custom_content class that will only be applied to that page. Essentially, this would allow me to create any number of custom classes for each page dynamically. Please correct me if I misunderstood.

    ambrosite

    (@ambrosite)

    When you say you want to “clean up the db,” do you mean you want to remove ALL of the posts/comments and start over with a blank slate? If so, you could do that by running these SQL queries on your WP database:

    DELETE FROM wp_posts;
    DELETE FROM wp_postmeta;
    DELETE FROM wp_comments;
    DELETE FROM wp_commentmeta;
    DELETE FROM wp_term_relationships WHERE object_id NOT IN (SELECT link_id FROM wp_links);

    That will remove every post, page, revision, comment, and post-to-category assignment (but not the categories themselves) from your db.

    Warning: these are extremely destructive queries. They are basically wiping out the entire contents of those tables in a non-recoverable way. Make a backup of your database first.

    ambrosite

    (@ambrosite)

    For the second question: the body_class template tag is applied in the header.php; you would need to write a function that retrieves the page name from the db and adds it to the body class via a filter. I recently wrote a plugin that does just that; there are also some examples that may help you:

    http://www.ambrosite.com/plugins/body-class-enhanced-for-wordpress

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Reference page id/title by name’ is closed to new replies.