附加字段的Wordpress自定义模板

时间:2016-11-07 06:08:15

标签: php wordpress portfolio

我创建了自己的自定义Wordpress模板:

<?php
/**
 * The main template file for display portfolio page.
 *
 * Template Name: Portfolio Test
 * @package WordPress
 */

/**
*   Get Current page object

我希望此页面按类别显示投资组合,如果所选类别是儿童,则页面将显示儿童照片。

如何在管理页面选择此模板时添加字段,输入类别slug以定义此页面的字段将显示哪个类别。

如何添加类别slug字段?

1 个答案:

答案 0 :(得分:0)

首先,我建议不要这样做,Wordpress为每个博客类别使用相同的页面,例如index.php文件。导航到某个类别时,每次都会显示相同的页面。单独的博客类别对我来说似乎很遥远。

但您可以为此类帖子添加自定义字段

    <?php
      function custom_field_meta( $meta_boxes ) {
          $prefix = 'custom'; // Prefix for all fields
          $meta_boxes['custom_field_metas'] = array(
              'id' => 'custom_field_metas',
              'title' => 'Custom Field Details',
              'pages' => array( 'post', '' ), // post type
              'context' => 'normal',
              'priority' => 'high',
              'show_names' => true, // Show field names on the left
              'fields' => array(
                  array(
                      'name' => 'Blog Category',
                      'desc' => 'choose category to display',
                      'id' => $prefix . '_cat',
                      'type' => 'text'
                  ),
              ),
          );

          return $meta_boxes;
      }
      add_filter( 'cmb_meta_boxes', 'custom_field_meta' );

并使用与此代码相似的输出

    <?php global $post;
        $text = get_post_meta( $post->ID, 'custom_cat', true );
        echo $text;
    ?>

然后在查询中使用自定义元,如下所示:

    <?php
    $loop = new WP_Query( 'category_name=
         <?php global $post;
           $text = get_post_meta( $post->ID, 'custom_cat', true );
           echo $text;
         ?>
      ' );
    ?>

一些警告:我没有使用过这段代码,并怀疑custom_meta在wp_query中不起作用,因为它需要在循环中;自定义字段需要完美地插入slug或者它不起作用。

对,那你该怎么办?为您的博客设置页面,“我们的博客”或其他东西。进入Wordpress设置“阅读”并选择“我们的博客”作为您的帖子页面。然后去外观&gt;菜单并创建一个链接到您的博客类别的菜单(如果您看不到要链接的类别,请单击屏幕选项并选中“类别”)。

祝你好运。

相关问题