Make a custom URL and link to custom Template - php

I have a set of categories in my WP site. I also have a "team" thing in my site. Now when a user makes a new post he selects a category and then chooses the team to which post belongs.
I have added this team selectbox using meta boxes Now i want to have a link for each team.
How can i do that??..
I though of 2 ways:
(1) - Making a template and linking it to a page called team. So its url becomes :
site.com/team/
And now for each team making url like
site.com/team/team1
I thought that 2nd url will also be served from my template. But, instead it gave not found.
(2) - Making a custom page for each team.
Is there any other way???

Could create post-types, team 1, team 2, team 3. Than create a single-team1.php and use that template file to list stuff that has to do with team 1. Example:
<?php
$args = array(
'post_type' => 'team1',
'' => '',
'' => '',
'' => '',
);
$team_one_stuff = new WP_Query( $args );
if ( $team_one_stuff -> have_posts() ) : while ( $team_one_stuff->have_posts() ) : $team_one_stuff -> the_post();
?>
<!-- Do WP stuff here -->
<?php endwhile; else: ?>
Wow, nothing assigned to team 1... o__O
<?php endif; ?>
You could pimp-up the metaboxes you use for team by using ACF. Realy easy to create new metaboxes for certain pages or post-types whith that plugin.
Hope it helps.
/Paul

Related

Wordpress Query Posts from specific subcategory of Custom Post Type

Okay guys, this one has me somewhere between ripping my hair out and kicking in my monitor. It seems no matter what I try, no matter how many times I re-write the query, change terms, change syntax...I get nothing.
So I have a website I am working on where we have a custom post type of 'Vinyl' for vinyl records of in an online collection the client wants displayed alphabetically, with a category of 'vinyls' inside the custom post type section. The client then further wanted to separate things down and create a child category within 'vinyls' called 'vinyl_ae' (Vinyls alphabetically sorted by the first letter A through E). Now, I have the issue where I am trying to query any post at all from the vinyl_ae category/sub-category/whatever the hell it is at this point, and NOTHING turns out. The only way I get any results at all is to set an else conditional for the if have_posts() statement. I'll try to include any all data I can here to help sort this mess out.
Custom post type - name : vinyl
main category - name : VINYLs, slug : vinyls, ID : 3
child category - name : Vinyl A-E, slug : vinyl_ae, ID : 4571
Screen Shots of all my category and sub-category layouts
Here is my working Code Currently (take in mind I have stripped it down so much tonight there is not much left and I have tried so many different solutions to the point where I am practically copying and pasting based on results others are having)
<section id="main">
<div class="content-holder no-spacing">
<div class="container">
<div class="content-inner">
<div id="ajax" class="records row">
<?php
$args = array(
'post_type' => 'post' ,
'posts_per_page' => 6,
'cat' => '4571',
'paged' => get_query_var('paged'),
'post_parent' => $parent);
$mv = new WP_Query($args);
if ( $mv->have_posts() ) {
while ( $q->have_posts() ) {
$serial = get_field('serial');
$mv->the_post(); ?>
<div class="serial-num"><?php echo $serial; ?></div>
<?php } ?>
<?php } else { ?>
<em>Things Still Screwy</em>
<?php } ?>
</div>
</div>
</div>
Thank any and all in advance for any help that can be given, I've thrown in the towel here.
There is two things that you should consider in your sample code:
You should set post_type argument to your custom post type vinyl not the post post type.
If the serial is a field of your post you should call the $mv->the_post(); line first and after that call the $serial = get_field('serial'); statement, because before the_post() call, you can't access post's meta data.

How to show two different categories in wordpress?

Actually i've a static website and i'm trying to convert it into wordpress, i'm new to wordpress. I have successfully created following page.
header.php
footer.php
sidebar.php
functions.php
index.php
Then i have created a main-template.php page for my other pages like about, contact, gallery, etc, They will all be using same template. The website is almost completed but only two pages are left.
1. Jobs
2. News & Events
So in these pages, There will be new jobs coming, and same for news & events. So i think these two pages will be like a blog post.
Jobs page will display all the jobs with title and 100 words description and read more button, Then it will open in new page for that particular job.
I have will different job categories like electrician, plumber,mason,english language,call center etc
I want to show all categories jobs on Jobs page.
So I have created a Page in wp-admin>pages>add new called Jobs and newsEvents and selected template main-template that i have used for all pages. But this page will be blog type to show all jobs.
As you know I will be having two blog type pages one for JObs and one for News Events So I have only created two categories Jobs & NewsEvents
Now If i add a new job, I will go to wp-admin>posts>add New then add title, description, select category Job if it is job, similarly for newsevents.
So the question is How do i show jobs on jobs page and newsevents on newsevents page?
How to create single.php?
How many single.php do i have to create?
I have created `single.php`
<?php
/**
* The template for displaying Jobs
*/
/*get_header();*/
get_header();
?>
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', 'Jobs' );
endwhile;
?>
<?php
get_footer();
?>
and I have created Empty jobs page in wp-admin>pages>Job its link is localhost/MyProject/jobs I want to show jobs on this page, all jobs that i will post..
I have created content-jobs.php page too, Now what should i do next? how will i show jobs?
my content-jobs.php
<?php the_content() ?>
I have already read wordpress documentation but unable to understand properly, this is my first time doing it, Help me?
1) you need create separate template for these pages.
2) Create the pages with receptive names
3) Use these templates on these pages in admin
4) Create custom post type News& Events
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'News',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News' )
),
'public' => true,
'has_archive' => true,
)
);
}
add this code in function.php
after that post some data in this post type
and to show this on front end in your template file use.
$args = array( 'post_type' => 'News', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
use this it will help you

Wordpress - Static front page should show latest posts

I want to have a static front page on my WordPress and this static one should display the latest 5 posts and below that up to 6 custom meta fields. Thats why I changed the reading settings from last posts to "Static page", because otherwise I would not have the possibility to access the custom meta fields.
How can I now include the latest 5 blogposts on my front-page and use the default post template/output.
I did it with this query:
$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 5 ) );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
// Loop output goes here
endwhile; endif;
Do i now have to rewrite the code for the post here completly or can I just include the post template in some way?
Thanks!
You need to write this line to show latest posts:
<?php
wp_get_archives(array('type' => 'postbypost', 'limit' => 10, 'format' => 'html'));
?>
Check this link for more details
Instead of WP_Query you should try
wp_get_recent_posts( $args, $output );
Check more details here

Dynamically call categories into WordPress template based on page name

Agh this is frustrating. I've been searching for hours on how to do this.
I am creating a WP template that pulls in queries of a category on the page.
My question is how can I make the category in the array dynamically load, whether by slug or whatever, based entirely on what the page title is?
The fancy piece is that it will be three sections on the page via an additional category that also need to be loaded in the template, but those are not dynamic.
So if I can break this down:
Section 1 - TitleCat + Section1Cat
Then
Section 2 - TitleCat + Section2Cat
Then
Section 3 - TitleCat + Section3Cat
That way during content creation, my team can simply click two categories, and the website will automatically put the post excerpts where it needs to go!
It needs to be dynamic so I don't need to build a template for each of the 31 categories the posts are divided into.
EDIT: This is how I was approaching it using ACF. That array can be simply switched out for categories if that's the way to go. Same concept.
<?php $pagecat = $post->post_title; //this copies the page title the page title ?>
<?php
// args
$args = array(
'posts_per_page' => 4,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'page',
'value' => $pagecat,
'compare' => '='
),
array(
'key' => 'section',
'value' => 'Second String',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
?>
On any custom / template page you can use this many times over, for example on home page you want to show samples of articles in many different categories:
Code Sample
<!-- Create query -->
<?php query_posts( array ( 'category_name' => 'category-slug-goes-here', posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// CODE THAT YOU WANT HERE
<?php endwhile; ?>
// IMPORTANT RESET QUERY
<?php // Reset Query
wp_reset_query(); ?>
Now you can do this OVER and OVER again as many times as you need on one template...
Now for displaying your SINGLE article for some category you can do this in a "Single.php" page use http://www.wordpress.org website for more references on this..
Here is the code for single.php page you need an IF statement and you can target your specific categories that you want to display a specific way...
<?php if(have_posts() && (in_category('category-name-here') || in_category('category-name-here-2') || in_category('category-name-here-3') )) : while(have_posts()) : the_post(); ?>
// NOW HERE use HTML to format the article for those categories.
Now you said you have 30+ different categories I assume you want different displays..
Now you will use that same IF statement over and over again and change the HTML format code in the middle. So you will have in the end ALL page layouts inside 1 page and you will target them by category-name
Best of luck!

Custom Field Menu Linking To Custom Field Pages

I'm having a hard time linking a menu built with custom fields to a page built the same way. I'm sure the problem is making it link via permalink, but for the life of me, I can't figure out what I'm missing.
<?php $loop = new WP_Query( array( 'post_type' => 'Employees', 'posts_per_page' => 100 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div>
<?php the_title(); ?><br />
<?php the_field('title'); ?>
Basically what I'm trying to do is on http://www.consociate.com/about/meet-the-team/, I want the links on the right to go to a template filled with custom fields (their bio information). Is this possible?
Thanks so much! I love this community!
What you are looking for can be achived by respecting the wordpress template hierarchy rules, you shouldn't hardcode links or hardcode add parameters to the links, simply use <?php get_template_part( $slug, $name ); ?> then create the resonse php file that will display the info as you want.

Categories