作为自定义帖子类型的网站书签

时间:2011-09-22 作者:JasonDavis

让我先解释一下我想做什么

我想将我现在浏览器中的数百个网站书签添加到wordpress中,原因如下。

可以按类别搜索我的书签可以按标签搜索我的书签可以按说明和/或名称搜索我的书签可以从任何地方访问我的书签从一篇定期的博客文章/列表以及我迄今为止所做的"Website Bookmarks" 具有以下代码功能。php

<?php
/*
*  Add custom post type
*  name: website_bookmarks
*/

function bookmark_post_type()
{
    // Set some labels for our bookmarks post type
    $bookmark_labels = array(
        \'name\' => _x(\'Website Bookmark\', \'post type general name\'),
        \'singular_name\' => _x(\'Websiteite Bookmark\', \'post type singular name\'),
        \'add_new\' => _x(\'Add New\', \'Websiteite Bookmark\'),
        \'add_new_item\' => __(\'Add New Website Bookmark\'),
        \'edit_item\' => __(\'Edit Website Bookmark\'),
        \'new_item\' => __(\'New Website Bookmark\'),
        \'all_items\' => __(\'All Website Bookmarks\'),
        \'view_item\' => __(\'View Website Bookmark\'),
        \'search_items\' => __(\'Search Website Bookmarks\'),\'not_found\' => __(\'No website Bookmarks found\'),
        \'not_found_in_trash\' => __(\'No Website Bookmarks found in Trash\'),
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Website Bookmarks\'
        );

    $bookmark_args = array(
        \'labels\' => $bookmark_labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(
            \'slug\' => \'bookmark\',
            \'with_front\' => false),
        \'taxonomies\' => array(\'post_tag\', \'category\'),
        \'capability_type\' => \'post\',
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'menu_position\' => null,
        \'can_export\' => true,
        \'supports\' => array(
            \'post-thumbnails\',
            \'thumbnail\',
            \'excerpt\',
            \'custom-fields\',
            \'editor\',
            \'title\'
        )
    );
    
    register_post_type(\'website_bookmarks\', $bookmark_args);
}

add_action(\'init\', \'bookmark_post_type\');
然后是我需要的帮助/想法这给了我一个自定义的帖子类型,因此我可以将书签添加为帖子,并搜索它们和所有好东西,还可以让我轻松地将它们排除在我的主博客循环之外,因此如果我选择的话,我可以让它们更私密。另外,让我轻松地为书签创建一个模板页面。

问题我真的很想能够标记我的书签,很多书签都会有不止一个标记,例如一个关于PHP、MySQL和javascript的网站,会标记所有3个术语/标记。我知道wordpress已经内置了标签,我在当前的博客文章中经常使用它,我应该添加一个NEW taxonomy ie。"bookmark_tags" 因为当用户现在查看标记页面时,它会显示我针对该标记的所有博客帖子,我不希望PHP标记页面在同一页面上显示我帖子的所有带有PHP的标记和我所有带有PHP标记的书签,所以创建一个称为bookmark\\u标记的新分类法的最佳方法是什么?

与问题1(1)完全相同的问题,除了关于Categories 而不是tags?

有没有其他改进我的书签部分的想法或建议?

谢谢您的帮助

2 个回复
最合适的回答,由SO网友:Brian Fegter 整理而成

这里有一个干净的方法来完成你所需要的。

注册自定义帖子类型(sans post\\u标记和类别)

  • 注册特定于书签的分类法
  • 为书签选项创建帖子元框(包括示例)。您可以在元框中添加nofollow复选框选项
  • 保存post meta
    • 这是整个沙邦。

      add_action(\'init\', \'bookmark_post_type\');
      function bookmark_post_type()
      {
          $labels = array(
              \'name\' => _x(\'Bookmark\', \'post type general name\'),
              \'singular_name\' => _x(\'Bookmark\', \'post type singular name\'),
              \'add_new\' => __(\'Add New\'),
              \'add_new_item\' => __(\'Add New Bookmark\'),
              \'edit_item\' => __(\'Edit Bookmark\'),
              \'new_item\' => __(\'New Bookmark\'),
              \'all_items\' => __(\'All Bookmarks\'),
              \'view_item\' => __(\'View Bookmark\'),
              \'search_items\' => __(\'Search Bookmarks\'),\'not_found\' => __(\'No website Bookmarks found\'),
              \'not_found_in_trash\' => __(\'No Bookmarks found in Trash\'),
              \'parent_item_colon\' => \'\',
              \'menu_name\' => \'Bookmarks\'
              );
      
          $args = array(
              \'labels\' => $labels,
              \'public\' => true,
              \'publicly_queryable\' => true,
              \'show_ui\' => true,
              \'show_in_menu\' => true,
              \'query_var\' => true,
              \'rewrite\' => array(
                  \'slug\' => \'bookmark\',
                  \'with_front\' => false),
              //\'taxonomies\' => array(\'post_tag\', \'category\'),
              \'capability_type\' => \'post\',
              \'has_archive\' => true,
              \'hierarchical\' => false,
              \'menu_position\' => null,
              \'can_export\' => true,
              \'supports\' => array(
                  \'post-thumbnails\',
                  \'thumbnail\',
                  \'editor\',
                  \'title\'
              )
          );
      
          register_post_type(\'bookmarks\', $args);
      
          $labels = array(
              \'name\' => _x( \'Bookmark Categories\', \'taxonomy general name\' ),
              \'singular_name\' => _x( \'Category\', \'taxonomy singular name\' ),
              \'search_items\' =>  __( \'Search Categories\' ),
              \'all_items\' => __( \'All Categories\' ),
              \'parent_item\' => __( \'Parent Category\' ),
              \'parent_item_colon\' => __( \'Parent Category:\' ),
              \'edit_item\' => __( \'Edit Category\' ), 
              \'update_item\' => __( \'Update Category\' ),
              \'add_new_item\' => __( \'Add New Category\' ),
              \'new_item_name\' => __( \'New Genre Category\' ),
              \'menu_name\' => __( \'Bookmark Categories\' ),
          );  
      
          register_taxonomy(\'bookmark_categories\',array(\'bookmarks\'), array(
                  \'hierarchical\' => true,
                  \'labels\' => $labels,
                  \'show_ui\' => true,
                  \'query_var\' => true,
                  \'rewrite\' => array( \'slug\' => \'bookmarks_categories\' ),
          ));
      
          $labels = array(
              \'name\' => _x( \'Bookmark Tags\', \'taxonomy general name\' ),
              \'singular_name\' => _x( \'Tag\', \'taxonomy singular name\' ),
              \'search_items\' =>  __( \'Search Tags\' ),
              \'popular_items\' => __( \'Popular Tags\' ),
              \'all_items\' => __( \'All Tags\' ),
              \'parent_item\' => null,
              \'parent_item_colon\' => null,
              \'edit_item\' => __( \'Edit Tag\' ), 
              \'update_item\' => __( \'Update Tag\' ),
              \'add_new_item\' => __( \'Add New Tag\' ),
              \'new_item_name\' => __( \'New Tag Name\' ),
              \'separate_items_with_commas\' => __( \'Separate tags with commas\' ),
              \'add_or_remove_items\' => __( \'Add or remove tags\' ),
              \'choose_from_most_used\' => __( \'Choose from the most used tags\' ),
              \'menu_name\' => __( \'Bookmark Tags\' ),
            ); 
      
          register_taxonomy(\'bookmark_tags\',\'bookmarks\',array(
              \'hierarchical\' => false,
              \'labels\' => $labels,
              \'show_ui\' => true,
              \'update_count_callback\' => \'_update_post_term_count\',
              \'query_var\' => true,
              \'rewrite\' => array( \'slug\' => \'bookmark_tags\' ),
          ));
      
      }
      
      //Create meta box
      add_action(\'admin_init\', \'bookmark_meta_boxes\');
      function bookmark_meta_boxes(){
          add_meta_box(\'bookmarks-meta\', __(\'Bookmark Options\'), \'bookmark_options\', \'bookmarks\', \'normal\', \'high\');
      }
      
      //Meta box form data
      function bookmark_options(){
          global $post;
          $url = get_post_meta($post->ID, \'bookmark-url\', true);
      
          echo "
          <div>
              <label for=\'bookmark-url\'>URL</label>
              <input style=\'width:100%; padding:4px;\' type=\'text\' name=\'bookmark-url\' value=\'$url\' />
          </div>
          ";
      }
      
      //Save the form data
      add_action(\'save_post\', \'bookmark_save\');
      function bookmark_save($post_ID){
          //Do nonce checking here
          if(\'bookmarks\' === $_REQUEST[\'post_type\']){
              update_post_meta($post_ID, \'bookmark-url\', esc_html($_REQUEST[\'bookmark-url\']));
          }
      }
      
      要从主循环中删除书签,请在循环代码上方添加以下内容:

      query_posts(\'post_type=post\');
      
      如果要控制CPT的存档和单个上下文,则需要添加两个名为存档书签的文件。php和单个书签。php

      通过在帖子类型中保持“public\\u queryable”为true,可以搜索书签帖子。

      您可以对屏幕截图图像使用\\u post\\u缩略图功能。

      我会使用内容/编辑器区域来描述您的书签。

    SO网友:Bainternet

    我觉得人们害怕习惯分类法,这是一种耻辱,

    抄本:

    自WordPress版本2.3以来,您就可以创建自己的自定义分类法,但在版本2.9之前,这些分类法是WordPress很少使用的功能。

    对于两个书签tagscategories 您应该使用custom taxonomies, 这将使您的搜索查询更加容易,不会影响网站的其他部分(贴子标签和类别)。

    免费:)

    结束

    相关推荐

    Hooks for Links Box

    Possible Duplicate:Getting archive pages in WP's AJAX internal link finder? 新的有挂钩吗internal links box 创建于WP 3.1? 我正在尝试在插件中修改它。