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.

If possible I want to insert the ID of a post(from a custom post type) into the html of the post itself when its loaded in the viewport.

The scenario is that the content of the custom post type is surrounded by a <div>. I want to append an 'id' to the <div> that includes the ID of the post. For example if the custom post type is glossary and the post ID is 351 then the resulting html would be:

<div id="glossary351"> Post content </div>

I looked at the conditional statements and googled on the subject but found that

  1. A lot of applications of finding the Post ID were to do with showing the ID on the page, as opposed to inserting it into the html.
  2. My almost non existent PHP skills were not up to fathoming out how to use the conditionals to get the result I need.

I'd appreciate any pointers regarding:

  • Is this possible
  • Any examples of how to approach the problem

Thanks

share|improve this question

3 Answers 3

up vote -1 down vote accepted

Try:

<div id="<?php the_title(); ?>-<?php the_ID(); ?>">

This should give you a result of:

<div id="Post Title-PostID">

Giving you a unique wrapper for each post.

share|improve this answer
    
@Tafts Thanks that solved it. –  dorich Feb 18 '11 at 7:31
    
I'll note for other readers who might be as ignorant as I am that one of the problems I had with this was figuring out how to identify and then edit where the loop was working - in other words which template to look in. My conclusion, after some research was the WP will default to the single.php template unless you create a custom template. In the example I used this template has to be named 'single-glossary.php'. WP then detects the custom template and uses it for the single post display. –  dorich Feb 18 '11 at 7:44
    
@dorich see Template Hierarchy for detailed explanation of which templates are used under different conditions. –  Rarst Feb 18 '11 at 17:27
    
@Rarst Thanks - that clarified the template question nicely. –  dorich Feb 18 '11 at 20:55
    
I wouldn't recommend using the_title() as an ID attribute of an html element. It isn't properly escaped nor is it valid to have spaces in the id attribute. –  prettyboymp Feb 19 '11 at 3:16

See http://codex.wordpress.org/Function_Reference/get_the_ID :

Function Reference/get the ID

Description

Returns the numeric ID of the current post. This tag must be within The Loop.

Usage

<?php get_the_ID(); ?> 

Parameters

This tag has no parameters.

Examples

Store the ID

The ID can be stored as a variable using

    $postid = get_the_ID();

Post Anchor Identifier

get_the_ID() Can be used to provide a unique anchor in a script. For instance, a dynamically-generated drop down menu with actions for each post in an archive could have

 $id = get_the_ID();
 $dropdown = "<select name='dropdown-".$id."' >";
 $dropdown .= "<option id='option1-". $id ."'>"Option 1</option>";
 $dropdown .= "</select>";

This would allow us to use JavaScript to control the element as it has a unique ID, and when submitting it as a form through the POST or GET methods the dropdown box will be sent with a unique ID which allows the script to note which post it is working on. Alternatively a hidden variable could be sent which will allow the script to see which post the submission is referring to

 echo '<input type="hidden" name="activepost" id="activepost" value="'.get_the_ID().'" />';

If the ID is not called within PHP, then we can use the_ID rather than echo get_the_ID();

share|improve this answer
    
Thanks for the response. I did look at this in the codex but I wasn't smart enough to see how to apply it, see my other comments for clarification. –  dorich Feb 18 '11 at 7:46

I would also suggest using WordPress' built in post_class() function to add classes to the div as well, so the resulting code would be:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
share|improve this answer
    
Thanks, I hadn't gone as far as figuring out I could add more than just the ID. Very helpful. –  dorich Feb 18 '11 at 7:33

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.