• Resolved mireck

    (@mireck)


    Hi
    My theme supports different post types – one of those is portfolio posts. I’m trying to display portfolio posts on a page but only from one specific category – the one that has the same name as the current page.

    Exaple:
    I have portfolio posts in categories: red, green, blue.
    I have pages named: red, green, blue.
    Now i would like to display on page “red” only post from category “red”.

    I’m very new to wordpress and making websites in general.

    I tried to read a bit about it and found a way to display posts from certain category only:
    <?php $the_query = new WP_Query( 'portfolio_category=red' );?>

    From there I tried to do this but it doesn’t work:
    <?php $the_query = new WP_Query( 'portfolio_category=$pagename' );?>

    Here is full code of the page template I’m talking about:
    https://pastebin.com/LxetTnUz

    Thank you in advance for help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You cant write variables like that in a string.
    Try this:
    <?php $the_query = new WP_Query( ‘portfolio_category=’ . $pagename );?>

    Thread Starter mireck

    (@mireck)

    This seems to work perfectly as I wanted! THANK YOU veeerrry much! It saves alot of my time.
    All the best!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying posts only form a category that has same name as the current page’ is closed to new replies.