2016-12-24 46 views
0

我有這樣的代碼,以顯示他們的第一個崗位類別和縮略圖的所有職務。如何使WordPress的簡碼

<?php $recent = new WP_Query(); ?> 
<?php $recent->query('cat=1&showposts=5'); ?> 
<?php $is_first_post = true; ?> 
<?php while($recent->have_posts()) : $recent->the_post(); ?> 
    <ul> 
     <li> 
     <?php 
     if ($is_first_post && has_post_thumbnail()) { 
     the_post_thumbnail(); 
     $is_first_post = false; 
     } 
     ?> 
     <a href="<?php the_permalink(); ?>"> 
     <?php the_title(); ?> 
     </a> 
     </li> 
    </ul> 
<?php endwhile; ?> 

,但我想這顯示使用shortcode.which使用&類職位數 ,但我不能讓簡碼

回答

0

一個簡碼是一個PHP函數。你需要一個接受你所有參數的函數。對於示例 -

function get_posts($atts) { 
    extract(shortcode_atts(array(
    'cat_id' => 'cat_id', 
    'num_posts' => 'num_posts' 
), $atts)); 

    $loop = array(
    'cat' => $cat_id, 
    'posts_per_page' => $num_posts 
); 

    if ($loop->have_posts()) : 
    while ($loop->have_posts()) : $loop->the_post(); 
     // YOUR CODE HERE 
    endwhile; 
    endif; 
} 

add_shortcode('getposts', 'get_posts'); 

你的簡碼會像這個 -

getposts[cat_id="1", num_posts="5"] 

此代碼尚未經過測試,但其實這就是你怎麼做

+0

先生,這段代碼顯示錯誤時,我把我現在可以做functions.php .how現在呢? –

0

在function.php 添加該代碼,這是您的短碼 「[my_form_shortcode貓=」 1" showposts = 「5」]」。

function my_form_shortcode($atts) { 
ob_start(); 
$atts = shortcode_atts(
array(
     'cat' => '1', 
     'showposts' => '5', 
), $atts, 'my_form_shortcode'); 

//YOUR CODE START 

$recent = new WP_Query(); 
$query = "cat=".$atts['cat']."&showposts=".$atts['showposts']; 
$recent->query($query); 
$is_first_post = true; 
while($recent->have_posts()) : $recent->the_post(); ?> 
<ul> 
    <li> 
    <?php 
    if ($is_first_post && has_post_thumbnail()) { 
    the_post_thumbnail(); 
    $is_first_post = false; 
    } 
    ?> 
    <a href="<?php the_permalink(); ?>"> 
    <?php the_title(); ?> 
    </a> 
</li> 
</ul> 
<?php endwhile; 
//YOUR CODE END 

return ob_get_clean(); 
} 

add_shortcode('my_form_shortcode', 'my_form_shortcode'); 
+0

你好,先生? –

+0

在Skype上添加我prashant1879 –