问题描述:

I want to display my articles info in different ways, with on/off functionality, something like this:

1.title

2.text

3.date

or

1.title

2.readmore button

3.text

etc...

The script below can do this, but it's kinda long and complicated are there are other ways to do that ? Without using array.

$firstpositioninfo is info field to display. 1 - title, 2 text etc..

<div id="pos1">

<?php

if ( $firstpositioninfo = 1 ) {

echo 'Here will be title';

elseif ( $firstpositioninfo = 2 ) {

echo 'Here will be text';

elseif ( $firstpositioninfo = 3 ) {

echo 'Here will be readmore';

.....

else { }

?></div>

<div id="pos2">

<?php

if ( $firstpositioninfo = 1 ) {

echo 'Here will be title';

elseif ( $firstpositioninfo = 2 ) {

echo 'Here will be text';

elseif ( $firstpositioninfo = 3 ) {

echo 'Here will be readmore';

.....

else { }

?></div>

网友答案:

Yea, a lot of better solutions exist...

Try:

switch($firstpositioninfo):
case 1:
   for each post, display it properly
   break;
(...) 

Displaying each post should be also declared as function, check DRY principle.

相关阅读:
Top