[英]How do I create a while loop within a PHP string?
I am creating a query using shortcodes ultimate lightbox. 我正在使用短代码终极灯箱创建查询。 But the only way this will work within a regular php page, is to save the data as string.
但是这在常规php页面中工作的唯一方法是将数据保存为字符串。 So what I need to do is to create my query but somehow get my results within a string.
所以我需要做的是创建我的查询,但不知何故在字符串中得到我的结果。
Here is what works before I use any kind of php query: 在使用任何类型的php查询之前,这是有用的:
<?php
$my_tabs = "<ul class='easybuttons'>
<li>[su_lightbox type='inline' src='#lightbox1']AT&T[/su_lightbox]</li>
<li>[su_lightbox type='inline' src='#lightbox2']Sprint[/su_lightbox]</li>
<li>[su_lightbox type='inline' src='#lightbox3']T-Mobile[/su_lightbox]</li>
</ul>";
echo do_shortcode( $my_tabs );
?>
but I need the ATT, Sprint, T-Mobile to be dynamic. 但我需要ATT,Sprint,T-Mobile才能充满活力。 Keep in mind the shortcode will only work if it within a string.
请记住,短代码只有在字符串中才有效。
So how can I do a while loop within this string? 那我怎么能在这个字符串中做一个while循环呢? I tried using an operator but did not work.
我尝试使用运算符但没有工作。
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'the_title', 'order' => 'ASC');
query_posts($args);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox1"]' . get_the_title() . '</li>';
}
$my_tabs .= '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
echo do_shortcode( $my_tabs );
?>
UPDATE: 更新:
I tried using this code but it does work. 我尝试使用此代码但它确实有效。 Nothing comes through.
没有任何事情发生。 I don't get any errors but no shortcode is displayed.
我没有收到任何错误,但没有显示短代码。
<?php
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'the_title', 'order' => 'ASC');
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$lid = 1;
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
$lid++;
}
$my_tabs .= '</ul>';
}
echo do_shortcode( $my_tabs );
wp_reset_postdata();
You need to initialise the variable $my_tabs
somewhere, for instance outside the if block, and increment the lightbox id. 您需要在某处初始化变量
$my_tabs
,例如在if块之外,并增加灯箱ID。 You don't need to call query_posts()
. 您不需要调用
query_posts()
。 order_by
should be title
, not the_title
. order_by
应该是title
,而不是the_title
。 Make sure $childid
is definitely a string of the category slug , not the name, if in doubt, take out that parameter altogether to see if you get anything as I imagine this is most likely to be the main issue. 确保
$childid
绝对是类别slug的字符串 , 而不是名称,如果有疑问,请完全取出该参数,看看你是否得到任何东西,因为我认为这很可能是主要问题。
$args = array('post_type' => 'services', 'category_name' => $childid, 'order_by' => 'title', 'order' => 'ASC');
// The Query
$the_query = new WP_Query( $args );
$my_tabs = '';
// The Loop
if ( $the_query->have_posts() ) {
$lid = 1;
$my_tabs .= '<ul class="easybuttons">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
$lid++;
}
$my_tabs .= '</ul>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.