wordpress loop clarifications about contents - php

To learn more about the latest version of WordPress, I'm reading the documentation. I've some questions about the loop. As I can understand this is the basic function that will display contents inside WordPress pages. I want to understand if this function is responsible to display only the posts or also it will display pages. If this will display both, how I can customize the appearance of the post list to differentiate it from the site pages contents?
I want to make some pages for my website and one of these will only be responsible to display a list of posts, so it will be a blog and not really a page. I'm a bit confused about, every question or article I've read talk about the loop as necessary to display contents.
This means that with this basic code I will display all my page contents?
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-6 col-lg-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
</div>
<?php endwhile; else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div>

Related

how link several pages, and adding a plugin to a new custom WordPress theme

I've created my own WordPress custom theme for a website that I'm working on. It's my first time building a theme from scratch! I'm having a difficulty 1st: to link several pages to the fron-page, or home page. For example lets say that I want to link "Who we are", page + "Contact us", page, etc.. How can I make WordPress understands that each page is different, and link to it? here is what I did so far:
in page.php I placed this code:
<?php get_header(); ?>
<div class="container">
<?php
if (have_post()) {
while(have_post()) {
the_post();
get_template_part( 'template-parts/content', 'page');
}}
?>
</div>
<?php get_footer(); ?>
I also created a content-page.php where I've placed my HTML code. (I'm building the whole thing with an HTML and CSS code that's just like building a website from scratch).
I also want to know when using a plugin, how can I connect that into the pages that I'm adding. For example, if I'm installing a form such as WpForms for a page like "Contact us" how can I link that?
Please let me know if my questions need more details
If I understand this correctly...
You want to be able to display (link) a couple of pages to your home page?
Ex:
[home page content here]
[Connect page content here] [Blurb page here]
If so, you need to pull in the page content into the home page.
Something like:
<?php
$postid = 0 /*Need to get page id, that's the key */;
//Pull in the page data
$featured_page = get_post($postid);
$thumbnail = get_the_post_thumbnail( $featured_page->ID );
//Only show the excerpt in this case
$content = apply_filters( 'the_content', $featured_page->post_excerpt );
?>
<article id="post-<?php echo $featured_page->ID ?>">
<header class="entry-header">
<h1><?php echo $featured_page->post_title ?></h1>
</header>
<div class="entry-content">
<div class="thumbnail">
<?php echo $thumbnail; ?>
</div>
<div>
<?php echo $content; ?>
</div>
</div>
Important! You'll need to get the page id from somewhere. You can hard-code it in, but that's risky if something changes. You can set up an Customizer option that lets the user select what page to use.
https://codex.wordpress.org/Theme_Customization_API
As for the contact form etc. I'd suggest putting it on a page as a shortcode, then displaying the full page, not the excerpt.
I use this code for a theme I built myself. If you need anymore help, let me know, I can show you where to find it.

How to display the content of a blog post using a custom post template?

I am using my own custom WordPress theme and I am running into trouble displaying the content of blog posts. I can display the title and the date it was published using php but I can't get any of the paragraphs, images, headings, etc. to display on the page. I am using Gutenberg blocks (default) for the content of the blog posts.
I have tried using php functions to grab the content but they don't seem to be working.
<div class="col-md-6 col-md-offset-3">
<p class="date"><span class="glyphicon glyphicon-time">
</span> <?php echo get_the_date();?></p><br />
<p><?php $content = apply_filters('the_content', $post-
>post_content);?></p>
</div>
I am expecting the content of the post to display within the div container but the function is not grabbing the content. Any help would be appreciated!
It sounds like you may be trying to retrieve the post content from outside the loop.
If you look at the post template for a theme e.g. 2017, it is this bit that does the magic. It’s not even necessary to pass a post ID:
<?php
while ( have_posts() ) : the_post();
get_template_part( 'components/page/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
E.g. you should just be able to do:
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
Might be a good idea to start with the code on the link above, or copy the single.php file for the theme you’re using and use that as the basis for your custom post page?

PHP slider based on current posts from active category in Wordpress

Hello fellow community,
I'm pulling my hair off for the past 3 days.
I tried many post / gallery / sliders plugins on Wordpress, and I cannot get the result I was looking for.
Basicaly what I would like to do is to generate a dynamic slider based on the current posts from the active category page. The posts are all listed under the same category. The slider must display ONLY the posts belonging to the current category.
I tried to customize the category.php page with Slider Revolution but it only retrieve "the most recent" post and not the rest.
I tried with many others plugins but they all display posts from other categories.
Thank you very much for your help.
So a bxslider could look something like this
<?php
if ( have_posts() ) : ?>
<div class="bxslider">
<?php while ( have_posts() ) : ?>
<?php the_post();?>
<div class="whateverClassHere">whatever content here <?php echo get_the_title(); ?></div>
<?php endwhile; ?>
</div>
<?php endif; ?>
And then you would initiate it with Jquery
$('.bxslider').bxslider(); //read the docs for settings here.

Wordpress functions not working in custom theme

So I'm super new to Wordpress. I set up a Wordpress local server on MAMP yesterday and I'm trying to build my own theme currently. Trouble is, whenever I try to do the loop, I'm not getting any content. my code works if I just use php, but the problem is custom Wordpress functions aren't usable (i.e. have_posts())
A couple other posts have suggested requiring wp-blog-header, but that hasn't worked either. Here's my code:
<?php
define('WP_USE_THEMES', false);
require('../../../wp-blog-header.php');
if(have_posts()) :
echo 'testing'
else :
echo 'testing'
endif;
?>
Nothing currently displays on my screen.
wp-blog-header.php is located here: wordpress -> wp-blog-header.php
My custom theme is located here: wordpress -> wp-content -> themes -> firstTheme -> index.php
All tips are appreciated.
Well, you're writing PHP and your code contains
if(have_posts()) :
echo 'testing'
else :
echo 'testing'
endif;
I'm not sure how can this be valid in PHP. I'd expect
if(have_posts())
echo 'testing';
else
echo 'testing';
See also if and else syntax in PHP.
I found this issue as well in building a few wordpress sites. After installing a PhP plugin (Insert PhP) my problems were solved.
Once installed, was changed to [insert_php] and [/insert_php] and my code worked.
You need to use the proper WordPress functions to build your own custom theme.
For example, instead of require and then x-path deep (which is also not wanted from WP-Core perspective) you should put your file in your theme's root.
like :
`get_template_part('name-of-file-to-include-without-php-ending');`
Reason for this is the fact, that WordPress Codex proposes the use of child theme functions.
The get_template_part(''); function does some checks, e.g. if a child theme has been installed etc.
Question: Why do you define that constant?
All constants should reside inside wp-config (root folder).
Additionally, it is worth mentioning that you are not looping throught the posts. Your code, at this point in time of writing, only looks IF there are posts, and then does nothing. You need to add the while function as well in order to work through your posts and display them.
So, do (in your loop.php or at the place in your theme where you want to display them):
<?php if(have_posts() ) : ?>
// The while added
<?php while ( have_posts() ) : the_post(); ?>
// Your template tags here: e.g. the_author();
<h2><?php the_author(); ?></h2>
<h3>the_title();</h3>
etc...
<?php endwhile?>
<?php endif; ?>
One last thing: WordPress has some conventions, which files should reside in the theme folder. For example, every theme should have a functions.php where you put stuff like menu and widgets etc. inside.
The most popular tags to build a theme are:
get_header();
Sure, you need to have a header.php file in your theme. Again, WordPress will look for exactly these files to include, no other naming is allowed.
get_footer();
Make a guess, right - it will look for footer.php in your theme folder.
For more information please see the WordPress Codex > Template Parts, etc..
https://codex.wordpress.org/Theme_Development#Template_Files
I highly advice you to use the WordPress core functions for templates, ignoring them and do 'classic' php e.g. include/require will lead you the dark side and not be successful.
A good plugin in the see your which template parts you are using is "What the File". Grab a default theme, install that plugin and look in the admin bar what it says to get an idea of what WordPress is doing.
Here is an excerpt of my loop, in my theme which uses bootstrap.
<?php if(have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(is_front_page() || is_page() || is_single() ) : ?>
<div class="row row-content">
<div class="content col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php the_content(); ?>
</div>
</div>
<?php // category.php, archive.php, search.php ?>
<?php elseif(is_category() || is_archive() || is_search() ) : ?>
<div class="row row-excerpt">
<div class="thumbnail-box col-lg-4">
<a href="<?php the_permalink(); ?>" class="preview-image-link">
<?php
// Thumbnail und Post Auszug
if(has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
?>
</a>
</div>
<div class="text-details-box col-lg-8">
<div class="row row-excerpt-text">
<div class="excerpt col-lg-12">
<?php the_excerpt(); ?>
</div>
</div>
<div class="row row-tags row-read-more">
<div class="tags col-lg-8">
<?php the_tags('<ul class="tag-list">
</div>
<div class="read-more-boxcol-md-4 col-lg-4">
<a href="<?php the_permalink(); ?>" class="read-more-btn">
<span class="read-more-btn-text">></span>
</a>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>

Why do wordpress theme loops in a single page

I am new to wordpress and wordpress loop. I am trying to understand the loop but without any success. Baer with me I will try to explain what I am not understanding ...
I using a template called 'graphy'. When I create a 'Page' there is an option to create a page with no side bar the template is called 'nosidebar.php' and this is its code:
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; // end of the loop. ?>
</main><!-- #main -->
</div><!-- #primary -->
1- Why this template contains a loop ? where it only displays a single page content without side bar ! Obviously it is not looping through posts and displaying them !
I tried to create my own template page which will be used only for front-page and here is what I came up with
<?php
/**
* Template Name: Main_Page
* Description: A page template without sidebar.
*
* #package Graphy
*/
get_header();
?>
<!--<div id="primary" class="content-area">-->
<!--<main id="main" class="site-main" role="main">-->
<div id="main_content">
<?php
the_content(); // the content
?>
</div>
<!--</main> #main -->
<!--</div> #primary -->
<?php get_footer(); ?>
However when I installed this plugin which is used to insert widgets to pages and posts
with the main_page no widget is displayed but when I switched to "no sidebar page" it worked.
I then copied the loop into my main page and it worked.
2- What is the secret that this loops makes the plug-in work, while calling only <?php the_content() ?> does not ?! Obviously this loop makes some other things than what 90% of the posts on the internet explain.
On your first question, page templates does output information, that is why you see the loop. This information that is shown is the information entered in the page editor screen inside the tinymce editor.
For better understanding, go and read these two posts I've done recently on WPSE
Guidance with The Loop for CMS
The Loop in Static Page
On question two, the_content() does not output sidebars and widgets, but the content entered into the post editor. Sidebars are displayed with specific calls.
You will need to go and look how your theme register sidebars. I also suspect that your sidebar's behavior is manipulated by body_classes. Unfortunately here I can't help as this is quite very specific to your theme
Its all about the_post(); ,there is no need for any while loop there.
Try this
<?php
the_post();
the_content();
?>
It will work. the_post() is the function that retrieves a post content.
The while loop is needed only when retrieved are called from a category.

Categories