开发者

How do I insert a PHP code within another PHP code?

I'm using WordPress. I appreciate being shown the code, but this is one I am interested in straight out learning how to do myself too - so if you know where I can find a tutorial or can give me information I'd appreciate it!

I'm calling posts and want to include a PHP code within a PHP code, this is for a theme options panel.

<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged); 
if (have_posts()) :
?>

Where the 51 is I want to put:

<?php echo get_option('to_postc_home'); ?>

Where the 3 is I want to put:

<?php echo get_option('to_p开发者_如何学Goosti_home'); ?>


If I'm interpreting right, this is what you need, use the concatenation operator . to use those functions in place of plain text ex: 'this is text' versus 'this '.get_option('stuff').' text'

<?php
    query_posts('cat='.get_option('to_postc_home').'&posts_per_page='.get_option('to_posti_home').'&paged='.$paged); 
    if (have_posts()) :
?>


To include a php file from another file you use the include function


You can use it whereever you want

<?php
query_posts('cat=-51&posts_per_page=3&paged='.$paged); 
if (have_posts()) :
?>
hello world
<?php echo get_option('to_postc_home'); 
endif;


<?php
  $a = get_option('to_postc_home');
  $b = get_option('to_posti_home');

  query_posts("cat={$a}&posts_per_page={$b}&paged={$paged}");

  if (have_posts())
?>


That is called string concatenation, and you are already using that on the first line of your code, when you concatenate the literal string 'cat=-51&posts_per_page=3&paged=' with the variable $paged. In PHP, the . operator does that.

So, in your code, you can do this:

<?php
query_posts('cat=-' . get_option('to_postc_home') . '&posts_per_page=' . get_option('to_posti_home') . '&paged='.$paged); 
?>

This will inject the output of the function calls at the places you indicated.


<?php
$cat = get_option('to_postc_home');
$per_page = get_option('to_posti_home');

query_posts("cat=${cat}&posts_per_page=${per_page}&paged=".$paged);
if (have_posts())
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜