【问题标题】:Create a Custom WordPress Page with Post titles创建带有帖子标题的自定义 WordPress 页面
【发布时间】:2013-12-08 19:48:23
【问题描述】:

我正在尝试创建一个自定义 WordPress 页面,该页面将仅包含指向我所有帖子标题的链接,分为 4 列。我也在使用 Bootstrap 和 WordPress。

我创建了 php 文件,用她的页面属性创建了一个新页面,但帖子标题不显示。

这是我使用的代码:

    <?php
/**
 * The template used for displaying page content in questions.php
 *
 * @package fellasladies
 */
?>

<?php 

<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4 col-sm-4 pbox'); ?>>
    <header class="entry-header">
        <h1 class="entry-title"><?php the_title(); ?></h1>
    </header><!-- .entry-header -->

    <div class="entry-content">
        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'fellasladies' ),
                'after'  => '</div>',
            ) );
        ?>
    </div><!-- .entry-content -->
    <?php edit_post_link( __( 'Edit', 'fellasladies' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' ); ?>
</article><!-- #post-## -->

非常感谢您的帮助!谢谢

【问题讨论】:

    标签: php html wordpress twitter-bootstrap


    【解决方案1】:

    您需要首先创建一个查询,该查询用您要迭代的帖子填充一个数组。阅读 WordPress 中的 get_posts() 函数。

    这是一个例子。请注意,我们不能使用旨在“在循环中”使用的函数,例如 the_title() 或 the_content()。我们必须为每次迭代指定 post_id。我们不应该针对这种情况修改主查询。

    // the arguments for the get_posts() function
    $args = array(
      'post_type' => 'post', // get posts int he "post" post_type
      'posts_per_page' => -1 // this means the array will be filled with all posts
    );
    $my_posts = get_posts($args);
    
    // now we'll iterate the posts
    foreach ( $my_posts as $p ) {
      // a title
      echo get_the_title($p->ID);
      // the link
      echo get_permalink($p->ID);
      // a custom field value
      echo get_post_meta($p->ID,'custom_field_key',true);
    }
    

    每次迭代中发生的事情由您决定。

    祝你好运! :)

    【讨论】:

      【解决方案2】:

      我鼓励您在 Wordpress Codex 上阅读有关 Page Templates 的信息,这对您有很大帮助!

      页面是 WordPress 的内置帖子类型之一。您可能希望您的大部分网站页面看起来都差不多。但是,有时您可能需要一个特定的页面或一组页面来显示或表现不同。这可以通过页面模板轻松完成。

      看来你有一个&lt;?php 没用。您也没有定义模板的名称,是必需的。

      【讨论】:

      • 您能否总结一下那篇文章的主要发现?
      • 页面名称显示在页面属性下,所以我选择了它。但是,当指向该页面时,不会显示所描述的帖子标题。
      猜你喜欢
      • 2021-06-17
      • 2023-03-30
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多