How to show two different categories in wordpress? - php

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

Related

Show a single post on other template/page

Working on a site, and I need to have a separated template/page for showing just that one post.
On the home page (index) Ill loop through my categories and showing just a part of them - events - news - references.
Like this:
<?php
$query = new WP_Query( array( 'category_name' => 'Events' ) );
if ($query->have_posts())
{
while ($query->have_posts()) : $query->the_post();
$haveEvents = true;
if ($eventCount < 3) {
$eventCount++;
?>
<div class="event-tile">
<div class="event-tile-content">
<h3><?php the_title(); ?></h3>
<h4><?php the_time('F jS, Y'); ?></h4>
Lees meer
</div>
</div>
<?php
}
endwhile;
}
Every section has a read all and read specific item button:
I already found out how to display all posts within a category on a separated page named: page-posts.php
On page-posts.php I simply check on which page I am and depending on which page, make a query to show the posts within a specified category
$query;
if (is_page('events')) {
$query = new WP_Query( array( 'category_name' => 'Events' ) );
}
elseif (is_page('news')) {
$query = new WP_Query( array( 'category_name' => 'News' ) );
}
if ($query->have_posts())
{
etc..
Now what I cant figure out (not even after spending like hours on google), how to show a single post from the home page (index.php), on a single-post page...
On the action buttons I use:
Lees meer
Which brings me back to the homescreen. (just started on Wordpress)
In a Wordpress theme single.php is used to display a post. If it does not exist it drops down to using your index.php
Try moving your page-posts.php to single.php
Maybe its better to use archive-events.php. You can make an archive for custom posts.
With that you can do: where events is your custom post-type. Then you land on the events archive page.
As far as i can see your problem is that the post is the global post (from your home). So maybe you can setup_postdata($post), or use the home_url('url') functions.

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

how to display posts from all page in wordpress

Currently I am trying to develop wordpress one page portfolio theme. Now it shows blog posts and page posts. I have created a custom page template for displaying one page portfolio items. It will display post from those pages which user will create from theme menu.
Now I need to create that, but I have no idea how to do that. Please note that it will display only those page posts which user created from theme menu , and it will show those page navigation link(although I dont need any help about that , but I need help to show pages post) , and another thing is that , it will not display blog posts.
Run a WP_Query on your index page, and inside the loop add the page contents
<?php $args = array(
'post_type' => 'page', // Calling pages only
'order' => 'ASC',
'posts_per_page'=> '-1', // display all pages published
);
$loop = new WP_Query( $args ); if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();?>
<?php the_title(); //Page Contents etc.?>
<?php endwhile; endif; wp_reset_postdata();?>

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!

Make a custom URL and link to custom Template

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

Categories