【问题标题】:How to show two different categories in wordpress?如何在wordpress中显示两个不同的类别?
【发布时间】:2016-09-22 13:52:13
【问题描述】:

实际上我有一个静态网站,我正在尝试将其转换为 wordpress,我是 wordpress 的新手。我已成功创建以下页面。

header.php footer.php sidebar.php functions.php index.php

然后我为我的其他页面创建了一个main-template.php 页面,例如关于、联系人、图库等,它们都将使用相同的模板。网站快完工了,只剩下两页了。

1. Jobs

2. News & Events

因此,在这些页面中,将会有新的工作出现,新闻和事件也是如此。所以我认为这两个页面就像一篇博客文章。

职位页面将显示所有职位的标题和 100 字的描述和阅读更多按钮,然后它将在该特定职位的新页面中打开。

我有不同的工作类别,例如electricianplumbermasonenglish languagecall center

我想在职位页面上显示所有类别的职位。

所以我在wp-admin>pages>add new 中创建了一个名为JobsnewsEvents 的页面,并选择了我用于所有页面的模板main-template。但此页面将是博客类型以显示所有工作。

如你所知,我将有两个博客类型页面,一个用于工作,一个用于新闻事件所以我只创建了两个类别 JobsNewsEvents

现在如果我添加一个新工作,我将转到wp-admin>posts>add New 然后添加标题、描述,如果是工作,则选择类别工作,新闻事件类似。

所以问题是我如何在工作页面上显示工作并在新闻事件页面上显示新闻事件?

如何创建single.php?

我需要创建多少个 single.php?

I have created `single.php`
<?php
/**
 * The template for displaying Jobs
 */
/*get_header();*/
get_header();
?>
<?php

while ( have_posts() ) : the_post();
    get_template_part( 'content', 'Jobs' );
endwhile;
?>
<?php
get_footer();
?>

我在 wp-admin>pages>Job 中创建了 Empty jobs 页面,它的链接是 localhost/MyProject/jobs 我想在这个页面上显示工作,我将发布的所有工作..

我也创建了content-jobs.php 页面,接下来我该怎么做?我将如何展示工作? my content-jobs.php

    <?php the_content() ?>

我已经阅读了wordpress文档但无法正确理解,这是我第一次这样做,帮助我吗?

【问题讨论】:

    标签: php wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:

    1) 您需要为这些页面创建单独的模板。 2)创建具有接受名称的页面 3) 在管理员的这些页面上使用这些模板 4)创建自定义帖子类型新闻和事件

      add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'News',
        array(
          'labels' => array(
            'name' => __( 'News' ),
            'singular_name' => __( 'News' )
          ),
          'public' => true,
          'has_archive' => true,
        )
      );
    }
    

    在function.php中添加这段代码

    之后在此帖子类型中发布一些数据 并在您的模板文件使用中显示在前端。

    $args = array( 'post_type' => 'News', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
      the_title();
      echo '<div class="entry-content">';
      the_content();
      echo '</div>';
    endwhile;
    

    使用它会帮助你

    【讨论】:

    • 我很困惑所以有两种类型的帖子,JobsNews。我是否必须在 function.php 中为 Jobs 和 News 添加两次代码?
    • 是的,如果你想展示这些不同的不同页面,你需要创建两种类型的帖子类型 Jobs 和 News。
    • 我已经创建了两种类型的页面,但是如何做到这一点,我的新帖子wordpress.stackexchange.com/questions/227783/…
    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多