在Wordpress中添加自定义帖子类型的所有视图

时间:2017-01-07 07:13:25

标签: php wordpress

我正在尝试创建http://www.naijacanteen.com中的菜单部分。我已成功创建该部分,但我想只显示六个项目并查看所有链接。知道我怎么能这样做吗?这是我的代码:

<?php 
if(has_post_thumbnail())
    the_post_thumbnail();
?>
</div>
<h3>
    <?php the_title();?></h3>
<p>
    <?php  the_content();?></p>
</div>
</div>
<?php endwhile;?>

1 个答案:

答案 0 :(得分:0)

对于创建,你必须循环遍历所有帖子。更好地使用短代码或模板使其变得灵活。我总是使用短代码。

如何创建短代码

你必须打开你的functions.php并添加下面的代码

function add_resturant_menu(){

    $args = array( 'post_type' => 'resturant_menu_item', 'posts_per_page' => 6 );
    $the_query = new WP_Query( $args ); 

     if ( $the_query->have_posts() ) : 
     while ( $the_query->have_posts() ) : $the_query->the_post(); 
    echo "<h2>".the_title()."</h2>";
    echo "<div class='entry-content'>";
    the_content(); 
    echo "</div>";
    wp_reset_postdata();
    endif;
    return;
}

add_shortcode('add_resturant_menu','add_resturant_menu');

如何在PHP文件中使用:

使用

在php文件中添加代码很简单
<?php do_shortcode('[add_resturant_menu]'); ?>

如何在WYISWYG编辑器中使用:

将行代码添加到编辑器中。

['add_resturant_menu']