Pergunta

i'm trying to display only the first 10 categories (categories not posts). my code currently displays all categories. can someone tell me how to modify this so it limits it to the only the first 10? also how to get the next ten after that?

<ul>
    <?php
    $job_categories = wpjb_form_get_categories();
    foreach ($job_categories as $cat) :
    ?>
    <li><a href="/jobs/find/?query=&category=<?php echo $cat['value']; ?>"><?php selected($cat['value'], $param["category"]); ?><?php echo $cat['description']; ?></a></li>
    <?php endforeach; ?>
</ul>
Foi útil?

Solução

Have a look at http://codex.wordpress.org/Function_Reference/get_categories and http://codex.wordpress.org/Template_Tags/wp_list_categories

You can define the number of categories to get, also note that wpjb_form_get_categories isn't defined by wordpress, so you may have to modify that function to add this capability.

<ul>
    <?php
    $args = array(
       'orderby' => 'name',
       'number' => 10
    );
    $job_categories = get_categories( $args );
    foreach ($job_categories as $cat) :
    ?>
    <li><a href="/jobs/find/?query=&category=<?php echo $cat['value']; ?>"><?php selected($cat['value'], $param["category"]); ?><?php echo $cat['description']; ?></a></li>
    <?php endforeach; ?>
</ul>

Outras dicas

PHP's array_chunk() will split your array into chunks/pages:

<ul>
    <?php
    $page = 1; 
    $job_categories = wpjb_form_get_categories();
    $chunks = array_chunk($job_categories, 10);
    ?>
    <?php if (isset($chunks[$page-1])): ?>
        <?php foreach ($chunks[$page-1] as $cat): ?>
            <li><a href="/jobs/find/?query=&category=<?php echo $cat['value']; ?>"><?php selected($cat['value'], $param["category"]); ?><?php echo $cat['description']; ?></a></li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>

There may be some arguments you can pass to wpjb_form_get_categories() to get only the categories you need but I'm not very familiar with wordpress. It looks like there's only a way to get a certain number of categories. This would work for the first 10 but not the next 10.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top