简体   繁体   中英

WordPress Search Results on Page

I am creating a website for an upcoming election here.

My WordPress site has a category called 'In The News' which contains news articles for all of the candidates, as well as other information relating to the election.

I also have another section for the candidates, which each candidate having their own page. These candidate pages will list some information about the candidate, the area they are running in, a photo, and a comments section.

What I want to do is include some of the stories from the 'In The News' section automatically on each candidates page.

So, for example, If we were looking at a page for Jane Doe, a section on the page could say 'Recent News About Jane Doe' and would show the 5 most recent stories for her.

Basically, what I need to do is return search results for a term on the candidate page automatically.

Any ideas on how I can accomplish this either through a plugin, of using PHP or something?

Thanks in advance for any help you can provide.

This is the simplest way I can think of to link a category's post with a page.

Use Advanced Custom Fields to create a new field in your pages called "News Category" with a Field Type of 'Taxonomy' and the Taxonomy of 'Categories'. This will allow you to select the categories to associate with each page.

In your template file for your candidate pages, use wp_get_recent_posts ( http://codex.wordpress.org/Function_Reference/wp_get_recent_posts ) and set your 'category' in your $args to the the result of get_field() ( http://www.advancedcustomfields.com/resources/functions/get_field/ ).

This will return your most recent posts for the category you select for that page. All you have to do is format the data as you'd like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM