创建三个唯一的标记页面模板

时间:2013-08-08 作者:nanoo_k

在我的网站上,用户可以查看博客或卡通索引页上的内容。这两个页面都聚合了帖子和自定义帖子类型。卡通索引的不同之处在于,它在博客风格的内容之前有一个图像滑块。我们要创建标记。php页面,在单击标记链接时考虑一个人是在卡通档案中还是在博客中。

我无法附加每个页面的图像(我没有足够的声誉),但如果您想查看示例,请发消息给我。

现在,每一个对tag的调用。无论调用来自blogstream还是其他地方,php都会生成相同的页面。如何生成一个标记页面,该页面在调用访问者时对访问者的位置很敏感。或者有人能看到其他选择吗?

谨致问候--

编辑:

感谢George建议自定义分类法。这几乎让我找到了一个解决方案。然而,在把自己钉在这条道路上之前,我想考虑一下其他选择。我将把备选方案张贴在下面的答案中。

为了澄清,我的问题是,我想使用两个或三个唯一的模板显示标记结果。一个模板是blogstream,另两个模板有一个图像滑块,下面有一个blogstream。最后两个不同之处在于,它们显示不同类别的图像。例如,当一个人在查看blogstream时,当他们单击标记链接时,我希望给他们一个看起来像blogstream的标记页面(意思是没有图像滑块)。

6 个回复
最合适的回答,由SO网友:nanoo_k 整理而成

再次感谢米洛和乔治的帮助。我最终决定使用自定义分类法。这是我的程序。

// 1. Create the two taxonomies I need.
function  create_playground_taxonomy() {
    register_taxonomy( \'cartoon-playground\', \'post\', array (
            \'rewrite\' => array( \'slug\' => \'Cartoon-Playground\'),
            \'hierarchical\' => false,
            \'labels\' => array(
                \'name\' => _x( \'Cartoon Playground Tags\', \'taxonomy general name\' ),
                \'all_items\' => __( \'All Playground Cartoons\' ), 
                \'singular_name\' => _x( \'Cartoon Playground Tag\', \'taxonomy singular name\' )
            )
        )
    );
}
add_action( \'init\', \'create_playground_taxonomy\' );
function  create_treasury_taxonomy() {
    register_taxonomy( \'cartoon-treasury\', \'post\', array (
            \'rewrite\' => array( \'slug\' => \'Cartoon-Treasury\' ),
            \'hierarchical\' => false,
            \'labels\' => array(
                \'name\' => _x( \'Cartoon Treasury Tags\', \'taxonomy general name\' ),
                \'all_items\' => __( \'All Treasury Cartoons\' ), 
                \'singular_name\' => _x( \'Cartoon Treasury Tag\', \'taxonomy singular name\' )
            )
        )
    );
}
add_action( \'init\', \'create_treasury_taxonomy\' );


// 2. Copies terms from post_tag to the two new taxonomies.
// After updating functions.php, refresh any non-admin page
// on your site——that\'s what activates the function.
function replicateTaxonomyTerms() {
    $terms = get_terms( \'post_tag\' );
    $count = count($terms);
    if ( $count > 0 ){
        foreach ( $terms as $term ) {
            wp_insert_term( $term->name, \'cartoon-treasury\', array( \'slug\' => $term->slug) );
            wp_insert_term( $term->name, \'cartoon-playground\', array( \'slug\' => $term->slug) );
        }
    }
}
add_action(\'init\', \'replicateTaxonomyTerms\');


// 3. Identifies post_tag terms that are associated with one post
// and associates that post with terms in the other taxonomies.
// 
// I needed this code because I invented these taxonomies AFTER
// the posts were imported. If you define the taxonomies before
// importing, you should work with your import functions instead.
// This code was used only to initially update all my posts. Below
// is a different function that I use update the posts every day.
// 
// So for example post1 is originally associated with the post_tag
// term \'green-bean.\' After running this code, post1 will also be
// associated with the treasury taxonomy term \'green-bean.\'
// 
// Refresh a non-admin page once this code is in functions.php.
// Increment the offset by 500 (or whatever your server can handle)
// until you modify all your posts.
// 
// When you\'ve updated all your posts, comment-out or delete the code.
function updatePostsTerms() {
    global $post;  
    $args = array(  
        \'posts_per_page\' => 500,
        \'offset\' => 0,
        \'post_type\' => \'post\',
        \'cat\' => 953
        );  
    $the_query = get_posts( $args );  // Should grab ALL posts
    if ($the_query) {  
        foreach ($the_query as $post) {
            $post_id = $post->ID;
            $args = array( \'orderby\' => \'name\', \'order\' => \'ASC\', \'fields\' => \'names\' );
            $tags = wp_get_post_tags( $post_id, $args ); // Gets existing tags. The array is a list af arguments used to grab only the term IDs and order them.
            $new_terms = implode(\', \', $tags);
            $append = false;
            wp_set_post_terms( $post_id, $new_terms, \'cartoon-treasury\', $append );
            wp_set_post_terms( $post_id, $new_terms, \'cartoon-playground\', $append );
        }
    }
    wp_reset_query();  
}
add_action(\'wp_head\', \'updatePostsTerms\'); */


// Duplicates post_tag terms when post is saved. So for example user
// inputs \'chocolate\' as a new tag term. When saved, \'chocolate\' is
// saved to the other taxonomies as well.
// 
// To maintain accurate tag clouds, I need some posts to NOT be
// associated with all taxonomies, and so I check the posts\' category
// to see if it\'s one that should have the post_tag term duplicated.
// 
// Keep this code in functions.php.
function duplicateTerms(  ){
    $post_id = $_POST[\'post_ID\'];
    $new_terms = $_POST[\'adv-tags-input\'];
    $categories = $_POST[\'post_category\'];
    $new_terms = explode(\',\', $new_terms);
    foreach ($new_terms as $term){
        wp_insert_term( $term, \'cartoon-treasury\', array( \'slug\' => $term) );
        wp_insert_term( $term, \'cartoon-playground\', array( \'slug\' => $term) );
    }
    $append = false;
    foreach ( $categories as $category ) {
        if ( $category == 953 ) {
            wp_set_post_terms( $post_id, $new_terms, \'cartoon-treasury\', $append );
        }
    }
    foreach ( $categories as $category ) {
        if ( $category == 4074 ) {
            wp_set_post_terms( $post_id, $new_terms, \'cartoon-playground\', $append );
        }
    }
}
add_action( \'save_post\', \'duplicateTerms\' );

// With these taxonomies, I have control over tag clouds and template pages.
// When looking at cartoons via the slider, a user can click a tag from
// the tag cloud and be directed to another slider page. These slider pages
// have URLs that look like this: site.com/taxonomy-name/term , and I can 
// modify these pages by modifying the taxonomy\'s template.
// 
// When looking at cartoons on the blog, a user can click a tag from the tag
// cloud and be directed to posts in blog-format. URLs look like this: site.com
// /blog/term.
// 
// I\'m still running into some problems with this setup, by most things
// are working. :)
谢谢大家的帮助。

SO网友:George Yates

如果你真的想用单数标签。php文件,您可以在$_SERVER[\'HTTP_REFERER\'] 价值来确定您来自何处load a template part 根据结果,只需确保其中有一个默认值。抱歉,如果不知道您的URL结构,我无法更详细地介绍regex。

如果您只想有条件地显示模板的特定部分(如滑块),我会将其包装为if 语句,而不是为了可维护性而交换整个模板。

SO网友:nanoo_k

使用自定义分类法的解决方案:

如果我创建两个自定义分类法,那么Wordpress默认允许我创建指向唯一分类法页面的术语链接。可以使用wp_get_object_terms($post->ID, $taxonomy)get_term_link($term->slug, $taxonomy) 功能。

我的URL结构最终如下所示:

地点com/$分类法-1/%术语%
站点。com/$分类法-2/%术语%
站点。com/标签/%术语%

我现在的问题是,我希望这三种分类法都是相同的。因此,如果一个帖子在一个分类法中被标记为“苹果、桔子、水果”,那么在另外两个分类法中应该被标记为“苹果、桔子、水果”。我会手动执行此操作,但我正在处理2000多个帖子。

SO网友:Milo

您可以添加一个重写端点,以便普通标记视图具有如下URL/tag/apple/, 来自其他视图的链接附加了端点,如/tag/apple/blogstream/:

function wpa_tag_enpoint(){
    add_rewrite_endpoint( \'blogstream\', EP_TAGS );
}
add_action( \'init\', \'wpa_tag_enpoint\' );
然后你可以过滤tag_template 并检查是否存在该查询变量并加载其他模板:

function wpa_tag_template( $templates = \'\' ){
    global $wp_query;
    if( array_key_exists( \'blogstream\', $wp_query->query_vars ) ){
        $templates = locate_template( \'stream-tag.php\', false );
    }
    return $templates;
}
add_filter( \'tag_template\', \'wpa_tag_template\' );

SO网友:nanoo_k

使用category-$slug和archive-$posttype模板页的解决方案:

首先,一些背景。我的帖子分为几个类别:“操场”、“金库”、“文章”等。“操场”和“金库”是需要在顶部使用图像滑块标记结果页的类别。但我希望所有类别都显示在所有标记结果页面上。默认情况下,Wordpress提供类别-$slug。php页面,所以我可以有分类操场。php和类别库。php和标签。php(将显示blogstream样式的标记结果)。

或者,“游乐场”和“财政部”职位可以移动到同名的自定义职位类型中。然后我就可以拥有档案库了。php和归档游乐场。php和标签。php。

这两种解决方案都给我留下了一个相同的问题:类别或归档页面是否可以按标记术语过滤?让我用这些URL来说明:

我想要site.com/treasury/$term 指向类别库。php和show在图像滑块中仅显示标记为$term的帖子。(在滑块下方,我会编写一个自定义查询,以获取其他类别的帖子,并将其显示在博客流中)。这同样适用于site.com/playground/$term.

如果我能做到这一点,那么我只需要创建指向这些页面的链接。同样,blogstream链接返回blogstream样式的页面(本例中为tag.php)。标记类别操场中的链接。php应该导致类别混乱的样式页面。我想我可以使用RegEx来查找标记中的/tag/in链接,并将其替换为/playway/。

如果您能就这个或我以前的解决方案提出建议,我将不胜感激。(“最终”感激…因为我们是凡人…呵呵)。

谨致问候

SO网友:nanoo_k

下面的代码是米洛答案的扩展。

function wpa_tag_enpoint(){

    // Add /treasury/ to end of tag links on treasury-tag.php and the treasury index page
    if ( is_page_template( \'treasury-tag.php\' ) && is_page_template( \'treasury-index.php\' ) ){
    add_rewrite_endpoint( \'treasury\', EP_TAGS );
    }
    // Add /playground/ to end of tag links on playground-tag.php and the playground index page
    if ( is_page_template( \'playground-tag.php\' ) && is_page_template( \'playground-index.php\' ) ){
    add_rewrite_endpoint( \'playground\', EP_TAGS );
    }

    // Nothing is added to tag links elsewhere, so those go to tag.php which displays in blogstream format.
}
add_action( \'init\', \'wpa_tag_enpoint\' );


// Why is $templates defined as an empty string?
function wpa_tag_template( $templates = \'\' ){
    global $wp_query;

    // If \'treasury\' exists as a key in the $wp_query array, load treasury-tag.php
    if( array_key_exists( \'treasury\', $wp_query->query_vars ) ){
        $templates = locate_template( \'treasury-tag.php\', false );
    }

    // If \'playground\' exists as a key in the $wp_query array, load playground-tag.php
    if( array_key_exists( \'playground\', $wp_query->query_vars ) ){
        $templates = locate_template( \'playground-tag.php\', false );
    }
    return $templates;
}

// I couldn\'t find an entry in the codex for the tag_template filter, but I\'m guessing this just hooks the above function when a template is being chosen.   
add_filter( \'tag_template\', \'wpa_tag_template\' );
我想知道新的URL是否可以在不改变功能的情况下再次重写,以便访问者看到site.com/playground/tag/apple 而不是site.com/tag/apple/playground.

结束

相关推荐

在editags.php中使用多个id过滤分类术语

我想使用编辑标签。具有多个术语id的php搜索函数。例如,如果我在搜索框中键入以下内容:#1245&;6832,它应该在结果表(WP\\U terms\\U List\\U table)中显示这两个术语。我尝试使用pre\\u get\\u posts操作访问正在运行的搜索查询,但这只是显示一个“空”查询:add_action(\'pre_get_posts\', \'filter_terms_by_ids\' ); function filter_terms_by_ids( $w