假设有一篇文章属于2个或更多类别,我们希望在文章页面上显示所选类别,通过该类别我们可以访问文章页面。最好的方法是什么?是否将类别id暂时保留在会话或查询字符串中?或者其他什么?
THX公司
假设有一篇文章属于2个或更多类别,我们希望在文章页面上显示所选类别,通过该类别我们可以访问文章页面。最好的方法是什么?是否将类别id暂时保留在会话或查询字符串中?或者其他什么?
THX公司
我个人喜欢把一篇文章所附的所有类别都加上一个活动类。这是我找到的和当前使用的代码。
/** Active Category **/
// Single Posts only
function singlePostActiveCat ($CatText) {
global $post;
if (is_singular()) {
$categories = wp_get_post_categories($post->ID);
foreach ($categories as $category_id) {
$category = get_category($category_id);
$CatText = preg_replace(
"/class=\\"(.*)\\"><a ([^<>]*)>$category->name<\\/a>/",
\' class="$1 current-cat"><a $2>\' . $category->name . \'</a>\',
$CatText);
}
}
return $CatText;
}
add_filter(\'wp_list_categories\', \'singlePostActiveCat\');
要仅获取到达帖子的类别,可以使用查询变量:
$category_name = get_query_var( \'category_name\', \'No Category\' );
echo (\'Category: \' . $category_name);
get\\u query\\u var的第二个参数是在没有返回类别时使用的默认值。希望有帮助!
我知道这是一个老生常谈的问题,但我想我还是要考虑一下。
我想我会通过一个get请求来完成它。
Below-written code has not been tested.
所以在你的category.php
-文件,然后添加访问者来自的类别的id,例如:
$category = get_the_category();
$link_to_the_post = \'<a href="\' . the_permalink() . \'?cat_id=\' . $category[\'ID\'] . \'">Link to post</a>\';
然后在single.php
, 因此:if( !empty( $_GET[\'cat_id\'] ){
$category = get_the_category( $_GET[\'cat_id\'] );
echo \'Category: \' . $category[\'name\'];
} else {
// Fallback, showing all categories (in case the visitor came from some other place)
the_category();
}
我有一个问题,我需要在我的归档页面中显示完整的帖子。E、 G.我有>两篇帖子,我想在归档页面中完整显示每一篇帖子。谢谢我该如何处理胡曼主题呢?这是档案的内容。php主题:<?php get_header(); ?> <section class=\"content\"> <?php get_template_part(\'inc/page-title\'); ?> <?php if(function