如何按类别或自定义帖子类型显示相关帖子?
-
-
所以,基本上,您想要的是用于single.php中的侧边栏,以检测在页面模板中拉出的帖子,并使用类别或帖子类型来确定要显示的相关个人资料/帖子?那么,现在健康与科学,学生成功与创意社区仅仅是带有模板的页面还是自定义帖子类型的页面?So basically what you want, is for the sidebar that's in single.php to detect the post being pulled in the page template and use a category or post type to determine what related profiles/posts to show? So right now Health & Science, Student Success and Creative community are just pages with templates versus a custom post type?
- 0
- 2014-06-26
- RachieVee
-
是.我一直在研究wordpress所使用的条件语句,但无法弄清楚如何阅读该页并显示相关文章.Yes. I been looking into the conditional statements that wordpress uses but I can't figure how I can get it to read that page and show the related post.
- 0
- 2014-06-26
- jose
-
好吧,请给我一点时间尝试模仿我的WordPress安装中的所需内容,我会看看我能提供什么.Ok give me a bit of time to try and imitate what you want in a WordPress install of mine and I'll see what I can provide.
- 0
- 2014-06-27
- RachieVee
-
感谢您的帮助@RachieVee.如果它开始变得麻烦很多,您不必担心.谢谢!I appreciate your help @RachieVee. If it starts to become to much trouble you don't have to worry about it. Thanks!
- 0
- 2014-06-27
- jose
-
哦,天哪,这花了我一段时间,但是如果您不介意我不提供代码,我至少可以尝试为您指明正确的方向.我在您的模板中看到您正在查询具有类别的帖子-为什么不在"捐助者资料"区域中创建一个新循环,以按该类别获取帖子并列出链接(假设类别类似于Health-Science或其中之一)您的三个部分)?或者,如果您不想为使用get_posts循环创建的菜单提取类别,则可以在个人资料中使用postmeta.我觉得我没有道理?让我知道.Oh man, haha, yeah it's taking me a while, but if you don't mind that I don't provide code, I can at least try and point you in the right direction. I see in your template that you're querying posts with a category - why not create a new loop in your Donor Profiles area that gets posts by that category and lists out the links (assuming the category is something like Health-Science or one of your three sections)? Or you can use post meta in your profiles if you don't want to pull a category for a menu created with a get_posts loop. I feel like I didn't make sense? Let me know.
- 0
- 2014-06-27
- RachieVee
-
不用担心的人.我对WordPress有相当不错的编码经验.我的问题是,如果我在Wordpress中使用帖子功能,并为这些配置文件设置特定类别,则Wordpress将single.php模板用于所有帖子.因此,"新闻与活动"中的帖子将具有我所不希望的相同侧边栏.我希望新闻和事件的侧边栏保持不变,但要显示个人资料帖子的相关帖子.因此,我试图找出如何动态读取当前页面类别以在左侧边栏中显示相关类别的方法.有时候我觉得我没有意义哈哈No worries man. I have a decent amount of coding experience with WordPress. My issue is if I'm using the post feature in wordpress and set up specific cateogories for those profiles Wordpress uses the single.php template for all posts. So the post in News & Events will have the same sidebar which I don't want. I want the sidebar that News & Events to stay as it is now but show the related posts for the profiles posts. So I'm trying to figure out how to dynamically read the current page category to display the related cateogories in the left sidebar. I sometimes feel like I dont make sense haha
- 0
- 2014-06-27
- jose
-
嗯,如果尚未将类别显示为类别,则如何处理主体类别-并将其用作检测侧边栏中的类别并通过检查类别主体类别来控制侧边栏输出的方法?我的印象是您的个人资料显示在页面中,所以不仅个别帖子会使用single.php吗?我想这也取决于您的主题...Hmm, how about manipulating the body class if it doesn't already display the category as a class - and using that as a means to detect the category in your sidebar, and controlling the output of your sidebar by checking that category body class? I was under the impression that your profiles were showing in a page, so wouldn't only the individual posts use single.php? I guess it also depends on your theme...
- 0
- 2014-06-30
- RachieVee
-
1 个回答
- 投票数
-
- 2014-09-09
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5,'post_type' => 'news', 'post__not_in' => array($post->ID) ) ); if( $related ) foreach( $related as $post ) { setup_postdata($post); ?> <li> <?php the_post_thumbnail(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> </li> <?php } wp_reset_postdata(); ?>
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5,'post_type' => 'news', 'post__not_in' => array($post->ID) ) ); if( $related ) foreach( $related as $post ) { setup_postdata($post); ?> <li> <?php the_post_thumbnail(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> </li> <?php } wp_reset_postdata(); ?>
我正在寻找创建自定义帖子类型的最佳方法,您可以在边栏中过滤与该自定义帖子类型相关的类别.
这是我正在使用的网站上.最初,学生和捐赠者档案应该是某种职位或类别,可以在创建新档案时动态更新.因此健康与科学,学生成功和创造力社区拥有自己的一套特定配置文件.
因此,可以说John Doe是Student Success的新学生资料,我可以标记他单独浏览该页面,当查看他的个人帖子时,我无法显示相关的类别或资料,在本例中为Student成功.因此,在查看John Doe的个人资料时,应在侧边栏中列出学生成功下的其他个人资料.
我注意到wordpress共享了single.php模板文件,因此我用于single.php文件的侧边栏被用于我们不需要的配置文件.
总体而言,如果您通过Health&amp;科学,学生成功与创造力社区我正在寻找这样的设置,而不是客户在上传新个人资料时必须执行的四步过程,而我只是在寻找一步过程.
我当前的设置是将新的配置文件添加为页面,然后转到仪表板中的菜单区域,然后将新的配置文件添加到菜单中.
我当前的页面模板代码.