Hola,
He hecho un template de página que permite mostrar una lista de posts de una categoría determinada y que muestra, al lado, otra categoría a la que pertenecen, es decir que, en mi caso concreto, por ejemplo, recoje todos los títulos de los posts de la categoría "adjetivos" y muestra si pertenecen a la categoría "nivel básico", "nivel intermedio, "nivel avanzado".
El caso es que como mis capacidades de programación son extremadamente básicas, se me plantean dos dudas:
1- El código que he hecho, aunque funciona perfectamente, me parece muy largo y reiterativo ¿Alguien puede decirme si existe un modo más simple de llegar a lo mismo?
2- Tal y como lo he hecho, debo crear una plantilla de página para cada categoría que quiero mostrar, es decir, una plantilla para "adjetivos", otra para "verbos", etc. ¿No es posible hacer lo mismo desde una única plantilla?
He aqui el codigo:
< ?php
/*
Template Name: page-adjetivos
*/
? >
< ?php get_header
(); ? >
< !-- google_ad_section_start -- >
< div
class=
"post page" id=
"post-< ?php the_ID(); ? >" >
< ?php
include (TEMPLATEPATH .
"/post-head.php"); ? >
< div
class=
"paginaIndex" >
< div < span
class=
"paginaIndex-subheader" >< h2 >Adjetivos Descriptivos< /h2 >< /span >< /div >
< ?php
global $post;
$args =
array( 'category' = >
1628,
'numberposts' = > -
1,
'exclude' = >
'1133,891,1190');
$myposts = get_posts
( $args );
foreach( $myposts as $post ) : setup_postdata
($post); ? >
< div >
< ul < p >< span
class=
"paginaIndex-centre" ><a>
" >< ?php the_title(); ? ></a>< /span >< span id="paginaIndex-right
" >< a < ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='3'))
echo '<a>category_nicename . '/" >
'. $cat- >cat_name . '</a>
' . ' ';
} ? >
< ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='4
'))
echo '<a>category_nicename .
'/" >'.
$cat- >cat_name .
'</a>' .
' ';
} ? >
< ?php
foreach((get_the_category
()) as $cat) {
if (($cat- >cat_ID==
'5'))
echo '<a>category_nicename . '/
" >'. $cat- >cat_name . '</a>' . ' ';
} ? >
< ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='6'))
echo '<a>category_nicename . '/" >
'. $cat- >cat_name . '</a>
' . ' ';
} ? >< /span >< /p >< /div >
< ?php endforeach; ? >
< div class="paginaIndex-subheader" >< h2 >El Comparativo y el Superlativo de los Adjetivos Descriptivos< /h2 >< /div >
< ?php
global $post;
$args = array( 'category
' = > 1631,1632, 'numberposts
' = > -1);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ? >
< div >
< ul < p >< span class="paginaIndex-centre" ><a>" >< ?php the_title(); ? ></a>< /span >< span id="paginaIndex-right" >< a < ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='3
'))
echo '<a>category_nicename .
'/" >'.
$cat- >cat_name .
'</a>' .
' ';
} ? >
< ?php
foreach((get_the_category
()) as $cat) {
if (($cat- >cat_ID==
'4'))
echo '<a>category_nicename . '/
" >'. $cat- >cat_name . '</a>' . ' ';
} ? >
< ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='5'))
echo '<a>category_nicename . '/" >
'. $cat- >cat_name . '</a>
' . ' ';
} ? >
< ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='6
'))
echo '<a>category_nicename .
'/" >'.
$cat- >cat_name .
'</a>' .
' ';
} ? >< /span >< /p >< /div >
< ?php endforeach; ? >
< div
class=
"paginaIndex-subheader" >< h2 >Adjetivos Posesivos< /h2 >< /div >
< ?php
global $post;
$args =
array( 'category' = >
1629,
'numberposts' = > -
1);
$myposts = get_posts
( $args );
foreach( $myposts as $post ) : setup_postdata
($post); ? >
< div >
< ul < p >< span
class=
"paginaIndex-centre" ><a>
" >< ?php the_title(); ? ></a>< /span >< span id="paginaIndex-right
" >< a < ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='3'))
echo '<a>category_nicename . '/" >
'. $cat- >cat_name . '</a>
' . ' ';
} ? >
< ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='4
'))
echo '<a>category_nicename .
'/" >'.
$cat- >cat_name .
'</a>' .
' ';
} ? >
< ?php
foreach((get_the_category
()) as $cat) {
if (($cat- >cat_ID==
'5'))
echo '<a>category_nicename . '/
" >'. $cat- >cat_name . '</a>' . ' ';
} ? >
< ?php foreach((get_the_category()) as $cat) {
if (($cat- >cat_ID=='6'))
echo '<a>category_nicename . '/" >
'. $cat- >cat_name . '</a>
' . ' ';
} ? >< /span >< /p >< /div >
< ?php endforeach; ? >
< /div >
< /div >
< !-- google_ad_section_end -- >
< div class="clear" > < /div >
< ?php get_footer(); ? >
Como se puede ver el código llama a una categoría determinada, por un lado, y luego dice que categorías debe mostar como asociadas al lado descartando las demas categorías a las que también pertenece el post (si un post pertenece a la categoría, "adjetivos descriptivos", "superlativos", "nivel básico" solo mostrará la categoría "nivel básico")
En cualquier caso, el código, como he dicho, funciona, así que tal vez alguien quiera aprovecharlo.
Gracias