sledgeweight
sledgeweight

Reputation: 8105

Print PHP values or words instead of its corresponding incrementing numerical ID's

$page_id++ represents page order ID from 0 to 5 inclusive in a wordpress website and increments.

I then include some HTML like so:

<div class="page_title <?php echo page_item;?>" id="about"><?php the_title()?></div>

the output for the div class is "page_title 0" for the first one.

What I'm wanting to know is if i could replace the number with a word or a class. I've tried creating an array:

<?php $page_id = array (0 === 'one', 1 === 'two',2 === 'three',3 ==='four',4 ==='five', 5=== 'six')?>

but it just returns: "page_title Array"

EDIT: I've also tried replacing === with => in the Array.

I know I need to read up on my syntax (or even logic!) here but any help/ explainations or clues even would be appreciated as always.

Upvotes: 0

Views: 61

Answers (3)

flynn
flynn

Reputation: 1584

It looks like you're not actually refer to the items in the array, just the array itself. Are you using a foreach loop to print these out?

<?php foreach($page_id as $page_item):?>

   <div class="page_title <?php echo $page_item;?>" id="about"><?php the_title()?></div>

<?php endforeach;?>

Or alternatively do it manually:

 <div class="page_title <?php echo $page_id[0];?>" id="about"><?php the_title()?></div>

So you would do that, referencing keys 0 - 5.

Upvotes: 1

cHao
cHao

Reputation: 86535

You can, and in fact should, be using something besides numbers as class names. I'm about 78% certain that a class name should contain at least one non-digit character, and should start with a letter. (That's actually the rule for IDs, it seems. I'm not finding a hard-set rule for classes, but eh.)

As for how to map numbers to words, you should be looking up the id in your array rather than echoing the whole thing.

<?php $page_classes = array(...); ?>
...
<div class="page_title <?= $page_classes[$page_id] ?>" id="about">
  <?php the_title()?>
</div>

(Note, if you're doing this in a situation where you expect more than one such div to be printed, you should seriously consider losing the id attribute or making it equal to the class name you're printing out or...something. IDs should be unique.)

Upvotes: 0

Seer
Seer

Reputation: 5237

Try something more like:

<?php

    $pageIDs = array(0 => 'one', 1 => 'two'); // and so on... 
    $page_id = $pageIds[$page_id ];

    // ... Other wordpressy stuff...

Essentially, the idea being you grab the value from an array, rather than passing an array as the variable, when you get $page_id in this case, it'll be the string.

Upvotes: 0

Related Questions