PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How do I add an echo inside a query?

 July 08, 2022     custom-fields, php, posts, wordpress     No comments   

Issue

I'm trying to show posts from a certain category on a page like his:

<?php query_posts('category_name=category1'); ?>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

This works. However, I'd like 'category1' to be taken from a custom field of the page. Something like this (incorrect code incoming):

<?php query_posts('category_name=get_post_meta(get_the_ID(), 'custom_cat_name', TRUE); ?>'); ?>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

How could I make this possible?


Solution

Use it in this way:

$custom_cat_name = get_post_meta(get_the_ID(), 'custom_cat_name', TRUE);
query_posts('category_name=' . $custom_cat_name);

Hope this helps!



Answered By - Raunak Gupta
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing