WordPress的 - 自定义分类模板 - 页面未找到?(Wordpress - custom taxonomy template - page not found?)
我为该帖子创建了一个自定义帖子和自定义分类,并且帖子类型和分类法都可以正常工作:
function venue_post() { $rewrite = array( 'slug' => 'venue', 'with_front' => true, 'pages' => true, 'feeds' => true, ); $args = array( 'label' => __( 'Venue', 'text_domain' ), 'description' => __( 'Venue Posts', 'text_domain' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'thumbnail'), 'taxonomies' => array( 'locationCategories' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-groups', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'rewrite' => $rewrite, 'capability_type' => 'post', ); register_post_type( 'venue_post', $args ); } function venue_locationCategories() { $rewrite = array( 'slug' => 'venue-location', 'with_front' => true, 'hierarchical' => false, ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_tagcloud' => true, 'rewrite' => $rewrite, ); } register_taxonomy( 'locationCategories', array( 'venue_post' ), $args );
然后我设置了一个名为以下的模板页面:
taxonomy-locationCategories.php
但是当我访问分类页时,例如
www.example.com/locationcategories/london
我只是得到了404。任何想法我失踪我已经刷新永久链接。I have created a custom post and a custom taxonomy for that post and both the post type and the taxonomy work fine:
function venue_post() { $rewrite = array( 'slug' => 'venue', 'with_front' => true, 'pages' => true, 'feeds' => true, ); $args = array( 'label' => __( 'Venue', 'text_domain' ), 'description' => __( 'Venue Posts', 'text_domain' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'thumbnail'), 'taxonomies' => array( 'locationCategories' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-groups', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'rewrite' => $rewrite, 'capability_type' => 'post', ); register_post_type( 'venue_post', $args ); } function venue_locationCategories() { $rewrite = array( 'slug' => 'venue-location', 'with_front' => true, 'hierarchical' => false, ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_tagcloud' => true, 'rewrite' => $rewrite, ); } register_taxonomy( 'locationCategories', array( 'venue_post' ), $args );
I have then setup a template page named the following:
taxonomy-locationCategories.php
but when I visit the taxonomy page e.g.
www.example.com/locationcategories/london
I just get a 404. Any idea what I'm missing I have refreshed permalinks.
原文:https://stackoverflow.com/questions/47223339
最满意答案
好吧,经过一些挖掘TextKit框架后,我终于找到了答案。
我的代码在这样的循环中工作:
while ([self needsMoreColumns]) { [self addColumn]; } ... - (BOOL)needsMoreColumns { // Always create at least one column if (self.layoutManager.textContainers.count == 0) return YES; // Find out the glyph range of the last column NSRange range = [self.layoutManager glyphRangeForTextContainer:[self.layoutManager.textContainers lastObject]]; NSUInteger glyphs = [self.layoutManager numberOfGlyphs]; // Compare it with the number of glyphs return range.location + range.length < glyphs; }
我没有包含方法
[self addColumn]
因为它没有脑子。 它只使用我的布局几何和最后一列的位置(如果有的话)来计算下一列的CGRect
。 然后,它创建具有相应大小的NSTextContainer
,并将矩形的origin
属性存储在专用数组中以用于绘图目的。我还发现了方法
[NSLayoutManager firstUnlaidCharacterIndex]
和[NSLayoutManager firstUnlaidGlyphIndex]
但它们似乎没有按预期工作。 在仅在一列中布置三列文本后,它们返回整个字符串的长度,而不是第一个不适合第一列的字符的位置。 这就是为什么我宁愿使用基于范围的方法。这是所有人,安全! 皮特。
Well, after some digging through the TextKit framework I've finally found the answer.
My code works in a loop like this:
while ([self needsMoreColumns]) { [self addColumn]; } ... - (BOOL)needsMoreColumns { // Always create at least one column if (self.layoutManager.textContainers.count == 0) return YES; // Find out the glyph range of the last column NSRange range = [self.layoutManager glyphRangeForTextContainer:[self.layoutManager.textContainers lastObject]]; NSUInteger glyphs = [self.layoutManager numberOfGlyphs]; // Compare it with the number of glyphs return range.location + range.length < glyphs; }
I didn't include the method
[self addColumn]
as it's a no brainer. It simply uses the geometry of my layout and position of the last column (if any) to compute theCGRect
of the next one. Then, it createsNSTextContainer
with respective size and stores theorigin
property of the rectangle in a dedicated array for drawing purposes.I've also discovered methods
[NSLayoutManager firstUnlaidCharacterIndex]
and[NSLayoutManager firstUnlaidGlyphIndex]
but they don't seem to work as expected. After laying out three columns worth of text in only one column, they returned the length of the entire string and not the position of the first character which didn't fit into the first column. That's why I rather used the range-based approach.That's all folks, be safe! Pete.
相关问答
更多-
将文本容器添加到NSLayoutManager会导致异常(Adding text container to NSLayoutManager results in exception)[2023-11-07]
由于我调用了replaceLayoutManager因此发生了错误。 我改变了初始化文本视图的方式。 我没有使用init(frame: frameRect)构造函数,而是通过单独初始化文本容器来使用指定的初始化程序: var nextContainer = CASTextContainer(containerSize: containerSize) var nextView = CASTextView(frame: rect, textContainer: nextContainer) 然后我继续调用ma ... -
您需要保持对您创建的NSTextStorage变量的引用。 我不太确定这一切的机制,但看起来文本视图只保留对其文本存储对象的弱引用。 一旦此对象超出范围,文本视图就不再可用。 我想这与MVC设计模式保持一致,其中视图(在这种情况下为NSTextView )意味着独立于其模型( NSTextStorage对象)。 import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { @IBOutle ...
-
如何使用NSLayoutManager在CALayer中呈现文本?(How to render text in a CALayer using NSLayoutManager?)[2024-01-20]
我创建了一个自定义CALayer并覆盖了drawInContext方法,如下所示: override func drawInContext(ctx: CGContext!) { NSGraphicsContext.saveGraphicsState() var nsgc = NSGraphicsContext(CGContext: ctx, flipped: false) NSGraphicsContext.setCurrentContext(nsgc) NSColor. ... -
我发现了一种方法来完成这项工作。 在NSLayoutManagerDelegate方法didCompleteLayoutFor textContainer中:我正在检查一些段落是否在多于一个文本容器中,如果是,只需减少第一个段落的高度。 像魅力一样工作,但需要重新输出文字。 I've discovered a way to do the job. In the NSLayoutManagerDelegate method didCompleteLayoutFor textContainer: I'm che ...
-
NSLayoutManager / NSTextContainer忽略比例因子(NSLayoutManager/NSTextContainer Ignores Scale Factor)[2022-01-18]
答案结果很简单:将NSTextView添加为NSTextView的子视图,其框架与NSTextView相同。 最终的高度函数如下: -(CGFloat)calculateHeightForAttributedString:(NSAttributedString*)attributedNotes { CGFloat width = self.textView.frame.size.width; // http://www.cocoabuilder.com/archive/cocoa/54083 ... -
您应该可以使用-[NSString sizeWithAttributes:]首先为具有给定绘图属性的字符串获取边界框,然后相应地调整视图大小并进行字符串的实际绘制。 You should be able to use -[NSString sizeWithAttributes:] to get a bounding box for the string with a given set of drawing attributes first, then size your view accordingly ...
-
好吧,经过一些挖掘TextKit框架后,我终于找到了答案。 我的代码在这样的循环中工作: while ([self needsMoreColumns]) { [self addColumn]; } ... - (BOOL)needsMoreColumns { // Always create at least one column if (self.layoutManager.textContainers.count == 0) return YES; ...
-
尝试通过textview的textContainerInset调整字形rect: var glyphRect = txtView.layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: glyphContainer!) glyphRect.origin.y += txtView.textContainerInset.top 这应该够了吧! Try adjusting the glyph rect by th ...
-
每个段落带有NSTextContainer的NSLayoutManager(NSLayoutManager with an NSTextContainer per paragraph)[2023-04-24]
我认为最简单的方法是子类NSTextContainer和覆盖 - (CGRect)lineFragmentRectForProposedRect:(CGRect)proposedRect atIndex:(NSUInteger)characterIndex writingDirection:(NSWritingDirection)baseWritingDirection remainingRect:(CGRect *)remainingRect 在自定义方法中,您将检查characterIndex的属性,并 ... -
事实证明我忘了添加 [self.textStorage addLayoutManager:self.layoutManager]; 这解决了这个问题。 It turns out I had forgotten to add [self.textStorage addLayoutManager:self.layoutManager]; That fixes the issue.