在静态页面上显示博客文章

发布于 2024-12-18 05:51:35 字数 1432 浏览 4 评论 0原文

我对 WordPress 比较陌生,有一个问题。我创建了自己的主题,一切似乎都运行良好。但是,我有一个问题。我想在主页以外的页面上创建我的博客页面(包含所有帖子)。因此,在我的主题文件夹中,我创建了一个名为 blog.php 的页面模板:

 <?php
  /*
  Template Name: blog  
  */
  ?>
  <?php get_header(); ?>

    <table id="about-table"  >
<tr>
    <td colspan="7">            
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
            <?php the_title(); ?>
            <?php the_author(); ?>
            <?php the_time("jS F"); ?>
            <?php comments_number("0","1","%"); ?>
            <?php the_excerpt(); ?>
        <?php endwhile; endif; ?>
    </td>
</tr>
   </table> 
   <?php get_footer(); ?>   

然后,我还在仪表板的“页面”部分中在 WordPress 中创建了一个名为“blog”的页面。然后我将其模板分配给上面的“博客”模板。但问题是,该代码无法正常工作。它没有向我显示帖子的标题、评论等,而是显示一些其他信息。另一方面,如果我只是将其复制

   <table id="about-table"  >
<tr>
    <td colspan="7">            
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
            <?php the_title(); ?>
            <?php the_author(); ?>
            <?php the_time("jS F"); ?>
            <?php comments_number("0","1","%"); ?>
            <?php the_excerpt(); ?>
        <?php endwhile; endif; ?>
    </td>
</tr>
   </table> 

到我的索引页,它就可以正常工作。那么,如何在主页以外的页面上显示我的所有帖子信息呢?

I am rather new to Wordpress and have a question. I have created my own theme, which all seems to work fine. But, I am having one issue. I want to create my blog page (with all the posts) on a page other than my home page. So, in my theme folder, I created a page template called blog.php:

 <?php
  /*
  Template Name: blog  
  */
  ?>
  <?php get_header(); ?>

    <table id="about-table"  >
<tr>
    <td colspan="7">            
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
            <?php the_title(); ?>
            <?php the_author(); ?>
            <?php the_time("jS F"); ?>
            <?php comments_number("0","1","%"); ?>
            <?php the_excerpt(); ?>
        <?php endwhile; endif; ?>
    </td>
</tr>
   </table> 
   <?php get_footer(); ?>   

Then, I created a page in wordpress called "blog" as well, in the "pages" section in the dashboard. I then assigned its template to the above "blog" template. The problem is, though, that the code does not work as it should. Instead of showing me the titles, comments, etc of the posts, it displays some other info. On the other hand, if i just copy this:

   <table id="about-table"  >
<tr>
    <td colspan="7">            
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
            <?php the_title(); ?>
            <?php the_author(); ?>
            <?php the_time("jS F"); ?>
            <?php comments_number("0","1","%"); ?>
            <?php the_excerpt(); ?>
        <?php endwhile; endif; ?>
    </td>
</tr>
   </table> 

to my index page, it works fine. So, how do I display all my post info on a page other than the home page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够 2024-12-25 05:51:35

我想为您提供一个更简单的循环作为第二个选择。如果您使用此选项并将阅读设置设置为特定博客页面,则效果很好:

<?

/*

Template Name: Blog Template Example

*/

?>

<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php the_content(); ?>
</div>

<?php endwhile; ?>

<div class="navigation">
    <div class="next-posts"><?php next_posts_link(); ?></div>
    <div class="prev-posts"><?php previous_posts_link(); ?></div>
</div>

<?php else : ?>

<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1>Not Found</h1>
</div>

<?php endif; ?>
<?php get_sidebar(); ?>

<?php get_footer(); ?>

阅读设置

I wanted to provide you with a simpler loop as a second option. If you use this and set Reading settings to the specific blog page this works well:

<?

/*

Template Name: Blog Template Example

*/

?>

<?php get_header(); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php the_content(); ?>
</div>

<?php endwhile; ?>

<div class="navigation">
    <div class="next-posts"><?php next_posts_link(); ?></div>
    <div class="prev-posts"><?php previous_posts_link(); ?></div>
</div>

<?php else : ?>

<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1>Not Found</h1>
</div>

<?php endif; ?>
<?php get_sidebar(); ?>

<?php get_footer(); ?>

Reading Settings

小…楫夜泊 2024-12-25 05:51:35

你可以做这样的事情怎么样:

blog-template.php:

<?php/*
Template Name: Blog Page
*/
?>

<?php get_header(); ?>
<?php get_template_part( 'layout-page', 'blog' );?>
<?php get_footer(); ?>

layout-page-blog.php:

<?php 
the_post();
$title = get_the_title();
$baselink = get_permalink();
$category = get_field('category_blog'); 

if( !empty($category) ){
    $post_per_page = get_option('posts_per_page'); 
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;

    $categoryID = get_category_id($category);

    $total = get_post_count(array($categoryID));

    $the_query = new WP_Query("posts_per_page={$post_per_page}&cat=    {$categoryID}&paged={$paged}");
?>

<div id="wrapper">
<div id="content">
    <h1 class="title"><?php echo $title; ?></h1>
    <div class="content-middle">
        <div class="node">              
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
            <h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="content">
                <?php echo content(150); ?>
            </div>
            <div class="read-more"><a href="<?php echo get_permalink(); ?>">Read more</a></div>                     
            <?php endwhile; ?>
            <br/><br/>
            <div class="wp-paginate">
            <?php 
                wp_reset_query();

                echo paginate_links( array(
                    'base' => $baselink.'%_%',
                    'total' => ceil($total/$post_per_page),
                    'current' => $paged,
                ));

                ?>
            </div>
        </div>
    </div>

</div> <!-- end content -->

<div style="clear:both"></div>
</div>

<?php
}
?>

这可以全部在一个文件中,或者你可以像我写的那样分成两部分使用它。

编辑:

抱歉,我已将其设置为也从帖子中抓取图像。我认为这是您需要的功能代码:

function get_images_by_cat($id){
    $limit = 1000;

    $the_query = new WP_Query("posts_per_page={$limit}&cat={$id}");
    $arr = array();
    while ( $the_query->have_posts() ) { 
        $the_query->the_post();

        $title = get_the_title();
        $image_src = get_field('banner_image');
        $image_link = get_field('banner_link');

        $arr[] = array(
            "title" => $title,
            "link" => $image_link,
            "image" => $image_src,
        );
    }

    wp_reset_query();

    return $arr;    
}

How about you do something like this:

blog-template.php:

<?php/*
Template Name: Blog Page
*/
?>

<?php get_header(); ?>
<?php get_template_part( 'layout-page', 'blog' );?>
<?php get_footer(); ?>

layout-page-blog.php:

<?php 
the_post();
$title = get_the_title();
$baselink = get_permalink();
$category = get_field('category_blog'); 

if( !empty($category) ){
    $post_per_page = get_option('posts_per_page'); 
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;

    $categoryID = get_category_id($category);

    $total = get_post_count(array($categoryID));

    $the_query = new WP_Query("posts_per_page={$post_per_page}&cat=    {$categoryID}&paged={$paged}");
?>

<div id="wrapper">
<div id="content">
    <h1 class="title"><?php echo $title; ?></h1>
    <div class="content-middle">
        <div class="node">              
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
            <h3><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="content">
                <?php echo content(150); ?>
            </div>
            <div class="read-more"><a href="<?php echo get_permalink(); ?>">Read more</a></div>                     
            <?php endwhile; ?>
            <br/><br/>
            <div class="wp-paginate">
            <?php 
                wp_reset_query();

                echo paginate_links( array(
                    'base' => $baselink.'%_%',
                    'total' => ceil($total/$post_per_page),
                    'current' => $paged,
                ));

                ?>
            </div>
        </div>
    </div>

</div> <!-- end content -->

<div style="clear:both"></div>
</div>

<?php
}
?>

This could all be in one file, or you can use it in two pieces as I have written it.

EDIT:

Sorry I have it set to also grab the images from the post. I think this is the functions code you need:

function get_images_by_cat($id){
    $limit = 1000;

    $the_query = new WP_Query("posts_per_page={$limit}&cat={$id}");
    $arr = array();
    while ( $the_query->have_posts() ) { 
        $the_query->the_post();

        $title = get_the_title();
        $image_src = get_field('banner_image');
        $image_link = get_field('banner_link');

        $arr[] = array(
            "title" => $title,
            "link" => $image_link,
            "image" => $image_src,
        );
    }

    wp_reset_query();

    return $arr;    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文