Support » Themes and Templates » 'cat=$function' in order to get posts from a category (loop)

  • Resolved HoneyDaPooh


    Hi there,

    I am currently developing a theme for a client but I am stuck with something I never encountered before.

    The mechanic of the website is that you will get different category articles displayed as a function of 2 things : the “year” you have selected and the current page. I have used global functions (storing it in the URL) in order to retrieve the year and I am simply defining in each custom page the current page with a function. At the top of each of my custom page I have therefore defined :

    <?php $pagecourante="accueil";

    $fonciere is the year (from 2013 to 2018) and $pagecourante is the french for currentpage.

    In order to select the category I want for each page, I have made a php table that looks like that (put in function.php), first [] is the year and the second one is the current page. The number at the end is the category id. It just means that if you have selected $fonciere=2013 and $pagecourante=accueil, then I want $conv to return 10, wich is the category of articles I want to display under such circumstances.

    // -
    // -
    // -

    Now, I would like to be able to do get the loop displayed and define each category by simply putting 'cat=$conv[$fonciere][$currentpage]'wich would return 10 if you are in year 2013 and the current page is accueil for instance.

    However, this is not working, I can’t seem to be able to call a category with a function, that’s why I am looking for your help dear fellas. Is it my syntax that is wrong or the whole reasoning?

    I thank you in advance for your help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Please could you provide the complete line of code in which 'cat=$conv[$fonciere][$currentpage]' appears. It will help to solve the problem.

    Meanwhile I am guessing that this is in a WP_Query? If so then your call to your array will be treated as a string literal as you have written it above. i.e. WordPress will be looking for cat=$conv[$fonciere][$currentpage].

    Try changing it to 'cat='.$conv[$fonciere][$currentpage] instead and see if that helps. (note the position of the single quotes and the full stop).

    Than you very much TheHandofCod, you have guessed right, it works. Here is the full code where I want to call $conv for the sake of helping future readers:

    <?php query_posts('cat='.$conv[$fonciere][$pagecourante]); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       <?php the_content(); ?>
    <?php endwhile; endif; ?>

    That is working like a charm, thanks again TheHandofCod.


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘'cat=$function' in order to get posts from a category (loop)’ is closed to new replies.