Show a single post on other template/page - php

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.

Related

How to show only posts with certain category in wordpress theme?

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; ?>

Multiple post loops in Wordpress

I've built a site for a health practitioner with CPT of:
(single-injuries.php, single-services.php, single-testimonials.php, single-partners.php)
and I have the appropriate (archive-injuries.php, archive-services, archive-testimonials, archive-partners) created with loops to display the relevant posts.
HOWEVER
I now want to create a sitemap page that pulls ALL THE POSTS from ALL ARCHIVES and just displays the PAGE NAME and URL for each...
How do I loop through multiple archives, do I nest a loop for each within a loop?
You can use a custom query that queries all of your CPTs which you listed (put them into the post_type array), similar to this (which lists all post titles found, each linked to its full post):
<?php
$args = array(
'post_type' => array('injuries', 'services', 'testimonials', 'partners' ),
'post_status' => 'publish',
);
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
$post_title = get_the_title();
?>
<div>
<p><a href='<?php echo get_the_permalink(); ?>'><?php echo post_title; ?></a></p>
</div>
<?php endwhile; else: ?>
<p>Nothing found</p>
<?php endif; ?>
I suggest you should do this using database, or what archives mean at your post? If you are using database just make a query that will select all from different tables, like this
SELECT archive-injuries.*, archive-services.*, archive-testimonials.*, archive-partners.* FROM your data base
Then make a while loop that will display posts, while mysqli_fetch_assoc have some data

How can I show a certain post recognised by title on my archive page? - Wordpress

So I basically have some sort of list with certain menu items, I want the top menu item to show on the archive page so it doesn't look empty. The post is always on top so I think the easiest way is to pick it out by the title of the post. But I have no idea how i can do this.
<?php
$query = array (
'post_type' => 'results',
'order' => 'DESC'
);
$queryObject = new WP_Query($query);
if ( $queryObject->have_posts() ): while ( $queryObject->have_posts() ): $queryObject->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; ?>
This is my code basically and you can see that it posts the titles from all posts in links so that is is some sort of menu, on click it shows the content from the post.

Permalink from WP_Query posts not displaying posts content

I am creating a custom WordPress Theme from scratch and I want to display some news from a certain category. I am doing it with the following code:
wp_reset_query(); $args = array(
'type' => 'post',
'posts_per_page' => 3,
'category_name' => 'oportunidades' );
$opp = new WP_Query($args);
while($opp->have_posts()) : $opp -> the_post(); ?>
<h4> <?php the_title(); ?></h4>
<?php endwhile; ?>
The list of news are being displayed correctly, but when I click the link to a certain post, it will change the URL of the page, but not display the contents of the post. If I echo the title, contents, and hyperlink of the post, it will echo the correct information.
How can I fix this, so when I click the post it shows the contents of the post?
*note: I have created a file named content.php, but it has no code inside. Can that be the problem?

Wordpress custom post type menu generation

The problem: I have a site (under development) where I have generated a custom post type called "Products" . Inside I have different categories and posts. I would like to generate a menu like behaviour to my sidebar, but using google I haven't really stumbled onto something good. Who knows, maybe my idea is all wrong and is doable using some other solution.
Currently I have used WP Query to echo out all posts under one category. So this means that I have to manually copy code to echo new category with its posts when it is created. One solution is to create a menu manually, but i'd like to keep it auto-generated because users are not keen to do extra work and ofter forgot how it is done.
So, the question: Is my solution all wrong? How can i achieve menu like behaviour with class active for clicked category and its sub item? Is it better to just use pages?
Example of my code:
<?php
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'cat' => 1
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post(); ?>
<li>Category name, title</li>
<li class="cpt-menu-item" id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php endwhile;
echo '</ul>';
}
wp_reset_postdata();
?>

Categories