Hello I want to Create a home page that has updating blog entries.
So 4 lists of headlines from different categories
And I want to have a link to the regular blog page with a different template.
Right now I just changed index.php around to have the containers for the featured posts content.
So this is a two part question how do I get these mini updates for the thing
I want to use query_posts() multiple times I assume and separate by category.
And how do I make a linkable page to a blog.php file which currently is telling me that all these functions are undefined.
<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
If you want to create a page that includes WP data/posts outside the themes or blog folder, you need to include and make available the Wordpress functions first:
define('WP_USE_THEMES', false);
require('./blog/wp-blog-header.php');
And then you can make the queries for each set of posts by category:
$args = array( 'numberposts' => '5, 'offset'=> 1, 'category' => 'your category ID' );
$myposts = get_posts( $args );
foreach($myposts as $post) {
...some code...
}
I hope it helps.
Related
I was wondering if there is a way to change this code to only display posts from certain category created in wordpress. Now it displays every recent post. Let's say I would create "News" category in wordpress and I want this piece of code to display only News posts.
Thanks for help
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
You can override the usual query that wordpress uses and create a custom one;
https://developer.wordpress.org/reference/classes/wp_query/
usually though for just displaying a category you can just open the category slug and provided your template has the correct archive page it will display the posts for that category.
If not use something similar to below. You can further refine your search parameters like number of posts and post types as defined in the link above.
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
I've been searching all morning for answers to no avail. I'm building a one page WordPress template. I have a home page which uses a one page template called one-page.php that is brining in all the other pages. Heres the php from that template:
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; endif; ?>
This code works great. All the sections can use the content-page.php template part, excluding the portfolio section which I would like to use another template part that brings all the portfolio custom post types.
I've tried to add conditional if statements to both the one-page.php and the content-page.php, like this:
<?php if ( is_page( 'portfolio' ) ) : ?>
//My portfolio custom post type loop is here
<? endif; ?>
But that didn't work either - I think that is because the is_page() function will be checking the current page being displayed which is the Home page. Rather than figuring out what page the query is currently dealing with - but I'm not sure.
Can anyone help me understand how I would go about conditionally loading the portfolio section into a separate template part?
You can achieve this checking page slug, which you can get in the loop. If it is "portfolio" (or whatever you saved), load content-portfolio.php, otherwise content-page.php. Something like this:
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if ("portfolio" === $post->post_name) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
Set condition in file
<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
if (is_page('portfolio')) {
get_template_part('content', 'portfolio');
} else {
get_template_part('content', 'page');
}
endwhile; endif;
?>
create a template file named content-portfolio.php which is copy of content-page.php and put the below code in it. if it show 'call' it means your template is working.
portfolio.php
<?php
echo "portfolio.php";
die('Call');
?>
This is my second question, and I promise as soon as I feel like I can help others, I'm gonna return the favours to the community!
I'm building a one page Wordpress site with different sections, working from _S theme and learning as I go.
I already have all my different pages being pulled into the front-page, but I would like the different sections to have different layouts and elements. To be specific - in one section I would like content to be pulled in along side an iFrame that float next to each other. To do this I know that I need to call a custom page template that specifies two floating div containers (I'm building using Bootstrap).
In the front-page.php, I've written this:
<?php
if ( get_option( 'show_on_front' ) == 'posts' ) {
get_template_part( 'index' );
} elseif ( 'page' == get_option( 'show_on_front' ) ) { ?>
<?php get_header(); ?>
<section class="home">
<div class="entry-content">
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
?>
<?php
while($the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if (is_page(9)) : ?>
<?php get_template_part('play','page');?>
<?php else : ?>
<?php get_template_part('content', 'page');?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</section>
<?php
get_footer();
}
?>
Which works perfectly in bringing in my different page sections, but I can't see how to call a different for the page (called 'play' or with the ID of 9).
I have already created a template called play-page
Any ideas?
Thanks masses
Harry
Let's assume you want to display 3 sections (about, work, contact) on the custom front page, you can create per page template for each section, like:
section-about.php
section-work.php
section-contact.php
In page-about.php like this:
<div class="section-about">
<?php the_content(); ?>
</div>
And use section-work, section-contact for other 2 pages.
Then you create 3 pages in the Dashboard and assign each to its template.
Now, you can add the loop in your font-page.php
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1, 2, 3) // the ids of those pages
);
$the_query = new WP_Query($args);
?>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
the_title();
the_content();
// etc
?>
<?php endwhile; wp_reset_postdata(); ?>
I am building a personal website to host my university work, personal projects and photos etc.
The menu is a hierarchical structure made up of pages and links. Take my university pages for example. What I would like to achieve is to display posts that are related to the module code which is the page's slug.
I've used the following link http://codex.wordpress.org/Page_Templates#A_Page_of_Posts and managed to get it working but I have hard coded the module code into the template, meaning for each module I will have to have a separate template and the only thing that will be different from one file to the next is 5 characters which isn't great for code re-use.
What I am asking, is, is there a way to get the slug from the page I'm looking at and use that for the WP_Query arguments.
If you go to http://michaelnorris.co.uk/ and look at the menu structure. Navigate to University -> Year Three -> Individual Project, you will notice the url is http://michaelnorris.co.uk/uni/three/ci301 where ci301 is the module code for the Individual Project. I want to have this system on each of the module pages so that I can tag posts and they are displayed in the relevant module.
Ok, I actually found the answer myself, but for others looking to do the same. Below is a solution.
Solution found here on the Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts
Name the file pageofposts.php and edit the Page within the Wordpress Dashboard and set the Template (in the dropdown) to 'Page of Posts'. Bingo!
<?php
/*
Template Name: Page Of Posts
*/
/* This example is for a child theme of Twenty Thirteen:
* You'll need to adapt it the HTML structure of your own theme.
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
/* The loop: the_post retrieves the content
* of the new Page you created to list the posts,
* e.g., an intro describing the posts shown listed on this Page..
*/
global $post;
$slug = get_post( $post )->post_name;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display content of page
get_template_part( 'content', get_post_format() );
wp_reset_postdata();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// Change these category SLUGS to suit your use. category_name is comma separated.
'tag' => $slug,
'paged' => $paged
);
$list_of_posts = new WP_Query( $args );
?>
<?php if ( $list_of_posts->have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
<?php // Display content of posts ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I'm having an issue with the listings in the WordPress site I'm working on.
I have three listings only showing up out of 6. I can't seem to figure out how to make all of them display. This is using the twentyeleven WordPress theme.
The arrows on the right are used to move the gallery back and forth. Only one more shows up on the right side.
Here's the code I believe is generating it.
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php if ( is_home() ) {
query_posts($query_string . '&cat=-3');
}
?>
<?php
$page_name="Articles";
$page=get_page_by_title($page_name);
//echo $page->ID;
query_posts( 'cat=-1,-2' );
?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
Any help would be great, thanks.
Change your query_posts() function to the following:
query_posts( 'cat=-1,-2&posts_per_page=6' ); // You can change the post_per_page variable as needed
However, I would suggest using an $args array instead of a querystring to make your query. The same query would look like this:
$args = array(
'cat' => array( -1, -2 ),
'posts_per_page' => 6
);
query_posts($args);
It is much more readable and easier to update. Also, it's worth mentioning, you are adding a negative operator to your categories. In the query_posts function, that will exclude a category. You may only be getting 3 posts because you are excluding posts from your query.