Wordpress - Individual pages as sections on front-page - wordpress

I created multiple pages like services, about, our team... in wordpress and i can modify content of those pages creating individual php files for each page like page-services.php or page-about.php so i can use advanced custom fields plugin in those pages. But what i need to do now i to include those pages as sections in my front-page or static-page so those previous pages are actually sections in my front-page. And i try to do this with get_template_part() and with get_post() and get_page() and include() but nothing is working. I look for answer all over internet but i just cant find it and i think it should be very simple. I rly appreciate any help.
<div id="service" class="text-center">
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<h3><?php the_title(); ?></h3>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p><?php the_field('service_subtitle'); ?></p>
</div>
</div>
<div class="row">
<?php if(get_field('service_box')):
while(has_sub_field('service_box')): ?>
<div class="col-sm-4 column-box wow fadeInUp animated" data-wow-delay="0.3s">
<h3><?php echo the_sub_field('service_box_title'); ?></h3>
<div class="title-line"></div>
<p><?php echo the_sub_field('service_box_content'); ?></p>
</div>
<?php endwhile;
else :
// no rows found
endif;
?>
</div>
</div>
This is my code on service page or in page-service.php file. And now i need to include this page or this code in my front page. Also i have multiple pages (sections) like this which i need to include in my front page also. End i dont want to just copy this and rest of code in front-page i wont it to be on separate pages and then include those pages in front page.

You can query the page content based on ID and insert to your current front-page code. Like the code below.
<?php
// Assume that the Id of the page you need to include is 7.
$mypages = get_pages( array( 'include' => '7', 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<h2><?php echo $page->post_title; ?></h2>
<div class="entry"><?php echo $content; ?></div>
<?php
}
?>

Related

Wordpress custom post types current post

What I have is a podcast page with multiple podcast. but when I click into a podcast, it takes me to the most recent podcast and not the one that I had clicked on. How do I get the current podcast and not the most current one that was uploaded? below is where I am calling the the post to the new single.php file that I have created. Thanks in advance.
<?php
$new_loop = new WP_Query( array(
'post_type' => 'custom-post-type',
'posts_per_page' => 1
) );
?>
<?php if ( $new_loop->have_posts() ) : ?>
<?php while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<div class="container mycontainer">
<div class="row">
<h1 class="whiteText col-md-12">hs</h1>
<h2 class="whiteText episodeTitle col-md-8"><?php the_title(); ?></h2>
<span class="whiteText col-md-12"><em>Hosted by</em></span>
</div>
<?php the_content(); ?>
</div>
</div>
You are overwriting the default loop with your own WP_Query which is set up to just return the most recent post of type custom-post-type.
Is there a reason you are not using the standard Wordpress loop? If you have everything set up properly, your single.php should show the correct post by default without having to re-query for it:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="container mycontainer">
<div class="row">
<h1 class="whiteText col-md-12">hs</h1>
<h2 class="whiteText episodeTitle col-md-8"><?php the_title(); ?></h2>
<span class="whiteText col-md-12"><em>Hosted by</em></span>
</div>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;?>
<?php endif; ?>
Just use the_permalink() as $post will return the ID of the main post outside of your loop.

Wordpress posts missing in the admin panel, but visible in the database

I use a downmloaded wordpress code, the home page displays two banner images banner.jpg and banner2.jpg.( it seems to used bxslider ). But I could se the code where the slider images embedded. It is in header.php. Code attached below.
I could see only 12 posts in the admin panel, but if I search the banner image names(banner.jpg and banner2.jpg.) in the database, I can see them in two separate posts which are not available or access from the admin panel.
Database shows more than 200 posts, but admin panel shows only 12.
<!--banner starts-->
<?php if (is_front_page())
{
?>
<section class="banner">
<ul class="bxslider">
<?php
$type = 'slider';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$feat_image = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<li><img src="<?php echo $feat_image; ?>" alt=""/>
<div class="banner-pattern"></div>
</li>
<?php endwhile; } ?>
</ul>
<div class="banner-content-top">
<div class="container">
<div class="row">
<div class="col-lg-5 col-md-6 col-sm-12">
<div class="banner-content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
</div>
</div>
</div>
</div>
</section>
<?php } else
{
}
?>
<!--banner ends-->
My questions are:
1) which images are pulled by the code line
( wp_get_attachment_url( get_post_thumbnail_id() ))
displays. Is it the image we set as featured one in a post?
2) why the admin panel shows only 12 posts, even though the database has more than 200 posts in it?
3) Why the posts with the banner image not visible in the admin panel.
I'm try to answer all your questions:
1) The function get_post_thumbnail_id() gets the featured image of your post.
2, 3) Maybe this website only have 12 posts. Wordpress save in wp_posts table, all the posts, pages and the navigation menu items.
You can read more about what stores in this table here

Wordpress the_content / post is empty

I'm building my own template for Wordpress and I'm bumping into an issue when displaying the content of a post.
I have already built one page template for the home page and it works fine. The loop outputs what I want to display. Now I'm building the template to display an article but the loop doesn't return anything.
Here is the code of the page template:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
This results in displaying the "sorry, no posts matched your criteria" message when i click on one of the links on the homepage. The strange thing is that the page really exists (it has ID=26 as you'll see here below):
<?php
$post = get_post(26);
$title = $post->post_title;
echo $title;
?>
This works and displays the expected title. I have tried get_the_ID(); to get the post ID but it returns an empty variable.
There is probably something missing in my template but I can't figure out what.
Any idea?
Thanks
I have found the issue and it had nothing to do with the template itself.
I discovered what's wrong by using one of the standard Wordpress themes (twentyfifteen) where every post led me to a 404 even if I clicked on a post from the Admin UI. I swapped back permalink structure to the ?p=123 option and there everything working. Definitely a permalink structure problem.
The problem was coming from the polylang plug-in. I'm using a network of wordpress site for which I have made this plug-in available. I did not need this plug-in for this particular site but somehow it needed to be active anyway. So I activated polylang and configured it to have only one language and now it works.
Got new gray hair in the process but if that can help anyone...
Thanks for your help!
Laurent
I don't think, that this template can output any posts like this. For WordPress
/*
Template Name: PAGE
*/
indicates: this is a page-template which you can asign to the content of a page but not to a post.
When you want to show a specific post on such a page you will have to:
Asign the template to the page in WP-backend
Add a new query to the template to show the post
Like:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
// The new Query
$args = array (
'p' => '26',
);
// The Query
$query = new WP_Query( $args );
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
But as this is very complicated I would recommend to:
delete the /* Template Name: PAGE */ from the template
save it as single.php or more specific as single-26.php
link to the desired post in your wp-menu

How to make a page template for displaying custom type posts

I just started WP, and made a table with many rows arrived at WP house. But don't know how to show. basic content well shown but custom fields. learnt that a page have to be created to deal them in order to retrieve custom typed posts.
the following is from my content-movie.php under twentyfourteenchild:
/* translators: %s: Name of current post */
the_content();
// handling movie stuff starts
$p_id = $post->ID;
$ar_fields = array( 'studio','director','starring','grade');
.
.
foreach $ar_fields as $field
$some_field = get_post_custom_values($field, $p_id);
do some thing dealing $some_field...
end for
===================
In order to make a regular page to populate movie-typed custom posts, do I have to put such codes in archive-movie, page-movie single-movie etc ?
I guess, somewhere it can be dealt instead of putting same scripts in many files.
I really want someone to help me go right direction.
firstly create a custom page template by doing the following:
Copy the regular page.php and rename it to page-movies.php.
In the page header add the following php code:
/**
* Template Name: Movie Page
*/
Now the page template should be available in the backend, select the Movie Page as your page template.
Now replace the regular page query with your query, here is an example below:
<?php $args = array( 'numberposts' => 7, 'order'=> 'ASC', 'post_type' => 'movies');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="item">
<div class="item-content">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumbnails"> <a class="ajax" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'projects-thumbnail' ); ?>
</a>
<div class="copy">
<h3><a href="<?php the_permalink(); ?> ">
<?php the_title(); ?>
</a></h3>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
This should do the trick...

How to Modify Taxonomy.php to Show All Posts for Custom Post Type and Custom Taxonomy?

I'm building a Wordpress site, and I'm trying to figure out how to modify taxonomy.php to display all of the posts in a custom category and custom taxonomy, but I'm stuck. Here's the situation:
I have a custom post type called 'work.' Within that custom post type, I have a custom taxonomy called 'project type' where I have created several categories called photography, video, print design, etc. I set up a template called taxonomy.php which governs mydomain.com/work as well as mydomain.com/project-type/print-design/. The problem is that these pages only display the 5 most recent posts. I need them to display all the posts in the proper category (or in the case of mydomain.com/work, all of the posts in the custom post type regardless of category).
I found this page about query_posts which helped me realize that I needed to add <?php query_posts( array ( 'post_type' => 'work', 'posts_per_page' => -1 ) ); ?>. Now the work page is loading correctly, but all of the category pages are loading all the work posts (because I've defined the post_type as work in that snippet I added).
How can I generalize the code so that it makes the work page display all of the work posts and the category pages display only the relevant category posts? Here's the code in full:
<?php
/**
* Project Type Taxonomy Archive
*/
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<h3 style="color:#EEEEEE"><?php echo apply_filters( 'the_title', $term->name ); ?> PROJECTS</h3>
<ul class="thumbnails">
<div id="work">
<?php if ( !empty( $term->description ) ): ?>
<div class="archive-description">
<?php echo esc_html($term->description); ?>
</div>
<?php endif; ?>
<?php query_posts( array ( 'post_type' => 'work', 'posts_per_page' => -1 ) ); ?>
<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
<div class="post">
<li class="span4">
<div class="thumbnail">
<div class="tint"><?php the_post_thumbnail( 'featured-thumb' ); ?></div>
<br />
<div class="thumbcaption">
<h3><?php the_title() ?></h3>
<p> <?php the_excerpt(); ?></p>
<p><i><?php the_terms( $post->ID, 'work_project_type' , ' ' ); ?></i></p>
</div>
</div>
</li>
</div>
<?php endwhile; ?>
<div class="navigation clearfix">
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
</div>
<?php else: ?>
<h2 class="post-title">No Projects in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
<div class="content clearfix">
<div class="entry">
<p>It seems there aren't any projects in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, I try to add new projects regularly.</p>
</div>
</div>
<?php endif; ?>
</div>
</div>
One way to deal with this would be to not add a custom template.
When your go to a url relating to a taxonomy archive wordpress has already got all the posts out of the database. By creating a separate template although you have easier control over the query args like 'posts_per_page' it would be nicer to not have to do your own query.
Instead consider doing a pre_get_posts filter. Something like this.
// if on a certain taxonomy archive page, do not limit the posts
add_action('pre_get_posts', function($query){
if (isset($query->query_vars['name_of_your_taxonomy'])) {
add_filter('post_limits', function($limit){
/*
default will be something like this 'LIMIT 0, 10'
but we don't want a limit so return empty string
*/
return '';
});
}
});

Resources