简体   繁体   English

将内容应用于Wordpress主题部分

[英]Apply content to wordpress theme sections

Somebody help! 有人帮忙! I am new to Wordpress and trying to build single page responsive(bootstrap) theme from scratch. 我是Wordpress的新手,正在尝试从头开始构建单页响应(bootstrap)主题。 My index.php page looks like this: 我的index.php页面如下所示:

<?php get_header(); ?>
<section id="plx_mission_section" >
  <div class="container">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h2>title</h2></div>
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>text</p></div>
    </div>
  </div>
</section>

<section id="plx_whyus_section" >
  <div class="container">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h2>title</h2></div>
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>text</p></div>
    </div>
  </div>
</section>

......
<?php get_footer(); ?>

So my question is: How Can I add content to specific section? 所以我的问题是:如何向特定部分添加内容? looked for the answer but could not find good working example. 寻找答案,但找不到合适的例子。

You need a get_posts() Loop in each of your sections 每个部分都需要一个get_posts()循环

<?php
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
 <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h2>
    <?php the_title(); ?>
</h2></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h2>
    <?php the_content(); ?>
</h2></div>
<?php endforeach; 
wp_reset_postdata();?>

Edit $args as needed 根据需要编辑$ args

Updated: If it's fixed content in each section I would simply create a new wordpress page for each section. 更新:如果每个部分的内容都是固定的,我只需为每个部分创建一个新的wordpress页面。 Then you can run a loop like this in your index.php 然后,您可以在index.php中运行这样的循环

<?php

$mypages = get_pages( array( 'include' => '1, 2, 3, 4', 'sort_column' => 'post_title', 'sort_order' => 'asc' ) );

    foreach( $mypages as $page ) {      
        $content = $page->post_content;
        if ( ! $content ) // Check for empty page
            continue;

        $content = apply_filters( 'the_content', $content );
    ?>
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2></div>
         <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><?php echo $content; ?></div>
    <?php
    }   
?>

Replace '1, 2, 3, 4' with your new page ids created above. 将“ 1、2、3、4”替换为上面创建的新页面ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM