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

  • Resolved HoneyDaPooh

    (@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";
    $_GET['fonc'];
    $fonciere=$_GET['fonc'];
    ?>

    $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.

    $conv[2013][accueil]=10;
    $conv[2013][compta]=11;
    $conv[2013][juridique]=3;
    $conv[2013][lots]=5;
    $conv[2013][tdb]=4;
    // -
    $conv[2014][accueil]=12;
    $conv[2014][compta]=13;
    $conv[2014][juridique]=9;
    $conv[2014][lots]=14;
    $conv[2014][tdb]=5;
    // -
    $conv[2015][accueil]=17;
    $conv[2015][compta]=18;
    $conv[2015][juridique]=19;
    $conv[2015][lots]=20;
    $conv[2015][tdb]=21;
    // -
    $conv[2016][accueil]=23;
    $conv[2016][compta]=24;
    $conv[2016][juridique]=25;
    $conv[2016][lots]=26;
    $conv[2016][tdb]=27;
    ?>

    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.

    pleasure

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.